Shapes
In [1]:
Copied!
from chalk.core import BaseDiagram
from chalk import *
def help(f):
import pydoc
from IPython.display import HTML
return HTML(pydoc.HTMLDoc().docroutine(f))
from chalk.core import BaseDiagram from chalk import * def help(f): import pydoc from IPython.display import HTML return HTML(pydoc.HTMLDoc().docroutine(f))
Elementary diagrams can be created using shapes: polygons, circle-like shapes, text and paths.
Polygons¶
Triangle¶
In [2]:
Copied!
help(triangle)
help(triangle)
Out[2]:
- triangle(width: float) -> chalk.types.Diagram
- Draws an equilateral triangle with the side length specified by
the ``width`` argument. The origin is the traingle's centroid.
In [3]:
Copied!
triangle(1)
triangle(1)
Out[3]:
Square and rectangle¶
In [4]:
Copied!
help(square)
help(square)
Out[4]:
- square(side: float) -> chalk.types.Diagram
- Draws a square with the specified side length. The origin is the
center of the square.
In [5]:
Copied!
square(1)
square(1)
Out[5]:
In [6]:
Copied!
help(rectangle)
help(rectangle)
Out[6]:
- rectangle(width: float, height: float, radius: Optional[float] = None) -> chalk.types.Diagram
- Draws a rectangle.
Args:
width (float): Width
height (float): Height
radius (Optional[float]): Radius for rounded corners.
Returns:
Diagrams
In [7]:
Copied!
rectangle(3, 1, 0.0)
rectangle(3, 1, 0.0)
Out[7]:
Regular polygon¶
In [8]:
Copied!
help(regular_polygon)
help(regular_polygon)
Out[8]:
- regular_polygon(sides: int, side_length: float) -> chalk.types.Diagram
- Draws a regular polygon with given number of sides and given side
length. The polygon is oriented with one edge parallel to the x-axis.
In [9]:
Copied!
hcat(
[
regular_polygon(5, 1),
regular_polygon(6, 1),
regular_polygon(7, 1),
],
sep=0.5,
)
hcat( [ regular_polygon(5, 1), regular_polygon(6, 1), regular_polygon(7, 1), ], sep=0.5, )
Out[9]:
Circle-like shapes¶
Circle¶
In [10]:
Copied!
help(circle)
help(circle)
Out[10]:
- circle(radius: float) -> chalk.types.Diagram
- Draws a circle with the specified ``radius``.
In [11]:
Copied!
circle(1)
circle(1)
Out[11]:
Arc¶
Arcs can be specified either using angles (see arc
) or points (see arc_between
).
In [12]:
Copied!
help(arc)
help(arc)
Out[12]:
- arc(radius: float, angle0: float, angle1: float) -> chalk.types.Diagram
- Draws an arc.
Args:
radius (float): Circle radius.
angle0 (float): Starting cutoff in degrees.
angle1 (float): Finishing cutoff in degrees.
Returns:
Diagram
In [13]:
Copied!
quarter = 90
arc(1, 0, quarter)
quarter = 90 arc(1, 0, quarter)
Out[13]:
In [14]:
Copied!
arc(1, 0, quarter) + arc(1, 2 * quarter, 3 * quarter)
arc(1, 0, quarter) + arc(1, 2 * quarter, 3 * quarter)
Out[14]:
In [15]:
Copied!
help(arc_between)
help(arc_between)
Out[15]:
- arc_between(point1: Union[planar.vector.Vec2, Tuple[float, float]], point2: Union[planar.vector.Vec2, Tuple[float, float]], height: float) -> chalk.types.Diagram
- Makes an arc starting at point1 and ending at point2, with the midpoint
at a distance of abs(height) away from the straight line from point1 to
point2. A positive value of height results in an arc to the left of the
line from point1 to point2; a negative value yields one to the right.
The implementaion is based on the the function arcBetween from Haskell's
diagrams:
https://hackage.haskell.org/package/diagrams-lib-1.4.5.1/docs/src/Diagrams.TwoD.Arc.html#arcBetween
In [16]:
Copied!
arc_between((0, 0), (1, 0), 1)
arc_between((0, 0), (1, 0), 1)
Out[16]:
Text¶
In [17]:
Copied!
help(text)
help(text)
Out[17]:
- text(t: str, size: Optional[float]) -> chalk.types.Diagram
- Draw some text.
Args:
t (str): The text string.
size (Optional[float]): Size of the text.
Returns:
Diagram
Note that unlike other shapes, text
has an empty envelope, so we need to explicitly specify it in order to get a non-empty rendering.
In [18]:
Copied!
text("hello", 1).with_envelope(rectangle(2.5, 1))
text("hello", 1).with_envelope(rectangle(2.5, 1))
Out[18]:
Paths¶
In [19]:
Copied!
help(make_path)
help(make_path)
Out[19]:
- make_path(segments: 'List[Tuple[float, float]]', closed: 'bool' = False) -> 'Diagram'
In [20]:
Copied!
make_path([(0, 0), (0, 1), (1, 1), (1, 2)])
make_path([(0, 0), (0, 1), (1, 1), (1, 2)])
Out[20]:
Last update: 2022-08-21