Martin McBride, 2022-06-05
Tags geometry polygon regular polygon
Categories generativepy generative art
RegularPolygon class draws a regular polygon.
A regular polygon is defined by its:
- Number of sides.
- Radius (the distance from the centre to any one of its vertices).
You can also draw a regular polygon using the
Polygon class, by calculating the position of each vertex. The
RegularPolygon class is more convenient because it performs the calculations automatically, and also provides some useful properties of the shape.
See the RegularPolygon tutorial for examples.
RegularPolygon class methods
The Polygon class inherits
clip and other methods from Shape.
It has additional methods:
It has several readonly properties (these are simply calculated from the centre, number of sides, and radius):
Creates a regular polygon based on its parameters.
of_centre_sides_radius(centre, numsides, radius, angle=0)
|centre||(x, y) tuple of two numbers||The centre of the polygon.|
|numsides||int, 3 or greater||The number of sides of the polygon.|
|radius||number||The distance from the centre to any one of its vertices.|
|angle||number||Angle to rotate the shape (defaults to zero).|
Creates a regular polygon, centred at
radius controls the distance of each vertex from the centre, and therefore indirectly controls the size.
By default, the shape is oriented so that the bottom side is horizontal. If
angle is set to a non-zero value, the shape will be rotated about its centre by
angle radians in a clockwise direction.
Creates an open polygon, rather than a closed polygon.
Calling this method will cause the final polygon to be open - the last point will not be connected to the first point (see the example). To create a closed polygon, simply don't call this method.
side_len property gives the length of each side of the polygon. This is a readonly property calculated from the
outer_radius property gives the radius of a circle, with the same centre as the polygon, that would pass through the vertices of the polygon. In other words, it is the smallest circle that completely encloses the polygon. This is a readonly property equal to the
radius but it is included as a property for convenience.
inner_radius property gives the radius of a circle, with the same centre as the polygon, that would just touch the sides of the polygon. In other words, it is the largest circle that fits inside the polygon. This is a readonly property calculated from the
interior_angle property gives the interior of the polygon. This is a readonly property calculated from
exterior_angle property gives the exterior of the polygon. This is a readonly property calculated from
vertices property is a tuple containing the positions of the vertices of the polygon. Each vertex is stored as a tuple
(x, y), so
vertices is a tuple of tuples. This is a readonly property calculated from the
For illustrations of these properties see the RegularPolygon tutorial.