[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Re: Rotating an Image about another Point



You may also find this function useful, which I've pulled out of one of my games-in-progress (take a look at http://pen.svn.sourceforge.net/viewvc/pen/data/parts/cannon.py?revision=31&view=markup for an example):

def rotate_point( pt, rect, angle ):
	"""Maps coordinates from a given rectangle into a rotated rectangle.

	For example, the following should be approximately equivalent:
	  pygame.draw.circle( surface, pt, ... )
	  result = pygame.transform.rotate( surface, angle )

	  result = pygame.transform.rotate( surface, angle )
	  pygame.draw.circle( result, rotate_point( pt, rect, angle )
	"""

	# move the center to the origin, and find the corners
	translated = rect.move( -pt[0], -pt[1] )
	corners    = [translated.topleft, translated.topright, translated.bottomleft, translated.bottomright]

	# rotate the corners
	theta = math.radians( angle )
	c, s  = math.cos( theta ), math.sin( theta )
	xcoords = []
	ycoords = []
	for x,y in corners:
		xcoords.append( c*x + s*y )
		ycoords.append( c*y - s*x )

	# return the leftmost point and the topmost point
	return (- min( xcoords ), - min( ycoords ))


--Mike




Ian Mallett wrote:
Opps.
Hadn't found:
http://archives.seul.org/pygame/users/Jun-2006/msg00236.html
Everyone look at that.  Very nice.  Thanks.
Ian