# Regular polygons in generativepy

Martin McBride, 2022-06-05
Tags generativepy tutorial fill stroke polygon regular polygon
Categories generativepy generativepy tutorial

This tutorial shows how to create regular polygons in generativepy, using the RegularPolygon class.

Here are some related tutorials that contain useful information about other features this tutorial uses:

## RegularPolygon example code

Here is the code to show some of the features of RegularPolygon:

from generativepy.drawing import make_image, setup
from generativepy.color import Color
from generativepy.geometry import RegularPolygon, Circle
import math

def draw(ctx, pixel_width, pixel_height, frame_no, frame_count):

setup(ctx, pixel_width, pixel_height, background=Color(0.8))

red = Color('crimson')
green = Color('darkgreen')
blue = Color('dodgerblue')

.fill(blue)\
.stroke(green, 5)

.fill(blue)\
.stroke(green, 5)

.fill(blue)\
.stroke(green, 5)

p = RegularPolygon(ctx).of_centre_sides_radius((150, 400), 5, 100)\
.fill(blue)\
.stroke(green, 5)

p = RegularPolygon(ctx).of_centre_sides_radius((400, 400), 5, 100)\
.fill(blue)\
.stroke(green, 5)

p = RegularPolygon(ctx).of_centre_sides_radius((650, 400), 5, 100)\
.fill(blue)\
.stroke(green, 5)
for v in p.vertices:

make_image("regularpolygons-tutorial.png", draw, 800, 550)


This code is available on github in tutorial/shapes/regularpolygons.py.

Here is the resulting image:

We will examine this code in the sections below.

## Drawing basic regular polygons

This section of the code draws the two polygons in the top left of the image:

    RegularPolygon(ctx).of_centre_sides_radius((150, 150), 5, 100)\
.fill(blue)\
.stroke(green, 5)

.fill(blue)\
.stroke(green, 5)


This code creates two polygons with:

• A suitable centre point to position them on the page.
• 5 sides (pentagon) and 6 sides (hexagon).
• Radius of 100 to set the size.
• Filled in blue and outlined in green.

Notice that both shapes have horizontal bases.

## Drawing a rotated polygon

This code draws the hexagon in the top left of the image:

    RegularPolygon(ctx).of_centre_sides_radius((650, 150), 6, 100, math.pi/12)\
.fill(blue)\
.stroke(green, 5)


This is drawn in the same way as the previous hexagon, but with an angle of math.pi/12 radians (15 degrees). This rotates the shape 15 degrees clockwise about its centre, so the base is no longer horizontal.

## Drawing an inner circle

This code draws the same pentagon as before, but with an inner circle (bottom left of the main image):

    p = RegularPolygon(ctx).of_centre_sides_radius((150, 400), 5, 100)\
.fill(blue)\
.stroke(green, 5)


When we draw the pentagon, we also store the RegularPolygon object as p.

We then draw a circle. The circle has the same centre as the polygon, and a radius equal to the inner radius of the polygon. This is obtained from p.inner_radius.

The circle just fits inside the polygon.

## Drawing an outer circle

This code draws the same pentagon as before, but with an outer circle (bottom centre of the main image):

    p = RegularPolygon(ctx).of_centre_sides_radius((400, 400), 5, 100)\
.fill(blue)\
.stroke(green, 5)


This time, the circle has the same centre as the polygon, and a radius equal to the outer radius of the polygon. This is obtained from p.outer_radius.

The polygon just fits inside the circle.

## Drawing the vertices

This code draws the same pentagon as before, but marks each corner with a dot (bottom right of the main image):

    p = RegularPolygon(ctx).of_centre_sides_radius((650, 400), 5, 100)\
.fill(blue)\
.stroke(green, 5)
for v in p.vertices:

Then we use p.vertices to get the corners of the polygon. This returns a tuple of five coordinates (x, y) corresponding to the five vertices of the pentagon.
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.19672666490077972, "perplexity": 4353.818025604299}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 20, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2022-33/segments/1659882571909.51/warc/CC-MAIN-20220813051311-20220813081311-00389.warc.gz"}