Jump to content

  • Log In with Google      Sign In   
  • Create Account

Slope of a line after a rotation. Help.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 GDsnakes   Members   -  Reputation: 145

Like
0Likes
Like

Posted 15 October 2012 - 08:02 AM

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.

Edited by GDsnakes, 15 October 2012 - 08:04 AM.


Sponsor:

#2 clb   Members   -  Reputation: 1789

Like
0Likes
Like

Posted 15 October 2012 - 08:11 AM

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".
Me+PC=clb.demon.fi | C++ Math and Geometry library: MathGeoLib, test it live! | C++ Game Networking: kNet | 2D Bin Packing: RectangleBinPack | Use gcc/clang/emcc from VS: vs-tool | Resume+Portfolio | gfxapi, test it live!

#3 Álvaro   Crossbones+   -  Reputation: 13912

Like
1Likes
Like

Posted 15 October 2012 - 08:39 AM

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).

#4 FLeBlanc   Crossbones+   -  Reputation: 3117

Like
0Likes
Like

Posted 15 October 2012 - 09:48 AM

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.

#5 GDsnakes   Members   -  Reputation: 145

Like
0Likes
Like

Posted 22 October 2012 - 01:22 PM

(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.

#6 FLeBlanc   Crossbones+   -  Reputation: 3117

Like
0Likes
Like

Posted 22 October 2012 - 02:02 PM

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.

#7 Álvaro   Crossbones+   -  Reputation: 13912

Like
0Likes
Like

Posted 22 October 2012 - 03:12 PM

(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.

#8 GDsnakes   Members   -  Reputation: 145

Like
0Likes
Like

Posted 24 October 2012 - 06:35 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS