-1

I need to offset a curve, which by the simplest way is just shifting the points perpendicularly. I can access each point to calculate angle of each line along given path, for now I use atan2. Then I take those two angle and make average of it. It returns the shortest angle, not what I need in this case.

  • How can I calculate angle of each connection? Concerning that I am not interested in the shortest angle but the one that would create parallel offset curve.

enter image description here

Jansindl3r
  • 360
  • 3
  • 18

1 Answers1

1

Assuming 2D case...

So do a cross product of direction vectors of 2 neighboring lines the sign of z coordinate of the result will tell you if the lines are CW/CCW

So if you got 3 consequent control points on the polyline: p0,p1,p2 then:

d1 = p1-p0
d2 = p2-p1

if you use some 3D vector math then convert them to 3D by setting:

d1.z=0;
d2.z=0;

now compute 3D cross:

n = cross(d1,d2)

which returns vector perpendicular to both vectors of size equals to the area of quad (parallelogram) constructed with d1,d2 as base vectors. The direction (from the 2 possible) is determined by the winding rule of the p0,p1,p2 so inspecting z of the result is enough.

The n.x,n.y are not needed so you can compute directly without doing full cross product:

n.z=(d1.x*d2.y)-(d1.y*d2.x)
if (n.z>0) case1
if (n.z<0) case2

if the case1 is CW or CCW depends on your coordinate system properties (left/right handness). This approach is very commonly used in CG fur back face culling of polygons ...

if n.z is zero it means that your vectors/lines are either parallel or at lest one of them is zero.

I think these might interest you:

Also in 2D you do not need atan2 to get perpendicular vector... You can do instead this:

u = (x,y)
v = (-y,x)
w = (x,-y)

so u is any 2D vector and v,w are the 2 possible perpendicular vectors to u in 2D. they are the result of:

cross((x,y,0),(0,0,1))
cross((0,0,1),(x,y,0))
Spektre
  • 49,595
  • 11
  • 110
  • 380
  • Thanks a lot! I don't know how but I manage to do it following your instructions. finding out whether angle is CW or CWW was very handful enough. I couldn't really understand what follows after the links. Can you explain it, please? What is u, v, w and what are x, y.. x, y of which point? – Jansindl3r Nov 20 '19 at 23:01
  • @Jansindl3r `u` is any 2D vector (direction) defined by `x,y` ... `v,w` are 2 possible perpendicular vectors to `u` one is `u` rotated in `CW` direction and the other in `CCW` ... so to compute perpendicular vector in 2D you just swap `x,y` and negate one of them no need to do `atan2` and then `cos,sin` ... – Spektre Nov 20 '19 at 23:16