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
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'
|
left_corner
property
writable
Returns the left corner of the rectangle. This attribute is modifiable and can be set.
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()