MaldAtCoding
MaldAtCoding

Reputation: 27

Get the angle and the distance of point from center point

I'm trying to make a color picker and need some help, I need to get an angle of a point from a center point, starting from the top moving to the left, highest value being 1 and the lowest 0, as well as distance between the two points;

I skipped math in high school so I'm at a loss, any help would be appreciated

enter image description here

Upvotes: 0

Views: 364

Answers (1)

Metoděj Svoboda
Metoděj Svoboda

Reputation: 26

to find angle between center and the point in radians:

Math.Atan2(point.y-center.y,point.x-center.x) 

normalize it:

Math.Atan2(point.y-center.y,point.x-center.x)/Math.PI/2

make it start from top:

Math.Atan2(point.y-center.y,point.x-center.x)/Math.PI/2+0.25

don't let it go below zero:

(Math.Atan2(point.y-center.y,point.x-center.x)/Math.PI/2+0.25+1)%1

invert it so it goes counterclockwise:

1-(Math.Atan2(point.y-center.y,point.x-center.x)/Math.PI/2+0.25+1)%1

you can rewrite it as:

1-(Math.Atan2(point.y-center.y,point.x-center.x)/2/Math.PI+1.25)%1

Upvotes: 1

Related Questions