# 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] //coordinate of last point
ABy=0
CDy=w1[last-1]
//          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]
ycd = w1[j]
xprime = (xcd*(cos(theta)))-(ycd*(sin(theta)))
yprime = (xcd*(sin(theta)))+(ycd*(cos(theta)))
w1[j] = xprime
w1[j] = 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. Forum Support Gallery