There's a 3 point bezier curve. Trying to find a specific angle, I end up with its opposite angle at times. When & when not to add 180° to correct it?

112 Views Asked by At

I am trying to implement a web app to draw directed graphs. I am using bezier curve for the edges. I need to place an arrow at some value $t =$ say $t_1$. The arrow should be at an angle to match the curve. The objective is to find that angle.

Let the three points be $p_1=(x_1,x_2)$ (starting pt), $p_c=(x_c,y_c)$ (control pt), $p_2=(x_2,y_2)$ (ending pt). The equation of the bezier curve is $(1-t)^2*p1 + 2*(1-t)*t*p_c + t^2*p_2$. I tried to find the angle by $arctan($slope of tangent of the curve at $t_1)$. So, I calculated $ m =\frac{dy}{dx} = \frac{-\,2\,(1-t)\,y_1 \,\; + \,\; 2\,(1-t)\,y_c \,\; - \,\; 2\,t\,y_c \,\; + \,\; 2\,t\,y_2}{-\,2\,(1-t)\,x_1 \,\; + \,\; 2\,(1-t)\,x_c \,\; - \,\; 2\,t\,x_c \,\; + \,\; 2\,t\,x_2}$ at $t=t_1$ and $angle\,=\,arctan(m)$.

But this sometimes gives the angle that is exactly the opposite $(angle+180)$ and I can't figure out when/why it happens. Check https://www.desmos.com/calculator/i2xs0nhwqy and try moving one node around the other. I need the slope of the tangent be towards the point labeled as $node\;2$ all the time or some other way of finding the required angle.

Thanks in advance.