Point translation orthogonally to line

I am currently working on a thick polylines drawing project and I am using interpolation in OpenGL. I was able to calculate all the points I needed, but I need to draw two more points. I need to translate one point orthogonally to the line connecting the two points. The screenshot below shows which points. The L point must be translated for the distance between L and nJ orthogonal to the AB line (B is the center point). A similar situation is associated with the transfer to nK.

enter image description here

I wrote the code:

float alpha = atan2(B.y - A.y,B.x - A.x) - deg90;
float alpha2 = atan2(C.y - B.y, C.x - B.x) - deg90;


nJ.x = L.x + w*cos(alpha); // w is distance between A1 and A2
nJ.y = L.y + w*sin(alpha);
nK.x = L.x + w*cos(alpha2);
nK.y = L.y + w*sin(alpha2);

      

The code only works for some points, not all. I need to fix + sing in nJ and nK calculations, but I don't know how. Who has an offer?

+3


source to share


1 answer


First you need the left function:

lhs(v) = [-v.y, v.x]

      

This rotates the vector 90 degrees counterclockwise.

Now you need a rotation function:

turn(u, v, w) = sign(lhs(v - u), w - v)

      

If you have a polyline from u

to v

to w

, turn(u,v,w)

tells you whether it will rotate left (counterclockwise) (positive), rotate right (rotate clockwise) (negative), or colinear (0).

There are four infinite lines in your image that are parallel ab

and bc

, with a distance w

between each pair.

Lines at the bottom:

f(s) = (a + 0.5 * w * normalize(lhs(b - a))) + (b - a) * s
g(t) = (b + 0.5 * w * normalize(lhs(c - b))) + (c - b) * t

      



You want to find the intersection of two lines; that is, you want to decide for s

and t

in f(s) = g(t)

. This is just a system of two linear equations with two unknowns.

The solution is your point L = f(s) = g(t)

in the picture.

To calculate I

, you can use the same idea:

f(s) = (a - 0.5 * w * normalize(lhs(b - a))) + (b - a) * s
g(t) = (b - 0.5 * w * normalize(lhs(c - b))) + (c - b) * t

      

Decide for I = f(s) = g(t)

.

Update

Once you have it L

, you can calculate Kn

and Jn

as follows.

Kn = L - w * normalize(lhs(b - a))
Jn = L - w * normalize(lhs(c - b))

      

In computational geometry code, trigonometry is usually a code smell - it's not always wrong, but it's usually wrong. Try to stick with linear algebra.

+1


source







All Articles