Skip to content

Rectangle

Rectangle

Bases: DrawingObject

A class to manage rectangles in a tikz environment

The Rectangle class is used to handle rectangles in TikZ. It is analagous to the TikZ code

\draw[<options>] <left_corner> rectangle <right_corner>;

Parameters:

Name Type Description Default
left_corner tuple or Point)

Position of the left corner

required
width float)

Rectangle width

required
height float)

Rectangle height

required
options str)

String containing the drawing options, e.g, ("Blue")

''
action str)

The type of TikZ action to use. Default is "draw".

'draw'

center property writable

center: Point

Returns the center of the rectangle.

east property writable

east: Point

Returns the east point of the rectangle.

left_corner property writable

left_corner: Point

Returns the left corner of the rectangle. This attribute is modifiable and can be set.

north property writable

north: Point

Returns the north point of the rectangle.

right_corner property

right_corner: Point

Returns the right corner of the rectangle.

south property writable

south: Point

Returns the south point of the rectangle.

west property writable

west: Point

Returns the west point of the rectangle.

Example

Rectangles are often used as a background to many figures; in this case, we create a fancy colored background.

from tikzpy import TikzPicture, Rectangle
import math

tikz = TikzPicture(center=True)

yellow_box: Rectangle = tikz.rectangle_from_center((0, 0), width=7, height=5, options="rounded corners, Yellow!30",action="filldraw")
# Params
r = 2
n_nodes = 7
nodes = []
# Draw the nodes
for i in range(1, n_nodes + 1):
    angle = 2 * math.pi * i / n_nodes
    x = r * math.cos(angle)
    y = r * math.sin(angle)
    node = tikz.node((x, y), text=f"$A_{{{i}}}$")
    nodes.append(node)

# Draw the lines between the nodes
for i in range(len(nodes)):
    start = nodes[i].position
    end = nodes[(i + 1) % len(nodes)].position
    tikz.line(start, end, options="->, shorten >= 10pt, shorten <=10pt")
tikz.show()