Slope of a line after a rotation. Help.

Started by
6 comments, last by MylesEvans 11 years, 5 months ago
I made a similar thread but I worded it weird and didnt really get the answer I was looking for. So here is a better worded question that I hope someone can help me with solving.


Given a line with slope m, if the line is rotated 90? clockwise the slope of the resulting figure is ? 1/m, how can I determine the slope if the line is rotated 30? or 60? clockwise. What if its rotated 210 degrees, or 350?

I need to come up with a formula that will factor in the original slope, take/apply the amount I want to rotate, and give me the new slope.
Advertisement
If a line has slope m, then it advances m units in the y direction for each 1 unit advanced in the x direction. Therefore we can represent the direction of the line using the 2D vector v=(1, m).

When we rotate the line by an angle d, we apply a 2D rotation matrix M(d) = [ (cos(d), -sin(d)) (sin(d), cos(d)) ] to the line direction vector. This means that the rotated line has the direction vector v' = M(d)*v = ( cos(d) - m*sin(d), sin(d) + m*cos(d) ).

The slope of the rotated line with the direction vector v' is then ( sin(d) + m*cos(d) ) / ( cos(d) - m*sin(d) ). Be sure to handle the special case when cos(d) - m*sin(d) = 0, i.e. when the line is vertical and the slope is essentially "infinite".
I also get (m*cos(d)+sin(d))/(cos(d)-m*sin(d)) .

May I suggest using a better representation for a direction than the slope of the line? My favorite is a unit-length vector (which can be interpreted as a complex number to make rotations easier).
I second the idea of ditching slopes.

Slope is calculated as rise over run. m=y/x. However, if x==0, then you have a problem. In vector form, you can represent a vertical line as (x=0, y=1) whereas in slope form you would have to represent it as (1/0) which is undefined.

If you really need to know the actual slope of a line (for whatever reason) you can perform the division after the fact, just be prepared to deal with the edge cases.
(m*cos(d)+sin(d))/(cos(d)-m*sin(d))

What is this? Does this (m*cos(d)+sin(d)) give me the rise and this (cos(d)-m*sin(d)) the run?

I don't want this in vector form.

My code goes something like this. Object A is always wherever the base object is, but moved Y units up (rise) and X units over (run).
Now when I want to rotate the "slope" (Object A's position relative to the base Object) so the slope is not going to be constant. Which is why I made this thread asking for a formula to represent the change in the slope. So now my code needs to work like this.

Object A is draw wherever the base object is drawn but up *Y units determined by formula that accounts for rotation* and over *X units determined by formula that accounts for rotation* So I basically need to know how rotation affects the rise and the run, but not in vector form.
You really, really don't want to work directly with slope if you are allowing rotation. You want to work with vector form, because eventually your slope is likely to become undefined, if the "run" is ever equal to 0. Working in vector form essentially means that instead of keeping track of "slope", you keep track of "rise" and "run" instead. It's just that if object A is directly above the base object, if you are working with vector form then things won't crash and burn, but if you are working with slope you will suddenly end up with a divide-by-zero exception.

Just remember that in vector form, the x component equals "run" and the y component="rise".

That being said, the general form for 2D rotation about a point is:

x' = x*cos(A) - y*sin(A)
y' = y*cos(A) + x*sin(A)

Where A is the angle by which to rotate.

(m*cos(d)+sin(d))/(cos(d)-m*sin(d))

What is this?


That's exactly what you asked for. It is the slope after rotation by an angle d of a line that used to have slope m.

You really, really don't want to work directly with slope if you are allowing rotation. You want to work with vector form, because eventually your slope is likely to become undefined, if the "run" is ever equal to 0. Working in vector form essentially means that instead of keeping track of "slope", you keep track of "rise" and "run" instead. It's just that if object A is directly above the base object, if you are working with vector form then things won't crash and burn, but if you are working with slope you will suddenly end up with a divide-by-zero exception.

Just remember that in vector form, the x component equals "run" and the y component="rise".

That being said, the general form for 2D rotation about a point is:

x' = x*cos(A) - y*sin(A)
y' = y*cos(A) + x*sin(A)

Where A is the angle by which to rotate.


Thank you very much. This is exactly what I was looking for. Also don't worry about the run equaling zero because my code is written in a way where that wont be a problem. There will be no 0 in any denominators.

This topic is closed to new replies.

Advertisement