Shop OBEX P1 Docs P2 Docs Learn Events
rotating a point around another point — Parallax Forums

rotating a point around another point

science_geekscience_geek Posts: 247
edited 2009-08-19 17:11 in Propeller 1
im trying to get the prop to rotate· a point around a different point. the equations im using are X'= X*COS(a) - Y*SIN(a)
[font=Arial,Helvetica][size=+1]····· Y'= X*SIN(a) + Y*COS(a)· im having trouble getting sin and cos. i know there are sin lookup tables but i have no idea where to start to get this information. can someone help me figure out how to get a sin and cos value that i can plug into this equation so i can do point rotation.[/size][/font]

any help is much appreciated

Comments

  • KyeKye Posts: 2,200
    edited 2009-08-17 04:03
    Its a work in progress, but the sin, cos, and tan·are functional.

    Don't trust the other trig functions. They aren't complete/tested.

    The idea is that you supply the radius of a circle to the library functions so that it can return an integer value after it computes the cos/sin/tan and not just a number between 0 and 1 since that is not an integer.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
  • heaterheater Posts: 3,370
    edited 2009-08-17 05:15
    Here is a trick to rotate a point around the origin using just add, subtract and shift:

    x = (2 ** 31)
    y = 0
    for n in range(1, 203):
        x = x - (y >> 5)
        y = y + (x >> 5)
        print x, y
    
    



    Ok it's written in Python but I'm sure you can see how to do it in Spin/PASM. Adjust the "203" to change the number of steps and adjust the "5"s to change the step size.

    Scale your points x's and y's coming in and going out. If you want to rotate around a point other than the origin just translate the point accordingly on the way in and the results on the way out.

    No idea what your application is but this is neat for continuous animation of say a moon around a planet or a star field. Some place where you don't require a totally smooth circular path.

    Attached is a plot of the points produce by the above.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
    757 x 672 - 8K
  • cessnapilotcessnapilot Posts: 182
    edited 2009-08-18 09:20
    Heater,

    Thanks to the method you posted, I understand the CORDIC method better than before. It is interesting, that I've never seen such simple,·attractive and convincing graphical demonstration of the root of the algorithm.

    A short summary for the expansion from your method to CORDIC:

    One can add that the stepsize n· should be· small to make the rotation matrix as simple as

    1 - f
    f + 1

    where

    f=1/2^n

    is the angle step in approximate radians.

    For small f angle

    COS(f) approx. 1
    SIN(f) approx. f

    and one needs 2*Pi/f steps to draw a circle.

    To rotate a vector through an· arbitrary angle of f, we calculate:

    x' = x * COS(f) - y * SIN(f)
    y' = x * SIN(f) + y * COS(f)

    and this simplifies for small f. Since f=1/2^n,·one can do the multiplication with it with simple right shifts (division). When we want to proceed in larger steps toward a specified angle theta, then come the successive approximation(iteration) and the precomputed tables of sines/cosines into the picture.


    Cheers,


    Istvan
  • heaterheater Posts: 3,370
    edited 2009-08-18 12:41
    Interesting, I know nothing of CORDIC. The method I presented was used at the Marconi Radar Company in the early seventies to generate the points needed to draw range rings on huge old round radar displays using vector graphics. Implemented with some simple logic chips.

    To my mind it works because the differential of sin(a) is cos(a) and the differential of cos(a) is minus sin(a).
    (Differential = rate of change). Then x is proportional to sin(a) and y is proportional to cos(a). So we can add a wee bit of y to x to get our next x and subtract a wee little bit of x from y to get our next y.

    Don't you just love this "hand waving" maths.

    Now a problem: Looking at that last circle plot I posted it seemed to be a bit distorted and so it is. When the radius gets smaller the distortion gets worse. So if you were doing this for rough circles using 8 bit arithmetic things start to look like the attached. There I have plotted an accurate circle for comparison. Radius = 50 in both cases. As you see the first quadrant is pretty good but the other three are too close to the origin.

    I don't expect my circles to not be rough using small integer arithmetic but is there a way to pull this into shape?

    #!/usr/bin/python
    
    from math import *
    
    radius = 50
    steps = 50
    
    # A circle generated using add, subtract and shift
    x = radius
    y = 0
    for n in range(0, steps):
        print x , y
        x = x + (y >> 3)
        y = y - (x >> 3)
    
    # An accurate circle for reference
    for n in range (0, steps):
        print x, y
        x = radius * cos(((2 * pi) / steps) * n)
        y = radius * sin(((2 * pi) / steps) * n)
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
    717 x 600 - 25K
  • lonesocklonesock Posts: 917
    edited 2009-08-18 16:35
    heater said...
    I don't expect my circles to not be rough using small integer arithmetic but is there a way to pull this into shape?
    In my old "mode 13h" days, we would use symmetry to draw the circle: use your method for the 1st 1/8th of a circle, then draw each calculated point 8 times. For a unit circle, the points would be mirrored around (1,0) (0,1) (-1,0) (0,-1). The animation of the drawing would look like the circle was drawn starting at North South East West, and fill in along the circumference. Does that make sense?

    Jonathan

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    lonesock
    Piranha are people too.
  • heaterheater Posts: 3,370
    edited 2009-08-19 17:11
    Yes indeed it does lonesock, I had thought of doing it in a quadrant and drawing each generated point 4 times appropriately mirrored around the axis.

    BUT, one application I mentioned earlier is animating a moon around a planet or lots of points in a star field background. In which case we really only want one point at a time continuously moving.

    Of course one could move the "moon"/"star" through approx 90 degrees and then reset the x and y to 1, 0, -1, appropriately and continue, which seemed a bit clunky to me.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
Sign In or Register to comment.