Reputation: 439
Although there is plenty of tutorials on Internet about it, I can't find the proper solution! I'm drawing a line p1=(0,0); p2=(0,j)
and then I do a translation(h,k)
and a rotation(a)
. What is the new coordinate of p2
?
Here is the general formula I came with after looking at transformation matrix however it doesn't seems to work:
x' = (x*cos(a)) + (y*-sin(a)) + h
y' = (x*sin(a)) + (y*cos(a)) + k
So my p2
:
x' = (j*-sin(ofDegToRad(a))) + h
y' = (j*cos(ofDegToRad(a))) + k
What am I doing wrong?
EDIT:
Uploaded the code with answer below, however it is still not working (?). I've put an image with the small sample of code Here
When I print the value it says x = -141.5, y = 254.9
I would like to get the end point of my line (if start point is in the middle of the window)
Upvotes: 0
Views: 1333
Reputation: 58284
The equations you show are doing the rotation first then the translate, which is opposite of the order that you said. If you do the translate first, then:
x' = x + h
y' = y + k
So your p1' is (h, k) and p2' is (h, j+k). Then the rotation is:
x' = (x*cos(a)) - (y*sin(a))
y' = (x*sin(a)) + (y*cos(a))
Which means your p1'' is
( (h*cos(a)) - (k*sin(a)), (h*sin(a)) + (k*cos(a)) )
And your p2'' is
( (h*cos(a)) - (j+k)*sin(a)), (h*sin(a)) + ((j+k)*cos(a)) ).
These all assume that you start with an understood origin at (0,0).
If, instead, we do the rotation first, then we get:
p1' = ( 0, 0 ) [when you rotate (0,0) you get (0,0)]
p2' = ( -j*sin(a), j*cos(a) )
Then do the translation by (h, k):
p1' = ( h, k )
p2' = ( h - j*sin(a), k + cos(a) )
Upvotes: 3