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
{"ft_lang_label":"__label__en","ft_lang_prob":0.6086687,"math_prob":0.9936174,"size":3005,"snap":"2019-26-2019-30","text_gpt3_token_len":1041,"char_repetition_ratio":0.09763412,"word_repetition_ratio":0.78125,"special_character_ratio":0.34941763,"punctuation_ratio":0.14240506,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99741286,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-06-24T09:33:12Z\",\"WARC-Record-ID\":\"<urn:uuid:fda07c45-163a-4ecb-a066-802fd8b3c96f>\",\"Content-Length\":\"67643\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:7955c4f1-8349-4391-aa43-1225d63a0756>\",\"WARC-Concurrent-To\":\"<urn:uuid:faaa10df-59ab-4f58-8756-295543c5ec68>\",\"WARC-IP-Address\":\"67.225.238.18\",\"WARC-Target-URI\":\"https://www.wavemetrics.com/code-snippet/rotate-set-2d-coordinates-about-origin\",\"WARC-Payload-Digest\":\"sha1:KVLVFKJCL6LJPPCLMT3MSWBE5K42UPCA\",\"WARC-Block-Digest\":\"sha1:JTI37E4BANKLNDYYSKAZDDDYBMJRRYC6\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-26/CC-MAIN-2019-26_segments_1560627999298.86_warc_CC-MAIN-20190624084256-20190624110256-00335.warc.gz\"}"}