Thread: Can someone help me to math?

  1. #1
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877

    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!
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,608
    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. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    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. #4
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by whiteflags View Post
    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 ) );
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,608
    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.
    Last edited by whiteflags; 09-12-2014 at 11:13 PM.

  6. #6
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    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. #7
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Quote Originally Posted by Alpo View Post
    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. #8
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by MacNilly View Post
    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!
    Last edited by Alpo; 09-15-2014 at 08:04 PM.
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    It's a matrix multiplication using a rotation matrix.
    More about it here:

    Rotation matrix - Wikipedia, the free encyclopedia

    -

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    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.

    -
    Last edited by megafiddle; 09-16-2014 at 01:57 AM.

  11. #11
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by megafiddle View Post
    It's a matrix multiplication using a rotation matrix.
    More about it here:

    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?
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  12. #12
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    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. #13
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by MacNilly View Post
    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.
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  14. #14
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Quote Originally Posted by Alpo View Post
    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.

    Quote Originally Posted by Alpo View Post
    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.

    Quote Originally Posted by Alpo View Post
    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.

    Quote Originally Posted by Alpo View Post
    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.

    -
    Last edited by megafiddle; 09-20-2014 at 09:24 PM.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Alpo View Post
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Math
    By knightjp in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 04-01-2009, 05:36 PM
  2. Basic Math Problem. Undefined Math Functions
    By gsoft in forum C Programming
    Replies: 1
    Last Post: 12-28-2004, 03:14 AM
  3. Math in C++
    By Soccerman1234 in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2004, 12:34 PM
  4. Help with Math...
    By OneStiffRod in forum C++ Programming
    Replies: 4
    Last Post: 10-10-2002, 04:20 PM
  5. Gah! More math!
    By BMJ in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-02-2002, 09:33 PM