FitzFish
FitzFish

Reputation: 8629

Find a vector tangent to a circle

I need to move a point by vectors of fixed norm around a central circle. So to do this, I need to calculate the circle tangent vector to apply to my point.

Here is a descriptive graph : enter image description here

So I know p1 coordinates, circle radius and center, and the vector norm d. I need to find p2 (= finding the vector v orientation).

I put on the graph some ideas I got to find it : p1' is p1 projected on the circle. And t is the tangent vector to C in p1'.

That should be easy, but I'm too weak in maths to figure how to implement this. So I would like an idea of the implementation this (language agnostic is okay, javascript is cool).

Extra cool if you can also get me how to implement clockwise and counter clockwise movement with this.

Edit : Obtained this

let vx = cx - p1x,
    vy = cy - p1y,
    norm = Math.sqrt((vx * vx) + (vy * vy)),
    p2x = p1x - (vy * d / norm),
    p2y = p1y + (vx * d / norm);

But there is still a quirk : using this on time, the point is slowly getting away from the center of the circle, performing a spiral.

Upvotes: 2

Views: 10913

Answers (1)

Ripi2
Ripi2

Reputation: 7198

  1. Obtain vector Center of circle - point P1. Let's call this vector v1.

  2. Tangent vector 't' is perpendicular to v1. If v1=(vx, vy) then t=(-vy,vx) . Just swap values and a sign (I wrote -vy, it could also be -vx, but not both -vy,-vx).

  3. Setting one direction or the order is just using t2= -t1= (vy, -vx), or (-vy, vx)

For movements you must use normalized (||v|| = 1) vectors.

Upvotes: 4

Related Questions