Copy the script shown, which is rough because of the fairly big error of calculating angular differences.

Alternatively you can use this script, which uses more of the script limit, but gives you more accurate results. (Credits to Qma for the method, and vi for the arcus function) This is the differences of the scripts (top is old, bottom is new)