Skip to content

Polygon

Polygons are closed shapes, drawn using two parallel lists of x and y coordinates. Unlike a Polyline, the shape is closed (the last corner joins back to the first).

Creating a Polygon

You can create a Polygon using the following functions:

Polygon(xPoints, yPoints)
Polygon(xPoints, yPoints, color, fill, thickness, rotation, visibility)
Parameter Type Default Description
xPoints list[int or float] required The horizontal positions of the corners, in pixels.
yPoints list[int or float] required The vertical positions of the corners, in pixels.
color Color Color.BLACK The color.
fill bool False Whether the polygon is filled in (True) or just an outline (False).
thickness int 1 The outline thickness, in pixels.
rotation int or float 0 How far to turn the polygon, in degrees, counter-clockwise.
visibility int 100 How visible the polygon is, from 0 (invisible) to 100 (fully visible).

For example,

polygon = Polygon([312, 366, 510, 443], [244, 210, 312, 346])

Once created, you can add it to a Display using the Display's add() function.

Functions

Once a Polygon polygon has been created, the following functions are available:

Position

Function Description
polygon.getPosition() Return the object's position, the top-left corner of its bounding box.
polygon.getX() Return the object's horizontal position.
polygon.getY() Return the object's vertical position.
polygon.getCenter() Return the object's center point.
polygon.getCenterX() Return the object's horizontal center.
polygon.getCenterY() Return the object's vertical center.
polygon.setPosition(x, y) Move the object so the top-left corner of its bounding box sits at the given point.
polygon.setX(x) Set the object's horizontal position.
polygon.setY(y) Set the object's vertical position.
polygon.setCenter(x, y) Move the object so its center sits at the given point.
polygon.setCenterX(x) Set the object's horizontal center.
polygon.setCenterY(y) Set the object's vertical center.
polygon.move(x, y) Move the object to a new position.

Size

Function Description
polygon.getSize() Return the object's width and height.
polygon.getWidth() Return the object's width.
polygon.getHeight() Return the object's height.
polygon.setSize(width, height) Set the object's width and height.
polygon.setWidth(width) Set the object's width.
polygon.setHeight(height) Set the object's height.

Rotation

Function Description
polygon.getRotation() Return how far the object is turned.
polygon.setRotation(rotation) Turn the object to a given angle.
polygon.rotate(angle) Turn the object by an additional angle.

Visibility

Function Description
polygon.getVisibility() Return how visible the object is.
polygon.setVisibility(visibility) Set how visible the object is.

Color

Function Description
polygon.getColor() Return the shape's color.
polygon.getFill() Report whether the shape is filled in.
polygon.getThickness() Return the shape's outline thickness.
polygon.setColor() Set the shape's color.
polygon.setFill(fill) Set whether the shape is filled in.
polygon.setThickness(thickness) Set the shape's outline thickness.

Information

Function Description
polygon.getEndpoints() Return the objects's corners.
polygon.getBoundingBox() Return the smallest upright box that surrounds the object.
polygon.getGroup() Return the Group this object belongs to.
polygon.getDisplay() Return the Display this object is on.
polygon.setToolTipText() Set the hover text shown over the object.

Hit Testing

Function Description
polygon.contains(x, y) Report whether a point lies inside the polygon.
polygon.intersects(other) Report whether this object overlaps another.
polygon.encloses(other) Report whether this object completely contains another.

Events

Function Description
polygon.onMouseClick(action) Set up a function to call when the mouse is clicked on this object.
polygon.onMouseDown(action) Set up a function to call when the mouse button is pressed on this object.
polygon.onMouseUp(action) Set up a function to call when the mouse button is released over this object.
polygon.onMouseMove(action) Set up a function to call when the mouse moves over this object.
polygon.onMouseDrag(action) Set up a function to call when the mouse is dragged over this object.
polygon.onMouseEnter(action) Set up a function to call when the mouse moves onto this object.
polygon.onMouseExit(action) Set up a function to call when the mouse moves off this object.
polygon.onKeyType(action) Set up a function to call when a key is typed (pressed and released).
polygon.onKeyDown(action) Set up a function to call when a key is pressed down.
polygon.onKeyUp(action) Set up a function to call when a key is released.