gummworld2.geometry (version $Id: geometry.py 432 2013-09-07 03:43:04Z stabbingfinger@gmail.com $)
index
c:\cygwin64\home\bw\dev\python\dist\gummworld2\gamelib\gummworld2\geometry.py

geometry.py - Geometry module for Gummworld2.
 
Geometry classes and functions.

 
Modules
       
pygame
sys

 
Classes
       
__builtin__.object
CircleGeometry
Ellipse
LineGeometry
PolyGeometry
RectGeometry
pygame.Rect(__builtin__.object)
Diamond

 
class CircleGeometry(__builtin__.object)
     Methods defined here:
__init__(self, origin, radius)
getorigin(self)
getposition(self)
GOTCHA: Something like "circle_geom.position.x += 1" will not do what
you expect. That operation does not update the rect instance variable.
Instead use "circle_geom.position += (1,0)".
getradius(self)
setposition(self, val)
setradius(self, val)

Static methods defined here:
collided = circle_collided_other(self, other, rect_pre_tested=None)
Return results of collision test between a circle and other geometry.
 
The *_collided_other() functions provide a convenient means to test
disparate geometries for collision. The logic considers containment to be
a collision. The tests do not check for the "self is other" condition.
 
self is a circle, it must have origin and radius attrs.
 
If other does not have a collided attr, then a circle-vs-rect result is
returned.
 
Otherwise...
 
other must have attr collided. collided must be a staticmethod
*_collided_other function or a custom function in that form. If collided is
not one of the *_collided_other functions, other's collided function will
be called passing arguments other and self in that order.
 
other's collided attr dictates the kind of self-vs-other collision test. The
builtin collision tests are:
 
    1.  If other.collided is circle_collided_other, other is assumed to be a
        circle with origin and radius attrs.
    2.  If other.collided is rect_collided_other, other is assumed to be a
        rect with a rect attr.
    3.  If other.collided is poly_collided_other, other is assumed to be a
        poly with a points attr.
    4.  If other.collided is line_collided_other, other is assumed to be a
        line with an end_points attr.
 
The fall-through action is to call other.collided(other, self), which is
assumed to be a custom staticmethod function.
 
Here is an example minimal class and usage:
    
    class CircleGeom(object):
        def __init__(self, **kwargs):
            self.__dict__.update(kwargs)
        collided = staticmethod(circle_collided_other)
    
    circle = CircleGeom(origin=(300,300), radius=25)
    circle.collide(circle, other_poly)

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)
origin
position
GOTCHA: Something like "circle_geom.position.x += 1" will not do what
you expect. That operation does not update the rect instance variable.
Instead use "circle_geom.position += (1,0)".
radius

 
class Diamond(pygame.Rect)
    
Method resolution order:
Diamond
pygame.Rect
__builtin__.object

Methods defined here:
__init__(self, *args)
Diamond(left, top, width, height) : Rect
Diamond((left, top), (width, height)) : Rect
Diamond(object) : Rect
 
Construct it like a pygame.Rect. All the Rect attributes and methods
are available.
 
In addition, there are read-only attributes for:
    *   Individual corners: top_center, right_center, bottom_center, and
        left_center.
    *   Individual edges: side_a, side_b, side_c, side_d.
    *   All corners as a tuple of coordinates: corners.
    *   All edges as a tuple of line segments: edges.

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
bottom_center
corners
edges
left_center
right_center
side_a
side_b
side_c
side_d
top_center

Methods inherited from pygame.Rect:
__coerce__(...)
x.__coerce__(y) <==> coerce(x, y)
__copy__(...)
__delitem__(...)
x.__delitem__(y) <==> del x[y]
__delslice__(...)
x.__delslice__(i, j) <==> del x[i:j]
 
Use of negative indices is not supported.
__eq__(...)
x.__eq__(y) <==> x==y
__ge__(...)
x.__ge__(y) <==> x>=y
__getitem__(...)
x.__getitem__(y) <==> x[y]
__getslice__(...)
x.__getslice__(i, j) <==> x[i:j]
 
Use of negative indices is not supported.
__gt__(...)
x.__gt__(y) <==> x>y
__le__(...)
x.__le__(y) <==> x<=y
__len__(...)
x.__len__() <==> len(x)
__lt__(...)
x.__lt__(y) <==> x<y
__ne__(...)
x.__ne__(y) <==> x!=y
__nonzero__(...)
x.__nonzero__() <==> x != 0
__reduce__(...)
__repr__(...)
x.__repr__() <==> repr(x)
__setitem__(...)
x.__setitem__(i, y) <==> x[i]=y
__setslice__(...)
x.__setslice__(i, j, y) <==> x[i:j]=y
 
Use  of negative indices is not supported.
__str__(...)
x.__str__() <==> str(x)
clamp(...)
Rect.clamp(Rect): return Rect
moves the rectangle inside another
clamp_ip(...)
Rect.clamp_ip(Rect): return None
moves the rectangle inside another, in place
clip(...)
Rect.clip(Rect): return Rect
crops a rectangle inside another
collidedict(...)
Rect.collidedict(dict): return (key, value)
test if one rectangle in a dictionary intersects
collidedictall(...)
Rect.collidedictall(dict): return [(key, value), ...]
test if all rectangles in a dictionary intersect
collidelist(...)
Rect.collidelist(list): return index
test if one rectangle in a list intersects
collidelistall(...)
Rect.collidelistall(list): return indices
test if all rectangles in a list intersect
collidepoint(...)
Rect.collidepoint(x, y): return bool
Rect.collidepoint((x,y)): return bool
test if a point is inside a rectangle
colliderect(...)
Rect.colliderect(Rect): return bool
test if two rectangles overlap
contains(...)
Rect.contains(Rect): return bool
test if one rectangle is inside another
copy(...)
Rect.copy(): return Rect
copy the rectangle
fit(...)
Rect.fit(Rect): return Rect
resize and move a rectangle with aspect ratio
inflate(...)
Rect.inflate(x, y): return Rect
grow or shrink the rectangle size
inflate_ip(...)
Rect.inflate_ip(x, y): return None
grow or shrink the rectangle size, in place
move(...)
Rect.move(x, y): return Rect
moves the rectangle
move_ip(...)
Rect.move_ip(x, y): return None
moves the rectangle, in place
normalize(...)
Rect.normalize(): return None
correct negative sizes
union(...)
Rect.union(Rect): return Rect
joins two rectangles into one
union_ip(...)
Rect.union_ip(Rect): return None
joins two rectangles into one, in place
unionall(...)
Rect.unionall(Rect_sequence): return Rect
the union of many rectangles
unionall_ip(...)
Rect.unionall_ip(Rect_sequence): return None
the union of many rectangles, in place

Data descriptors inherited from pygame.Rect:
__safe_for_unpickling__
bottom
bottomleft
bottomright
center
centerx
centery
h
height
left
midbottom
midleft
midright
midtop
right
size
top
topleft
topright
w
width
x
y

Data and other attributes inherited from pygame.Rect:
__new__ = <built-in method __new__ of type object>
T.__new__(S, ...) -> a new object with type S, a subtype of T

 
class Ellipse(__builtin__.object)
     Methods defined here:
__init__(self, origin, radius_x, radius_y)
Construct an instance of Ellipse.
 
The origin argument is a sequence of two numbers representing the origin
(x,y).
 
The radius_x argument is a number representing the radius along x-axis.
 
The radius_y argument is a number representing the radius along y-axis.
draw(self, surface, color, arcs=360)
Draw an ellipse on a pygame surface in the specified color.
 
The surface argument is the target pygame surface.
 
The color argument is the pygame color to draw in.
 
The arcs argument is the number of points in the circumference to draw;
normally 360 or 720 will suffice. More than 720 will draw many duplicate
pixels.
plot(self, arcs=360)
Plot the circumference of an ellipse and return the points in a list.
 
The arcs argument is the number of points in the circumference to
generate; normally 360 or 720 will suffice. More than 720 will result in
many duplicate points.
 
The list that is returned is a sequence of (x,y) tuples suitable for
use with pygame.draw.lines().
point(self, angle)
Plot a point on the circumference of an ellipse at the specified angle.
The point is returned as an (x,y) tuple.
 
The angle argument is the angle in degrees from the origin. The angle 0
and 360 are oriented at the top of the screen, and increase clockwise.

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)
origin
Set or get origin.
radius_x
Set or get radius_x.
radius_y
Set or get radius_y.

 
class LineGeometry(__builtin__.object)
     Methods defined here:
__init__(self, x1, y1, x2, y2, position=None)
getend_points(self)
getp1(self)
getp2(self)
getpoints(self)
getposition(self)
GOTCHA: Something like "line_geom.position.x += 1" will not do what
you expect. That operation does not update the line instance variable.
Instead use "line_geom.position += (1,0)".
getx1(self)
getx2(self)
gety1(self)
gety2(self)
setp1(self, xorxy, y=None)
setp2(self, xorxy, y=None)
setpoints(self, endpoints)
set end points of line
line.points = x1,y1,x2,y2
line.points = (x1,y1),(x2,y2)
setposition(self, val)
setx1(self, val)
setx2(self, val)
sety1(self, val)
sety2(self, val)

Static methods defined here:
collided = line_collided_other(self, other, rect_pre_tested=None)
Return results of collision test between a line and other geometry.
 
self is a line, it must have an end_points attr.
 
See circle_collided_other description for other details.

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)
end_points
p1
p2
points
position
GOTCHA: Something like "line_geom.position.x += 1" will not do what
you expect. That operation does not update the line instance variable.
Instead use "line_geom.position += (1,0)".
x1
x2
y1
y2

 
class PolyGeometry(__builtin__.object)
     Methods defined here:
__init__(self, points, position=None)
getpoints(self)
getposition(self)
GOTCHA: Something like "poly_geom.position.x += 1" will not do what
you expect. That operation does not update the rect instance variable.
Instead use "poly_geom.position += (1,0)".
setposition(self, val)

Static methods defined here:
collided = poly_collided_other(self, other, rect_pre_tested=None)
Return results of collision test between a poly and other geometry.
 
self is a poly, it must have a points attr.
 
See circle_collided_other description for other details.

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)
points
position
GOTCHA: Something like "poly_geom.position.x += 1" will not do what
you expect. That operation does not update the rect instance variable.
Instead use "poly_geom.position += (1,0)".

 
class RectGeometry(__builtin__.object)
     Methods defined here:
__init__(self, x, y, width, height, position=None)
getpoints(self)
getposition(self)
GOTCHA: Something like "rect_geom.position.x += 1" will not do what
you expect. That operation does not update the rect instance variable.
Instead use "rect_geom.position += (1,0)".
setposition(self, val)

Static methods defined here:
collided = rect_collided_other(self, other, rect_pre_tested=None)
Return results of collision test between a rect and other geometry.
 
self is a rect, it must have a rect attr.
 
See circle_collided_other description for other details.

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)
points
position
GOTCHA: Something like "rect_geom.position.x += 1" will not do what
you expect. That operation does not update the rect instance variable.
Instead use "rect_geom.position += (1,0)".

 
Functions
       
angle_of(origin, end_point)
Calculate the angle between the vector defined by end points (origin,point)
and the Y axis. All input and output values are in terms of pygame screen
space. Returns degrees as a float.
 
The origin argument is a sequence of two numbers representing the origin
point.
 
The point argument is a sequence of two numbers representing the end point.
 
The angle 0 and 360 are oriented at the top of the screen, and increase
clockwise.
circle_collided_other(self, other, rect_pre_tested=None)
Return results of collision test between a circle and other geometry.
 
The *_collided_other() functions provide a convenient means to test
disparate geometries for collision. The logic considers containment to be
a collision. The tests do not check for the "self is other" condition.
 
self is a circle, it must have origin and radius attrs.
 
If other does not have a collided attr, then a circle-vs-rect result is
returned.
 
Otherwise...
 
other must have attr collided. collided must be a staticmethod
*_collided_other function or a custom function in that form. If collided is
not one of the *_collided_other functions, other's collided function will
be called passing arguments other and self in that order.
 
other's collided attr dictates the kind of self-vs-other collision test. The
builtin collision tests are:
 
    1.  If other.collided is circle_collided_other, other is assumed to be a
        circle with origin and radius attrs.
    2.  If other.collided is rect_collided_other, other is assumed to be a
        rect with a rect attr.
    3.  If other.collided is poly_collided_other, other is assumed to be a
        poly with a points attr.
    4.  If other.collided is line_collided_other, other is assumed to be a
        line with an end_points attr.
 
The fall-through action is to call other.collided(other, self), which is
assumed to be a custom staticmethod function.
 
Here is an example minimal class and usage:
    
    class CircleGeom(object):
        def __init__(self, **kwargs):
            self.__dict__.update(kwargs)
        collided = staticmethod(circle_collided_other)
    
    circle = CircleGeom(origin=(300,300), radius=25)
    circle.collide(circle, other_poly)
circle_intersects_circle(origin1, radius1, origin2, radius2)
Circle vs circle collision test.
 
Return True if circles intersect, else return False.
circle_intersects_line(origin, radius, line_segment)
Circle vs line collision test. Return point of contact, or None.
 
Return True if line_segment intersects the circle defined by origin and
radius. Return False if they do not intersect.
circle_intersects_poly(origin, radius, points)
Circle vs polygon collision test.
 
Points must describe a "closed" polygon with no redundant points. Winding is
a factor.
circle_intersects_rect(origin, radius, rect)
Circle vs rect collision test.
 
Return True if the shapes intersect, else return False. rect must be a
pygame.Rect().
distance(a, b)
Calculate the distance between points a and b. Returns distance as a float.
 
The a argument is a sequence representing one end point (x1,y1).
 
The b argument is a sequence representing the other end point (x2,y2).
interpolant_of_line(mag, p1, p2)
Find the point at magnitude mag along a line.
line_collided_other(self, other, rect_pre_tested=None)
Return results of collision test between a line and other geometry.
 
self is a line, it must have an end_points attr.
 
See circle_collided_other description for other details.
line_intersects_line(line_1, line_2)
Line vs line collision test.
 
Returns [x,y] if the lines intersect, otherwise []. [x,y] is the point
of intersection.
 
The return values are suitable for "if collided: ... else: ..." usage.
Accessing the (x,y) values is optional.
line_intersects_poly(line, points, fast=True)
Line vs poly test.
 
Tests a line against a polygon. Returns a list of intersecting
(x,y) pairs, or an emtpy list if there are no collisions.
 
This does not detect containment. point_in_poly() can be used to test for
containment.
line_intersects_rect(line, rect, fast=True)
Line vs rect intersection test.
 
rect must be a pygame.Rect().
 
Tests a line against the edges of a rect. Returns a list of intersecting
(x,y) pairs, or an emtpy list if there are no collisions.
 
This does not detect containment. point_in_poly() can be used to test for
containment.
lines_intersect_lines(lines1, lines2, fast=True)
Line lists intersection test.
 
Tests one list of lines against another. Returns a list of intersecting
(x,y) pairs, or an emtpy list if there are no collisions.
 
This does not detect containment. point_in_poly() can be used to test for
containment.
point_in_poly(point, poly)
Point vs polygon collision test.
 
Poly is assumed to be a sequence of points, length >= 3, with no duplicate
points. Winding is not a factor.
point_on_circumference(center, radius, degrees_)
Calculate the point on the circumference of a circle defined by center and
radius along the given angle. Returns a tuple (x,y).
 
The center argument is a representing the origin of the circle.
 
The radius argument is a number representing the length of the radius.
 
The degrees_ argument is a number representing the angle of radius from
origin. The angles 0 and 360 are at the top of the screen, with values
increasing clockwise.
points_to_lines(points)
Return a list of end-point pairs assembled from a "closed" polygon's
points.
poly_collided_other(self, other, rect_pre_tested=None)
Return results of collision test between a poly and other geometry.
 
self is a poly, it must have a points attr.
 
See circle_collided_other description for other details.
poly_intersects_poly(points1, points2, fast=True)
Poly vs poly intersection test.
 
Tests a polygon against another polygon. Returns a list of intersecting
(x,y) pairs, or an emtpy list if there are no collisions.
 
This does not detect containment. point_in_poly() can be used to test for
containment.
poly_intersects_rect(points, rect, fast=True)
Poly vs rect intersection test.
 
Tests a polygon against a rect. rect must be a pygame.Rect(). Returns a
list of intersecting (x,y) pairs, or an emtpy list if there are no
collisions.
 
This does not detect containment. point_in_poly() can be used to test for
containment.
rect_collided_other(self, other, rect_pre_tested=None)
Return results of collision test between a rect and other geometry.
 
self is a rect, it must have a rect attr.
 
See circle_collided_other description for other details.
rect_to_lines(rect)
Return a list of end-point pairs assembled from a pygame.Rect's corners.
step_toward_point(p1, p2, distance)
Calculate the point at a given distance along the line with end points
(x1,y1) and (x2,y2).
 
This function is about twice as fast as the combination angle_of() and
point_on_circumference().
 
The (x1,y1) argument is the origin.
 
The (x2,y2) argument is the destination.
 
The distance argument is the size of the step, or speed.

 
Data
        __all__ = ['Ellipse', 'Diamond', 'LineGeometry', 'RectGeometry', 'CircleGeometry', 'PolyGeometry', 'angle_of', 'distance', 'interpolant_of_line', 'point_on_circumference', 'step_toward_point', 'point_in_poly', 'circle_collided_other', 'rect_collided_other', 'line_collided_other', 'poly_collided_other', 'circle_intersects_circle', 'circle_intersects_line', 'circle_intersects_rect', 'circle_intersects_poly', ...]
__author__ = 'Gummbum, (c) 2011-2014'
__version__ = '$Id: geometry.py 432 2013-09-07 03:43:04Z stabbingfinger@gmail.com $'

 
Author
        Gummbum, (c) 2011-2014