How to calculate Arc length dependently of start angle?

匆匆过客 提交于 2019-12-08 07:02:51

问题


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:

  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.

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


回答1:


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 α?

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.)



来源:https://stackoverflow.com/questions/51908730/how-to-calculate-arc-length-dependently-of-start-angle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!