How to know if a point in a circle has crossed a plane passing through the center point?

106 Views Asked by At

I am creating a control in .NET which computes polar coordinates based on $(x,y)$- coordinates within a panel control. Here is an image to use as a reference: enter image description here

When the mouse moves over the circle, the mouse position is used to compute the polar coordinates based on the North and center point references.

enter image description here

To get the polar coordinates of point $(x_1, y_1)$ in example A, I use the Law of Cosines to find the angle:

$$ c^2 = a^2 + b^2 - 2ab \cos(c)$$

And the Pythagorean theorem to get the distance between the 3 points. For example with the Center point: $$ \sqrt{CenterX - x_1)^2 + (CenterY - y_1)^2}$$

Since I am moving clockwise around the circle and I want the degrees range to be $0 - 360$, once the mouse coordinate moves into the left hemisphere of the circle, the degrees begin to decrease back to $0$. To compensate for this, I use this:

if (x1 < NorthX)
    degrees = 360 - degrees;

This works well as long as the plane from North through the Center point is parallel to $X$ axis. But what if the North point is defined as in Example B? And I'm trying to find if $(x_2, y_2)$ is in the "back half" of the circle?

I am looking for the more complex algorithm / formula that will tell if the mouse location has crossed the plane from North through the Center regardless of where the North point is.

Thanks in advance!

1

There are 1 best solutions below

0
On

Solved! Thanks to @SrinivasK for the suggestion of using the [Math.Atan2][1] function. I realized that I could use it to get the tangent angle of North then compare that against the tangent angle of the mouse point to see if it was within the first 180 degrees. Here is the C# code I used, which should be simple enough to use as general psuedo-code:

//If in the 'left hemisphere', subtract from 360
//1) Get the point offset from center to north
PointF offsetN = new PointF(north.X - center.X, north.Y - center.Y);

//2) Get the Atan2 of the north point in degrees
double northTan = Math.Atan2(offsetN.Y, offsetN.X).ToDegrees();

//3) Get the offset of the location point
PointF offsetL = new PointF(location.X - center.X, location.Y - center.Y);

//4) Get the tangent of the location point in degrees
double locationTan = Math.Atan2(offsetL.Y, offsetL.X).ToDegrees();

//5) Conditionally update the value 
double northTanBound = northTan + 180;

if (locationTan < northTan || locationTan > northTanBound)
    C = 360.0 - C;