Advertisement

Simple Math question about angle. How can you find the end coordinates of a rotated line?

Started by August 09, 2015 05:08 AM
7 comments, last by Khatharr 9 years, 4 months ago

Here is the picture.

8LwYs3t.jpg

The line started at (0, 0) and has 40 pixels long, hence it ends at (40, 0). In case I rotate the line 30 degrees, in what coordinates the line will end?

I have tried to search over Google, perhaps I was not using the correct keyword, so that I never find the solution.

The word you look for is "trigonometry" :) A useful page is https://en.wikipedia.org/wiki/Trigonometry and it pretty much shows your problem. You know 'c' (40), and you know A (30 degrees, or 30*2*PI/360 radians). Now you can use 'cos A' to compute the new x, and 'sin A' to compute the new y.
Advertisement
An equivalent point of view is Polar-to-Cartesian coordinate conversion: https://www.mathsisfun.com/polar-cartesian-coordinates.html

Both of the posts above me give you information to find the solution. So don't dismiss them tongue.png. Read the links they provided.

Down Below is the process for obtaining the solutions. A few other things you might want to look at to understand what I did here...

This is a trig circle. This graph will show you some very useful information that can provide shortcuts, as well as some insights when you take a trig class. This graph shows how angles and it's comparable radian format will act when inputed into the functions sin and cos.

When you read it, remember that X is usually seen as Cos, and Y is seen as Sin.
(Cos, Sin)

https://lelandmath.files.wordpress.com/2013/10/screen-shot-2013-10-14-at-7-19-46-pm.png

Let's begin.

With Theta being 30.
And our Axis is (0,0)

Because we start out with our objects reference frame rotation being zero, we can go ahead and remove the y component to get R.

PROOF
sqrt(r2 = 402 + 02) is r=40

So...

r = 40
Theta = 30 deg

So... Polar Co-ordinates (r,theta) are (40, 30degrees) respectively.

This equates too...

x = 40 * COS(30) = 40 * (31/2/2) = 20*31/2 =APROX= 35
y = 40 * SIN(30) = 40/2 = 20

After calculation you have new coordinates
(x,y) = (35,20)

A few notes. Notice that our endpoint is not on y? This is because a rotation conserves the line's original length. What happens in Trig is that you make a circle as you rotate upon a point.

https://www.mathsisfun.com/algebra/trigonometry.html

Keep in mind, if you are doing this for a game... this is going to be horrendously slow. Your best bet would be to use a matrix.

Wow, thanks for the answer. I think I will definitely learn more on trigonometry.

Keep in mind, if you are doing this for a game... this is going to be horrendously slow. Your best bet would be to use a matrix.

This is pretty close to what a rotation matrix does, really. If you're only doing a z-axis rotation of a 2D point you may as well just extract the math.

Rotation of theta degrees around z-axis (this is exactly what a 2D rotation matrix would do, but without the matrix):

x = (x * cos(theta)) - (y * sin(theta))

y = (x * sin(theta)) + (y * cos(theta))

Matrices are handy because they allow multiple transforms to be concatenated into a single transform and then applied to several vertices without needing to repeat any of the work.

For example, if you want to rotate a lot of points by 30 degrees then your matrix is:

[cos(30), sin(30)]

[sin(30), -cos(30)]

Which is

[0.866, 0.5]

[0.5, -0.866]

Then the vector mul works out to:

x = (x * 0.866) + (y * 0.5)

y = (x * 0.5) - (y * 0.866)

For any 2D point/vector.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
Advertisement
Mmph. So it is... so it is. My product of stupidity at 4 am in the morning.


x = (x * cos(theta)) - (y * sin(theta))
y = (x * sin(theta)) + (y * cos(theta))

I'd have done

xnew = (x * cos(theta)) - (y * sin(theta))
ynew = (x * sin(theta)) + (y * cos(theta))

For both of these equations, as the naive coder would accidentally use the same variable names for both the input and output which would produce faulty results.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!


x = (x * cos(theta)) - (y * sin(theta))
y = (x * sin(theta)) + (y * cos(theta))

I'd have done

xnew = (x * cos(theta)) - (y * sin(theta))
ynew = (x * sin(theta)) + (y * cos(theta))

For both of these equations, as the naive coder would accidentally use the same variable names for both the input and output which would produce faulty results.

I got the negative sign in the wrong row of the matrix too. rolleyes.gif

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

This topic is closed to new replies.

Advertisement