Okay here's the math that will be involved to give you the location of your desired point.
Diagram for reference:
To find the Green
point, all we need to do is find the find the height and the width of the imaginary rectangle (see image), which are labeled as Adj
and Opp
respectively, and add those values to the Center
point.
If you remember SOH CAH TOA
, we'll leverage TOA
:
Tan(Theta) = Opposite / Adjacent
which can be re-arranged to:
Opposite = Tan(Theta) * Adjacent
So we'll need to first find Adjacent (Adj)
and Theta
.
Consulting the diagram, Adj
is obviously the difference in y
between Orange
and Center
:
Adj = Orange.y - Center.y
But we don't know Orange
? Well, Orange
would be equal to Center
plus half the height of the rectangle:
Orange = (Center.x, Center.y + Rect.Height/2)
Subbing the value of Orange
back into our Adj
calculation:
Adj = Center.y + Rect.Height/2 - Center.y
Adj = Rect.Height/2
Next we need to find Theta
.
Rerfering back to SOH CAH TOA
CAH
:
Cos(Theta) = Adjacent / Hypotenuse
Theta = acos(Adjacent / Hypotenuse) //Inverse cosine
We already know that Adj = Rect.Height/2
from earlier, so we just need to find Hyp
. To find Hyp
we can use the distance formula which says:
d = sqrt((x2 - x1)^2 + (y2 - y1)^2)
In our case:
Hyp = sqrt((Center.x - Mouse.x)^2 + (Center.y - Mouse.y)^2)
So now we have everything we need for Theta
:
Theta = acos(Adj / Hyp) //Inverse cosine
Finally, we have what we need for Opp
:
Opp = Tan(Theta) * Adjacent
Which means we have everything we need to answer your question:
Green = (Center.x + Opp, Center.y + Adj)
Notes:
This is the "on paper math" using degrees. A lot of programming languages deal with radians instead of degrees, so you may have to do some converting.
As @Jerry Andrews pointed out, there will be four different cases to this problem, which correspond to the four different sides of the rectangle that your drawing point could fall on:
To determine the quadrant, he first needs the angle between the center of the rectangle and any corner--thus, atan(height/width). This will give the half-angle (center-to-green, if green is on a corner). Then atan(mouse.y/mouse.x) will provide the angle of the line from the rectangle center to the mouse cursor (because in his code, the mouse is located relative to the center of the rectangle).