Primitive

Introduction

To help you create complex shapes, we created functions to create basic shapes and manipulate these shapes.

These functions are divided in three groups:

  • Create primitives shapes: square, circle, etc

  • Transformations: move, rotate, scale and invert

  • Boolean operations: add, sub, mult and xor


Create primitive shapes

Circle

Creates a circle, a positive SimpleShape instance from given radius and center

from shapepy import Primitive
circle = Primitive.circle(radius = 1, center = (0, 0))
SimpleShape instance circle of radius 1 and center (0, 0)

Square

Creates a square, a positive SimpleShape instance from given side and center

from shapepy import Primitive
square = Primitive.square(side = 1, center = (0, 0))
SimpleShape instance square of radius 1 and center (0, 0)

Triangle

Creates a triangle, a positive SimpleShape instance from given side and center

from shapepy import Primitive
triangle = Primitive.triangle(side = 1, center = (0, 0))
SimpleShape instance square of radius 1 and center (0, 0)

Polygon

Creates a polygon for given vertices, a positive SimpleShape instance

from shapepy import Primitive
vertices = [(1, 0),(0, 1), (-1, 1), (0, -1)]
simple = Primitive.polygon(vertices)
SimpleShape instance square of radius 1 and center (0, 0)

Regular polygon

Creates a regular polygon, a positive SimpleShape instance

from shapepy import Primitive
triangle = Primitive.regular_polygon(nsides = 3, radius = 1, center = (0, 0))
square = Primitive.regular_polygon(nsides = 4, radius = 1, center = (0, 0))
pentagon = Primitive.regular_polygon(nsides = 5, radius = 1, center = (0, 0))

reg3 reg4 reg5


Transformations

Move

Translate the entire shape by an amount (x, y)

from shapepy import Primitive
# Creates a circle of radius 1 and centered at origin (0, 0)
circle = Primitive.circle()
# Now the circle has radius 1 and center at (1, 2)
circle.move(1, 2)
Example of moving a circle of origin (0, 0) to (1, 2)

Rotate

Rotate counter-clockwise the entire shape

import math
from shapepy import Primitive
# Create square of side 2
square = Primitive.square(side = 2)
# Rotate the square in pi/6 radians
square.rotate(math.pi/6)
# Or in 30 degrees
square.rotate(30, degrees = True)
Example of rotating a square

Scale

Scale the entire shape in horizontal and vertical directions

from shapepy import Primitive
# Create square of side 2
square = Primitive.square(side = 2)
# Scales a square into a rectangle of width 2 and height 0.5
square.scale(2, 0.5)
Example of scaling a square

Invert

It’s possible to invert the orientation of a shape.

from shapepy import Primitive
# Create any shape, positive at counter-clockwise
circle = Primitive.circle()
# Change orientation to clockwise, negative
-circle

pic1 pic2


Boolean Operations

It’s possible to operate between two shapes by using |, &, - and ^:

Union / logic OR

The sum between two shapes is mathematically a union of two sets

from shapepy import Primitive
# Create two simple shapes
circle = Primitive.circle()
square = Primitive.square()
# Union
newshape = circle | square
newshape = circle + square
Schema of adding sets :math:`A` and :math:`B`
Table of union between two positive circles

Subtraction

The subtraction between two positive shapes means take out all part of \(A\) such is inside \(B\).

from shapepy import Primitive
# Create two positive shapes
circle = Primitive.circle()
square = Primitive.square()
# Subtract
newshape = circle - square
Schema of subtraction between sets :math:`A` and :math:`B`
Table of subtraction between two positive circles

Intersection / logic AND / Multiplication

The intersection between two shapes returns the common region between them.

# Create two positive shapes
circle = section.shape.primitive.circle()
square = section.shape.primitive.square()
# Subtract
newshape = circle * square
newshape = circle & square
Example of multiplication between two positive shapes
Table of intersection between two positive circles

XOR Operator

The xor between two positive shapes. For this operator, we use the symbol ^.

# Create two positive shapes
circle = section.shape.primitive.circle()
square = section.shape.primitive.square()
# Subtract
newshape = circle ^ square
Example of XOR between two positive shapes
Table of XOR between two positive circles

Rewrite operations

All the sub-operations (+, -, *, ^) operations are in fact only combinations of |, & and ~. On the background, it works only with these three and the other operations are transformed:

  • The A + B is transformed to A | B

  • The A * B is transformed to A & B

  • The A - B is transformed to A & (~B)

  • The A ^ B is transformed to (A - B) | (B - A)

Operations between two positives simple shapes