generativepy.shape2d module

By Martin McBride, 2023-04-27
Tags: formula tex latex
Categories: generativepy generative art


The shape2d module provide support for abstract shapes.

The module provides a Points object that stores a list of points that can be used to represent a polygon. The list of points that can be manipulated before being drawn.

The module doesn't have any drawing capability of its own, but a Points object can be passed into a Polygon object's of_points method to draw the polygon.

A Points object can be created from a list of coordinates, or via static methods. Currently there is one stati method, to create a regaular polygon.

Future versions of this class will contain other methods for manipulating shapes.

Points

A Points object is a wrapper around a list of Vector objects that each represent one vertex of a polygon.

Points objects are immutable.

staticmethod regular_polygon

Returns a Points object representing a regular polygon

regular_polygon(sides, centre=(0, 0), radius=1, flat_base=True)
Parameter Type Description
sides int Number of sides of polygon, must be 3 or greater
centre (Number, Number) Centre of polygon, default (0, 0)
radius Number Outer radius of polygon, default 1
flat_base Bool True (default) for flat base

Create a Points object representing a regular polygon.

sides is the number of sides - 3 for an equilateral triangle, 4 for a square, etc.

centre is the centre of the polygon.

The radius is the radius of a circle that tightly encloses the polygon.

If flat_base is true the points will be arranged so that the polygon base is horizontal. If it is false the points will be arranged so that the first vertex is on the x-axis.

To obtain other orientations, use the rotate method.

Constructor

Creates a Points object.

Points(points)
Vector(seq)
Parameter Type Description
points iterable of (Number, Number) items Points of shape

points is a iterable containing a list of point values. It can be a list, a tuple, another Points object, or any other iterable with at least 3 values.

Each value within the points parameter must be a sequence of two numbers. It can be a list, a tuple, a Vector or similar.

transform

    transform(m)
Parameter Type Description
m Matrix

Creates a Points that is the result of multiplying every point is this Points object by m.

scale

    scale(scale_x, scale_y=None)
Parameter Type Description
scale_x Number Scaling factor in x-direction
scale_y Number Scaling factor in y-direction

Creates a Points that is the result of scaling every point is this Points object by scale_x in the x-direction and scale_y in the y-direction.

If scale_y is None, the points will be scaled by scale_x in both directions.

translate

    translate(x, y)
Parameter Type Description
x Number x distance
y Number y distance

Creates a Points that is the result of translating every point is this Points object by (x, y).

rotate

    rotate(angle)
Parameter Type Description
angle Number Angle in radians

Creates a Points that is the result of rotating every point is this Points object by angle radians counterclockwise about the origin.

Standard operators

The Vector class supports a number of operators and built-in functions.

Variables used inn the examples:

  • m ia a Matrix objects.
  • p is a Points object.
  • n is a number (int or float).

Operator examples:

m * p      # Multiply matrix by points object (is transfrom the object using m).
u == v     # Test for equality, uses the `is_close` function on each corresponding pair of elements.
u[n]       # nth element of u. n must be an integer between 0 and 1 inclusive.

Built-in function examples:

len(p)     # Number of elements in p
iter(p)    # Return an iterator that iterates over the values in p then stops.
str(p)     # String representation of p

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