RegularPolygon

By Martin McBride, 2022-06-05
Tags: geometry polygon regular polygon
Categories: generativepy generative art

The RegularPolygon class draws a regular polygon.

A regular polygon is defined by its:

• Centre.
• 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 add, fill, stroke, fill_stroke, path, clip and other methods from Shape.

• open

It has several readonly properties (these are simply calculated from the centre, number of sides, and radius):

• side_len
• interior_angle
• exterior_angle
• vertices

Creates a regular polygon based on its parameters.

of_centre_sides_radius(centre, numsides, radius, angle=0)

Parameter Type Description
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 centre, with numsides sides. 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.

open

Creates an open polygon, rather than a closed polygon.

open()


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

The side_len property gives the length of each side of the polygon. This is a readonly property calculated from the radius and numsides.

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.

The 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 radius and numsides.

interior_angle

The interior_angle property gives the interior of the polygon. This is a readonly property calculated from numsides.

exterior_angle

The exterior_angle property gives the exterior of the polygon. This is a readonly property calculated from numsides.

vertices

The 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 centre, radius, and numsides.

For illustrations of these properties see the RegularPolygon tutorial.