1
votes

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. enter image description here

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:

  1. get YOffset from center point of the arc
  2. flip YOffset (to match screen y and math)
  3. get corresponding x value with Pythagoras (x = Math.sqrt(r^2 - YOffset^2))
  4. 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.

enter image description here

I'm looking for a general solution so it will work for any arc regardless of its start angle.

1
It is not clear - what data you have to build an arc?MBo
If you are using d3.arc that means you have the radius, start and end angles. Wouldn't the arc length simply be r * abs(end-start)?Mark
@Mark I think he has path.arc, not d3.arc.ccprog
@ccprog, ahh ok, but that takes the same arguments path.arc(x, y, radius, startAngle, endAngle[, anticlockwise])...Mark
it is best to draw and name the angles in the figure you use and calculate. A reference to θ or φ is better than "central angle"rioV8

1 Answers

0
votes

To me it seems this is not much of a programming problem, but of math. What I understand is: given a start point (x₁, y₁), an end pont(x₂, y₂) and a radius r, what is the angle α? enter image description here

You can compute the distance d between the two points. Half its length is the far leg of a right triangle with r as its hypothenuse. d/2r is the sinus of the near angle; double that and you have the angle between the end points.

(And, having α expressed in radians, the path length is simply α * r.)