Skip to content Skip to sidebar Skip to footer

Calculate The Point Of Intersection Of Circle And Line Through The Center

How do i get the point of intersection of a line and a circle.. I got lots of information on this topic, but my requirement is not matching.. I got a line whose one end point lies

Solution 1:

A JavaScript Version of Lindenhovius answer would look like this:

/**
 * Finds the intersection between a circles border 
 * and a line from the origin to the otherLineEndPoint.
 * @param  {Vector} origin            - center of the circle and start of the line
 * @param  {number} radius            - radius of the circle
 * @param  {Vector} otherLineEndPoint - end of the line
 * @return {Vector}                   - point of the intersection
 */functionfindIntersect (origin, radius, otherLineEndPoint) {
    var v = otherLineEndPoint.subtract(origin);
    var lineLength = v.length();    
    if (lineLength === 0) thrownewError("Length has to be positive");
    v = v.normalize();
    return origin.add(v.multiplyScalar(radius)); 
}

But you need to implement the vector "class"* your self:

function Vector (x, y) {
    this.x = x || 0;
    this.y = y || 0;
}

Vector.prototype.add = function (vector) {
    return new Vector(this.x + vector.x, this.y + vector.y);
};

Vector.prototype.subtract = function (vector) {
    return new Vector(this.x - vector.x, this.y - vector.y);
};

Vector.prototype.multiply = function (vector) {
    return new Vector(this.x * vector.x, this.y * vector.y);
};

Vector.prototype.multiplyScalar = function (scalar) {
    return new Vector(this.x * scalar, this.y * scalar);
};

Vector.prototype.divide = function (vector) {
    return new Vector(this.x / vector.x, this.y / vector.y);
};

Vector.prototype.divideScalar = function (scalar) {
    return new Vector(this.x / scalar, this.y / scalar);
};

Vector.prototype.length = function () {
    return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2));
};

Vector.prototype.normalize = function () {
    returnthis.divideScalar(this.length());
};

*there are no real classes in JavaScript — just constructor functions and prototypes, things change with ES6 but that's another topic.

Solution 2:

Simple solution in pesudocode:

Point findIntersect(Point origin, double radius, Point otherLineEndPoint) {
    Vectorv= otherLineEndPoint - origin;
    doublelineLength= v.length();    
    assert (!Math.isZero(lineLength));  //assert line has positive length
    v = v / lineLength;   //normalize vreturn origin + v * radius; 
}

Solution 3:

As a typescript class, don't know how to operator overload however:

classVector {
    constructor(public x = 0, public y = 0) {}
    add = (vector: Vector) =>newVector(this.x + vector.x, this.y + vector.y)
    subtract = (vector: Vector) =>newVector(this.x - vector.x, this.y - vector.y)
    multiply = (vector: Vector) =>newVector(this.x * vector.x, this.y * vector.y)
    multiplyScalar = (scalar: number) =>newVector(this.x * scalar, this.y * scalar)
    divide = (vector: Vector) =>newVector(this.x / vector.x, this.y / vector.y)
    divideScalar = (scalar: number): Vector =>newVector(this.x / scalar, this.y / scalar)
    length = (): number =>Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2))
    normalize = () =>this.divideScalar(this.length())
    findIntersect(o: Vector, p: Vector, radius: number) {
        let v = p.subtract(o)
        const lineLength = v.length()
        if (lineLength === 0) thrownewError('Length must be positive')
        v = v.normalize()
        return o.add(v.multiplyScalar(radius))
    }
}

Post a Comment for "Calculate The Point Of Intersection Of Circle And Line Through The Center"