3

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 at the origin of the circle.. and other end lies somewhere outside the circle.. Now i need the point of intersection of this line and circle..

I have tried to find closest edge point from outside the circle using below formula however unable to crack it -

closestCirclePoint = function(px, py, x, y, ray){
    var tg = (x += ray, y += ray, 0);
    return function(x, y, x0, y0){return Math.sqrt((x -= x0) * x + (y -= y0) * y);}(px, py, x, y) > ray ?
        {x: Math.cos(tg = Math.atan2(py - y, px - x)) * ray + x, y: Math.sin(tg) * ray + y}
        //{x: (px - x) / (length / ray) + x, y: (py - y) / (length / ray) + y}
        : {x: px, y: py};
};

Any way to get the solution for this problem..?? Thanks in advance!

brandonscript
  • 68,675
  • 32
  • 163
  • 220

3 Answers3

6

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
 */
function findIntersect (origin, radius, otherLineEndPoint) {
    var v = otherLineEndPoint.subtract(origin);
    var lineLength = v.length();    
    if (lineLength === 0) throw new Error("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 () {
    return this.divideScalar(this.length());
};

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

Moritz
  • 432
  • 5
  • 15
2

Simple solution in pesudocode:

Point findIntersect (Point origin, double radius, Point otherLineEndPoint) {
    Vector v = otherLineEndPoint - origin;
    double lineLength = v.length();    
    assert (!Math.isZero(lineLength));  //assert line has positive length
    v = v / lineLength;   //normalize v
    return origin + v * radius; 
}
lindenrovio
  • 357
  • 3
  • 13
0

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

class Vector {
    constructor(public x = 0, public y = 0) {}
    add = (vector: Vector) => new Vector(this.x + vector.x, this.y + vector.y)
    subtract = (vector: Vector) => new Vector(this.x - vector.x, this.y - vector.y)
    multiply = (vector: Vector) => new Vector(this.x * vector.x, this.y * vector.y)
    multiplyScalar = (scalar: number) => new Vector(this.x * scalar, this.y * scalar)
    divide = (vector: Vector) => new Vector(this.x / vector.x, this.y / vector.y)
    divideScalar = (scalar: number): Vector => new Vector(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) throw new Error('Length must be positive')
        v = v.normalize()
        return o.add(v.multiplyScalar(radius))
    }
}
jorjun
  • 115
  • 1
  • 2
  • 6