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.

It has additional methods:

  • of_centre_sides_radius
  • open

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

  • side_len
  • outer_radius
  • inner_radius
  • interior_angle
  • exterior_angle
  • vertices

of_centre_sides_radius

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.

outer_radius

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

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.

See also

If you found this article useful, you might be interested in the book NumPy Recipes or other books by the same author.

Join the PythonInformer Newsletter

Sign up using this form to receive an email when new content is added:

Popular tags

2d arrays abstract data type alignment and angle animation arc array arrays bar chart bar style behavioural pattern bezier curve built-in function callable object chain circle classes clipping close closure cmyk colour combinations comparison operator comprehension context context manager conversion count creational pattern data science data types decorator design pattern device space dictionary drawing duck typing efficiency ellipse else encryption enumerate fill filter font font style for loop formula function function composition function plot functools game development generativepy tutorial generator geometry gif global variable gradient greyscale higher order function hsl html image image processing imagesurface immutable object in operator index inner function input installing iter iterable iterator itertools join l system lambda function latex len lerp line line plot line style linear gradient linspace list list comprehension logical operator lru_cache magic method mandelbrot mandelbrot set map marker style matplotlib monad mutability named parameter numeric python numpy object open operator optimisation optional parameter or pandas partial application path pattern permutations pie chart pil pillow polygon pong positional parameter print product programming paradigms programming techniques pure function python standard library radial gradient range recipes rectangle recursion reduce regular polygon repeat rgb rotation roundrect scaling scatter plot scipy sector segment sequence setup shape singleton slice slicing sound spirograph sprite square str stream string stroke structural pattern subpath symmetric encryption template tex text text metrics tinkerbell fractal transform translation transparency triangle truthy value tuple turtle unpacking user space vectorisation webserver website while loop zip zip_longest