7

I know there would be no tool in ArcPy module as such but might have in third party modules. I however tried taking the vertices of the polygon and moving using the below formula. But did not get the correct output coordinate values and polygon thus generated is weird.

I have tried this function

def RotateAxis(AnchorX,AnchorY,inputx,inputy, WindDirection): x = inputx - AnchorX y = inputy - AnchorY resultx = (x * math.cos(WindDirection)) - (y * math.sin(WindDirection)) + AnchorX resulty = (x * math.sin(WindDirection)) + (y * math.cos(WindDirection)) + AnchorY return (resultx,resulty) 

where Anchor is the origin and WindDirection is the angle which I tried taking in radians as well as degrees but could not get correct results.

I tried with projected coordinates that didn't work either. Is there any suggestion?

2
  • Please tell us precisely how you could not get correct results. What were the inputs and what were the outputs? Commented Apr 17, 2012 at 6:04
  • To quickly answer one of your questions: angular units are radians. Commented Apr 18, 2012 at 2:59

1 Answer 1

18

You may use the following function:

def Rotate2D(pts,cnt,ang=pi/4): '''pts = {} Rotates points(nx2) about center cnt(2) by angle ang(1) in radian''' return dot(pts-cnt,ar([[cos(ang),sin(ang)],[-sin(ang),cos(ang)]]))+cnt 

It works well as can be seen in the following figures: about the anchor point on (0,0):
enter image description here

about the anchor point on (1,0):
enter image description here

about the anchor point on (0.5,0.5):
enter image description here

Update:
Well here is the full code, you must be able to generate the exact results as here:

from __future__ import division #to avoid integer devision problem import scipy import pylab #just for fun making further development easier and with joy pi = scipy.pi dot = scipy.dot sin = scipy.sin cos = scipy.cos ar = scipy.array rand = scipy.rand arange = scipy.arange plot = pylab.plot show = pylab.show axis = pylab.axis grid = pylab.grid title = pylab.title rad = lambda ang: ang*pi/180 #lovely lambda: degree to radian #the function def Rotate2D(pts,cnt,ang=pi/4): '''pts = {} Rotates points(nx2) about center cnt(2) by angle ang(1) in radian''' return dot(pts-cnt,ar([[cos(ang),sin(ang)],[-sin(ang),cos(ang)]]))+cnt #the code for test pts = ar([[0,0],[1,0],[1,1],[0.5,1.5],[0,1]]) plot(*pts.T,lw=5,color='k') #points (poly) to be rotated for ang in arange(0,2*pi,pi/8): ots = Rotate2D(pts,ar([0.5,0.5]),ang) #the results plot(*ots.T) axis('image') grid(True) title('Rotate2D about a point') show() 

The results are:
enter image description here
enter image description here

Good luck!

Appendices:

>>> pts array([[ 0. , 0. ], [ 1. , 0. ], [ 1. , 1. ], [ 0.5, 1.5], [ 0. , 1. ]]) >>> pts.shape (5, 2) >>> anchor = ar([0.5,0.5]) >>> anchor array([ 0.5, 0.5]) >>> ots = Rotate2D(pts,anchor,ang=rad(45)) >>> ots array([[ 0.5 , -0.20710678], [ 1.20710678, 0.5 ], [ 0.5 , 1.20710678], [-0.20710678, 1.20710678], [-0.20710678, 0.5 ]]) >>> ots.shape (5, 2) >>> '''a single point''' 'a single point' >>> pts = ar([3.1,1.3]) >>> pts.shape (2,) >>> ots = Rotate2D(pts,anchor,ang=rad(30)) >>> ots array([ 2.35166605, 2.49282032]) >>> ots.shape (2,) >>> dts = ots.reshape(-1,2) >>> dts array([[ 2.35166605, 2.49282032]]) >>> dts.shape (1, 2) 
9
  • Note that ar is array (numpy or scipy) and also note that cnt is an array i.e., array([0,0]). All these are necessary to avoid problems with multiplication of lists in Python. Commented Apr 17, 2012 at 13:57
  • I could not get it to work. I have a list of points and one anchor point I would like to get the rotated location of the points calculated per the angle specified. Commented Apr 17, 2012 at 16:45
  • I have used this code: Commented Apr 17, 2012 at 16:46
  • Once more, @Harry: exactly how does your attempt at a solution not "work"? We need something to go on here if we're going to do any better than guessing! Commented Apr 17, 2012 at 16:58
  • def RotateAxis(AnchorX,AnchorY,inputx,inputy, WindDirection): x = inputx - AnchorX y = inputy - AnchorY resultx1 = x * cos(radians(WindDirection)) resultx2 = y * sin(radians(WindDirection)) resultx = resultx1 + resultx2 + AnchorX resulty1 = x * sin(radians(WindDirection)) resulty2 = y * cos(radians(WindDirection)) resulty = resulty1 + resulty2 + AnchorY return (resultx,resulty) Commented Apr 17, 2012 at 20:44

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.