I am building a road editor where one can drag predefined road segments together and combine them to a connected road. One of those segments is a right curve.
The curve is drawn as SVG (using D3 arc path function) and has two handles to change the radius and the length directly within the SVG (small black circle changes length and small black square changes the radius). I use a d3 drag handler on the handles.
To calculate the new central angle I do as follows:
- get YOffset from center point of the arc
- flip YOffset (to match screen y and math)
- get corresponding x value with Pythagoras (x = Math.sqrt(r^2 - YOffset^2))
- get the respective central angle (Math.PI / 2 - Math.atan2(YOffset, x);
This will only work if the arc starts at PI/2 like in the figure. If I add further arcs with arbitrary start angles (see next figure, red arc), my solution wont work.
I'm looking for a general solution so it will work for any arc regardless of its start angle.
d3.arc
that means you have the radius, start and end angles. Wouldn't the arc length simply ber * abs(end-start)
? – Markpath.arc(x, y, radius, startAngle, endAngle[, anticlockwise])
... – Mark