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, etcTransformations:
move,rotate,scaleandinvertBoolean operations:
add,sub,multandxor
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))
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))
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))
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)
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))
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)
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)
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)
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
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
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
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
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
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 + Bis transformed toA | BThe
A * Bis transformed toA & BThe
A - Bis transformed toA & (~B)The
A ^ Bis transformed to(A - B) | (B - A)