Finding the distance between two points on a circle

215 Views Asked by At

The question I am posing is coming from a programming issue I am having but the concept is strictly math related. I am working on a hue circle that is fully 360 deg. I have two points: 1). blue @ 11deg, 2.) orange @ 216deg. What I am trying to do is basically changing the hues of a pixel based on proximity of the pixels hue. For example: pixel x.hue = 135deg. Since the hue is closer 216, I would change the hue to 216. My only issue is the case of a "wrap around". For example, if I had a hue point 345deg, I would color the pixel blue, however from a mathematical standpoint, how would I take care of finding the distance for any hue point on a circle between 11deg and 216deg since I cannot use the arc length formula due to not having a radius.

Visual Representation enter image description here

1

There are 1 best solutions below

1
On BEST ANSWER

Your question is slightly difficult to understand but it seems like you want the shorter of the two differences between two angles considering wraparounds.

If the absolute difference between two angles is greater than $180^\circ$ then you subtract from $360^\circ$ the absolute difference. For example $x=30^\circ$, $y=300^\circ$ has a distance of $270^\circ$ going the "right way" around the circle, but a distance of $90^\circ$ going the "wrong way", which is $360-|300-30|=90^\circ$. In computer code terms the absolute value is given by something like math.Abs(), so you want 360-math.Abs(y-x).

In full this would be something like

    diff := math.Abs(a-b)
    if diff > 180 {
         diff = 360 - diff
    }