You don't actually need trigs for this one. Simply use slopes, or change in x
and y
.
Given a line of slope m = y/x
, the line perpendicular to that line has slope -1/m
, or -x/y
.
The slope m between the red dots is -150/150
, or -1/1
. I noticed your positive y
points down.
Therefore, the positive slope is 1/1
. Both of your x and y changes at the same speed, with the same amount.
Once you know that, then it should be pretty easy to figure out the rest. Since they're aligned at 45 degrees angle, the edge ratio of the 45-45-90
triangle is 1 : 1 : sqrt(2)
. So if your length is 20
, the individual x and y change would be 20/sqrt(2)
, or roughly 14
in integers.
So, your two yellow dots would be at (36, 236)
, and (64, 264)
. If the lines are not aligned to a convenient degree, you would have to use arctan()
or something similar, and get the angle between the line and the horizontal line, so you can figure out the ratio of x and y change.
I hope my answer wasn't too hard to follow. For a more general solution, see Troubadour's answer.
Edit: Since the OP said the lower red dot is actually rotating around the upper red dot, we will need a more flexible solution instead.
I'm going to extend this answer from Troubadour's, since I'm doing exactly the same thing. Please refer to his post as you read mine.
1.
Get the vector from origin (200, 100) to rotating point (50, 250):
vector = (200 - 50, 100 - 250) = (150, -150)
2.
Rotate your vector by swapping the x and y, and negate x to get the new vector:
vector = (150, -150) => swap => (-150, 150) => negate x => (150, 150)
3.
Get the unit vector (of length 1) from the new vector:
vector = vector / length(vector)
= (150 / length(vector), 150 / length(vector))
~= (0.7071, 0.7071)
where
length(vector) = sqrt(150^2 + 150^2) ~= 212.2320
4.
Get the displacement vector of length 20, by multiplying the unit vector.
displacement_vector = vector * 20
= (0.7071 * 20, 0.7071 * 20)
= (14.1421, 14.1421)
5.
Add/Subtract this vector to/from your rotating vector (point):
yellow_1 = (50, 250) + (14.1421, 14.1421) ~= (64, 254)
yellow_2 = (50, 250) - (14.1421, 14.1421) ~= (36, 236)
I hope the above steps help you with formulating your code. Doesn't matter what the angle it's at, same steps.