Rotate a set of 2D coordinates about the origin

Rotates a set of 2D coordinates about the origin. Written for counterclockwise rotation so that last coordinate in a 2D wave is moved to y=0.
2D waves are organized with x cordinates in first column y coordinates in second column.
To move to a different position ABx ABy and the if-else-endif statements need to be changed.
Function Rotator(wavenames) //Select top graph. Execute with rotator(wavelist("*",";","WIN:"))
    String wavenames
    Variable theta, xprime, yprime, xcd, ycd
    variable i=0
    Variable j=0 // i will count through the waves, j will be row index
    Variable ABx, CDx, ABy, CDy
    For (i = 0; i < ItemsInList(wavenames); i += 1)
        String name = StringFromList(i,wavenames)
        Wave/z w1= $name
        variable last = dimsize(w1,0)
            ABx=1 //calculate theta. ABx and ABy are set at 1,0. This will give a 1 unit long line along the x-axis.
            CDx=w1[last-1][0] //coordinate of last point
            ABy=0
            CDy=w1[last-1][1]
            theta=acos(((ABx*CDx)+(ABy*CDy))/(sqrt((ABx^2)+(ABy^2))*sqrt((CDx^2)+(CDy^2)))) //answer is in radians 
//          print name, " is ",theta," radians", "or" , theta*(180/pi), "degrees. last-1 =", CDx,",", CDy //remove // for debugging
            if (CDy<0)
                theta=theta                             // execute if condition is TRUE
            else
                theta=(2*PI)-theta                      // execute if condition is FALSE
            endif //if-elseif-endif statement checks if last point is above the x-axis and corrects the rotation.
            For (j = 0; j < last; j+=1)
                xcd = w1[j][0]
                ycd = w1[j][1]
                xprime = (xcd*(cos(theta)))-(ycd*(sin(theta)))
                yprime = (xcd*(sin(theta)))+(ycd*(cos(theta)))
                w1[j][0] = xprime
                w1[j][1] = yprime
            endfor
//          print "Now y is", ",", yprime   //remove // for debugging. Should be ~0.
    endfor
End
This might be a lot easier if you use the r2polar and p2rect functions and if you used complex waves instead of 2-column real waves.

--Jim Prouty
Software Engineer, WaveMetrics, Inc.
2D Rotation can be expressed simply as a 2x2 matrix and so the last loop in your code is a simple MatrixOP matrix-matrix multiplication (one line of code).

A.G.
WaveMetrics, Inc.

Forum

Support

Gallery

Igor Pro 9

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More