# Thread: Can someone help me to math?

1. ## Can someone help me to math?

So I've taken 2 Trigonometry classes, but it was a long time ago. I've been pouring over my old book for a week now, and although a lot has come back, I still don't have a complete grasp of what's happening here. I can't remember doing anything like this in class...

This is code from "Programming Windows", where we are rotating points around a circle (like a unit circle, with a four quadrant graph):

Code:
```for( i = 0; i < iNum; i++ )
{
ptTemp.x = (int)( pt[i].x * cos( TWOPI * iAngle / 360 ) + pt[i].y * sin( TWOPI * iAngle / 360 ) );
ptTemp.y = (int)( pt[i].y * cos( TWOPI * iAngle / 360 ) - pt[i].x * sin( TWOPI * iAngle / 360 ) );
pt[i] = ptTemp;
}```
I was hoping someone could help me identify the theorems (or principals or whatever) being used? Or if there is a name for what's happening? It would help me in researching it.

Thanks! 2. Looks like polar coordinates being mapped to a Cartesian plane, like your screen. Everything is defined in columns and rows so needing to move theta degrees clockwise or whatever is unhelpful.

x = cos( theta ) r
y = sin( theta ) r

Polar and Cartesian Coordinates 3. Also note that the "TWOPI * iAngle / 360" stuff is meant to convert the angle ("iAngle") from degrees to radians (trig functions work on radians).

Radian - Wikipedia, the free encyclopedia 4. Originally Posted by whiteflags Looks like polar coordinates being mapped to a Cartesian plane, like your screen. Everything is defined in columns and rows so needing to move theta degrees clockwise or whatever is unhelpful.

x = cos( theta ) r
y = sin( theta ) r

Polar and Cartesian Coordinates
Well like I understand what's happening to some extent, I can't understand why. Like if I'm at (1, 1) in a unit circle, and I want to move to (-1, -1) why can't I just multiply ( x * -1 ), where -1 is the cos() of the new point (and multiply y by the new sin)? Why does he add and subtract the extra fields (in bold)?

Code:
```ptTemp.x = (int)( pt[i].x * cos( TWOPI * iAngle / 360 ) + pt[i].y * sin( TWOPI * iAngle / 360 ) );

ptTemp.y = (int)( pt[i].y * cos( TWOPI * iAngle / 360 ) - pt[i].x * sin( TWOPI * iAngle / 360 ) );``` 5. It's nowhere near as neat as the unit circle.

You have to make a convention. Say for example that the origin of the screen is the topright corner. Now give every pixel an address. You immediately have a problem because there are no real number addresses, so neat trig you can do to rotate a point kinda goes out the window.

I say kinda because you can still use polar to Cartesian coordinate conversion to do it good enough. What happens is the x part of the old coordinate will move to the left and the y will move down some number of pixels.

x + x` to go left
y - y` to go down

The trig function will estimate the angle and let you go right or up if you need to by making the operand it is a part of negative.

It's no mathematical proof but I hope you understand the nuance here. 6. I think it is rotating the point about the origin.

Here's the stock formula for a rotation of a point (x, y) about the origin producing a new point (x', y'):

x' = x cos f - y sin f
y' = y cos f + x sin f

where f is an angle measured in radians. Positive f corresponds to a counter-clockwise rotation, and negative f clockwise.

If you wanted to reverse clockwise/counter-clockwise, you could negate the angle f. Because cos is an even function (that is, cos(x) = cos(-x)) and sin is an odd function (sin(-x) = -sin(x)), you end up with the code you posted.

You could achieve the same result by simply negating iAngle.

I'm pretty sure this is correct... 7. Originally Posted by Alpo Like if I'm at (1, 1) in a unit circle
If you're talking Cartesian coordinates (the x/y plane), the point (1, 1) is not on the unit circle. The equation for a circle is r^2 = x^2 + y^2, where r is the radius of the circle and (x, y) is a point on the circle. But the equation 1^2 = 1^2 + 1^2 has no (real) solution, so (1, 1) cannot be on the unit circle.

On the other hand, if you're talking Polar coordinates, then all points of the form (a, 1) are on the unit circle, where a is any angle. 8. Originally Posted by MacNilly I think it is rotating the point about the origin.

Here's the stock formula for a rotation of a point (x, y) about the origin producing a new point (x', y'):

x' = x cos f - y sin f
y' = y cos f + x sin f

where f is an angle measured in radians. Positive f corresponds to a counter-clockwise rotation, and negative f clockwise.

If you wanted to reverse clockwise/counter-clockwise, you could negate the angle f. Because cos is an even function (that is, cos(x) = cos(-x)) and sin is an odd function (sin(-x) = -sin(x)), you end up with the code you posted.

You could achieve the same result by simply negating iAngle.

I'm pretty sure this is correct...
Yes that looks right, and you're right about the origin. I actually found an explanation of this in a book since I posted, I'm reading it linearly though so I won't get there for a while.

Oh yeah and on the coordinate thing, I was a little confused. I thought because you have a cos and sin of 1 at (1,0) (0,1), that to get a 45 degree triangle you would just say (1,1). It's actually cos and sin of .707 since I was going for an Isosceles.

Thanks for the info! 9. It's a matrix multiplication using a rotation matrix.

Rotation matrix - Wikipedia, the free encyclopedia

- 10. Normally you would want to keep the matrix elements in the correct order:

cos(a) -sin(a)
sin(a) cos(a)

So when you multiply it by the coordinate pair, which is also a matrix,
the elements get paired up correctly:

x' = x cos(a) - y sin(a)
y' = x sin(a) + y cos(a)

To reverse direction:

x' = x cos(a) + y sin(a)
y' = - x sin(a) + y cos(a)

In the calculation for y in the program above, the terms were rearranged,
to eliminate an operation, I imagine (the negation of the x term), or maybe
just to make it a little neater:

x' = x cos(a) + y sin(a)
y' = y cos(a) - x sin(a)

Also, the first matrix just above rotates CCW for a standard coordinate system
(positive y is north or up).

The second matrix, used in the program would rotate CW for a standard system.
For default Windows screen coordinates, it rotates CCW.

- 11. Originally Posted by megafiddle It's a matrix multiplication using a rotation matrix.

Rotation matrix - Wikipedia, the free encyclopedia

-

Just wanted to say thanks for this, it took a while to get through. I only understood about half (it turned into French about half way down ), but I feel like I have a much better grasp of what's going on. Or at least I know what is affecting what, and how. Hopefully I'll finish my trig book in a few weeks and find out what proofs are at work better.

Then I'll need to go hunting for other cool math stuff to use with graphics. I was thinking of trying to learn more about Bezier splines. Does anyone have any recommendations of other useful math things to know? 12. There's a lot of math behind 3D graphics; it all boils down to linear algebra. If you know matrix transformations, trigonometry, geometry and basic algebra, that's a considerable amount of work and is quite enough for most graphics applications. 13. Originally Posted by MacNilly There's a lot of math behind 3D graphics; it all boils down to linear algebra. If you know matrix transformations, trigonometry, geometry and basic algebra, that's a considerable amount of work and is quite enough for most graphics applications.
Oh yeah I'm sort of beginning to see the importance of the matrix manipulations now. It seems like for every new thing I'm learning there are a number of implications for programming that I haven't stumbled on yet. If I'm ever a math teacher I'm going to get my students computers, that way they could see the importance of some of this stuff. 14. Originally Posted by Alpo Just wanted to say thanks for this, it took a while to get through. I only understood about half (it turned into French about half way down )...
Hope it helped
You got further than I did I wish I had time to work out the derivations for all the stuff I run into
that I need to use. I know I did it once, but trig class was quite some
time ago. Originally Posted by Alpo Hopefully I'll finish my trig book in a few weeks and find out what proofs are at work better.
I believe you might derive the equations like this:

Take the original coordinates and replace them with the equations
for converting cartesian to polar. Add the rotiation angle to the
angukar coordinate. Then replace those with the equations for
converting polar to cartesian. Then simplify the whole mess.

I hope someone will verify all that before you actuall try it.

Martrices are a form of notation (although there are more than that).
They are a very convenient way of handling equations with a large
number of cross multiplied terms. You will see them a lot, especially
in 3D graphics. Originally Posted by Alpo Then I'll need to go hunting for other cool math stuff to use with graphics. I was thinking of trying to learn more about Bezier splines. Does anyone have any recommendations of other useful math things to know?
What type of graphics - 2D or 3D?

If 3D, look into dot products and normals.
Normals are vectors oriented at right angles to a plane surface. It can
be used to determine the direction that a surface is facing. Often used
to calculate illumination values for the surface of a 3D object.
Dot products are useful for determining the angle between two 3D vectors. Originally Posted by Alpo If I'm ever a math teacher I'm going to get my students computers, that way they could see the importance of some of this stuff.
That would be so excellent. I would have remembered a lot more if we
had them; hard to visualize things on a slide rule, and we rarely had any
practical applications in math class.
Fortunately, I remember what I need to look for when I need something.
The Interwebs do the rest.

Now you will need a language that non-programmers can use. Be careful
with that - that's how BASIC got started.

- 15. Originally Posted by Alpo If I'm ever a math teacher I'm going to get my students computers, that way they could see the importance of some of this stuff.
And remember: no math proofs. Only intuitive proofs. If you provide graphical representation of the proofs, then even better. Many people learn better if they can see something graphically. Popular pages Recent additions 