As I calculate the length of the arc (green) between two angles;

enter image description here I need a method like:

arc_lenght (xpos, ypos, radius, ang1, ang2) { return arc_lenght }

  • \$\begingroup\$ I'm not quite sure what you mean by 'arc length between two angles'. Generally, you can easily find the arc length from the radius and some central angle. Also what does xpos and ypos represent? Are they the center of the circle? Cause that doesn't seem relevant to the problem. \$\endgroup\$ – prithul Mar 15 '20 at 6:26
  • \$\begingroup\$ If you have two angles, there should be two arcs on the circle that join them. Which one do you want? My guess is clockwise from the first angle to the second, is that correct? Well, they should sum up to the circumference, so if you can get one, you can get the other. \$\endgroup\$ – Theraot Mar 15 '20 at 11:29

The arc-length, l, of a circle is equal to the angle of the arc, θ, in radians times the radius.

If we assume that ang1 is in the range [0 - ang2] and ang2 is in the range [ang1 - 360] (so ang1 is always the smaller of the two and neither is bigger than 360) then:

float angle = ang1 - ang2;
float rad = angle * PI / 180.0;
float radOutside = (2 * PI) - rad;

float arc_length = radOutside * radius;

I am not sure what xpos and ypos represents, but I don't think they're relevant for getting the arc length.

  • \$\begingroup\$ Don't forget to wrap your angle difference. If I give you the jnputs -90 and 180, the largest arc length between them is 1.5π•radius, but this code will give the answer 3.5π•radius — including an extra full rotation. \$\endgroup\$ – DMGregory Mar 15 '20 at 13:37
  • 1
    \$\begingroup\$ @DMGregory That's why I made an assumption on the range of ang1 and ang2. \$\endgroup\$ – bornander Mar 15 '20 at 13:56
  • 1
    \$\begingroup\$ You could make this quite a bit shorter by assuming input in radians, too. \$\endgroup\$ – Weckar E. Aug 12 '20 at 14:04
  • \$\begingroup\$ @WeckarE. agreed, I used degrees as input because OP's example was in degrees. \$\endgroup\$ – bornander Aug 12 '20 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.