View Source Tucan.Geometry (tucan v0.3.0)
An API for drawing common geometrical shapes.
Tucan.Geometry
provides an API for drawing common geometrical shapes. These among other include
lines, circles and polygons. In contrary to the Tucan
common API, these functions instead of a
dataset, expect the that define the underlying geometrical shapes.
You can overlay tucan plots with geometrical shapes through the Tucan.layers/2
function.
Examples
Tucan.layers([
Tucan.Geometry.circle({3, 3}, 4, line_color: "red", stroke_width: 3),
Tucan.Geometry.rectangle({-2, 10}, {7, -3}, line_color: "green"),
Tucan.Geometry.rectangle({-3.5, 0.1}, {8.1, -4.2},
fill_color: "pink",
fill_opacity: 0.3
),
Tucan.Geometry.polyline([{1, 1}, {2, 7}, {5, 3}],
closed: true,
fill_color: "green",
fill_opacity: 0.3
)
])
|> Tucan.Scale.set_xy_domain(-5, 11)
|> Tucan.set_size(400, 400)
|> Tucan.set_title("Tucan.Geometry examples")
Summary
Functions
Draws a circle with the given center
and radius
.
Draws a polyline defined by the given vertices.
Draws a rectangle defined by the given upper_left
and bottom_right
points.
Types
A cartesian point in the form {x, y}
Functions
@spec circle(center :: point(), radius :: number(), opts :: keyword()) :: VegaLite.t()
Draws a circle with the given center
and radius
.
The circle will be added as a new layer to the given plot vl
.
Options
Styling Options
:fill_color
(String.t/0
) - The fill color of the marks. This will override thecolor
encoding if set.:fill_opacity
(number/0
) - The fill opacity of the plotted elements. The default value is1
.:line_color
(String.t/0
) - The color of the line:opacity
(number/0
) - The overall opacity of the mark:stroke_dash
(list ofpos_integer/0
) - An array of alternating stroke, space lengths in pixels for creating dashed or dotted lines.:stroke_opacity
(number/0
) - The opacity of the stroke. The default value is1
.:stroke_width
(pos_integer/0
) - The stroke width in pixels The default value is1
.
Examples
Tucan.layers([
Tucan.Geometry.circle({3, 2}, 5),
Tucan.Geometry.circle({-1, 6}, 2, line_color: "red"),
Tucan.Geometry.circle({0, 1}, 4, line_color: "green", stroke_width: 5)
])
|> Tucan.Scale.set_x_domain(-5, 10)
|> Tucan.Scale.set_y_domain(-5, 10)
You can also draw filled circles by setting the :fill_color
option. Opacity of the fill color and
the stroke color can be configured by :opacity
or independently by :fill_opacity
and
:stroke_opacity
.
Tucan.layers([
Tucan.Geometry.circle({3, 2}, 5, stroke_width: 3, stroke_opacity: 0.4),
Tucan.Geometry.circle({-1, 6}, 2, line_color: "red", fill_color: "pink", opacity: 0.3),
Tucan.Geometry.circle({0, 1}, 4,
line_color: "green",
stroke_width: 5,
fill_color: "green",
fill_opacity: 0.2
)
])
|> Tucan.Scale.set_x_domain(-5, 10)
|> Tucan.Scale.set_y_domain(-5, 10)
Circles and plot dimensions
Notice that the plot must be square with identical scale domains across the two axes for the circle to appear circular. In a different case it will look like an ellipsis.
circle = Tucan.Geometry.circle({0, 0}, 1) Tucan.hconcat([ circle |> Tucan.set_size(150, 150) |> Tucan.set_title("Square frame"), circle |> Tucan.Scale.set_x_domain(-2, 2) |> Tucan.Scale.set_y_domain(-1, 1) |> Tucan.set_size(150, 150) |> Tucan.set_title("Different domains"), circle |> Tucan.set_size(200, 150) |> Tucan.set_title("Different frame dimensions") ])
@spec polyline(vertices :: [point()], opts :: keyword()) :: VegaLite.t()
Draws a polyline defined by the given vertices.
The order of the vertices defines the order of the line segments that will be generated.
The polyline or polygon will be added as a new layer to the given plot vl
.
Options
:closed
(boolean/0
) - Whether a last segment will be added between the last and first points. The default value isfalse
.
Styling Options
:fill_color
(String.t/0
) - The fill color of the marks. This will override thecolor
encoding if set.:fill_opacity
(number/0
) - The fill opacity of the plotted elements. The default value is1
.:line_color
(String.t/0
) - The color of the line:opacity
(number/0
) - The overall opacity of the mark:stroke_dash
(list ofpos_integer/0
) - An array of alternating stroke, space lengths in pixels for creating dashed or dotted lines.:stroke_opacity
(number/0
) - The opacity of the stroke. The default value is1
.:stroke_width
(pos_integer/0
) - The stroke width in pixels The default value is1
.
Examples
Tucan.Geometry.polyline([{-1, 1}, {-2, 4}, {-1, 3}, {4, 7}, {8, 2}])
|> Tucan.Scale.set_x_domain(-3, 10)
|> Tucan.Scale.set_y_domain(-1, 9)
If :closed
is set to true
a line segment is added between the last and
first point.
Tucan.Geometry.polyline([{-1, 1}, {-2, 4}, {-1, 3}, {4, 7}, {8, 2}], closed: true)
|> Tucan.Scale.set_x_domain(-3, 10)
|> Tucan.Scale.set_y_domain(-1, 9)
You can change the appearance of the polyline/polygon.
points = [{-1, 1}, {-2, 4}, {-1, 3}, {4, 7}, {8, 2}]
Tucan.Geometry.polyline(points,
closed: true,
fill_color: "red",
line_color: "green",
fill_opacity: 0.3,
stroke_width: 3,
stroke_dash: [5, 3]
)
|> Tucan.Scale.set_x_domain(-3, 10)
|> Tucan.Scale.set_y_domain(-1, 9)
@spec rectangle(point1 :: point(), point2 :: point(), opts :: keyword()) :: VegaLite.t()
Draws a rectangle defined by the given upper_left
and bottom_right
points.
Options
See polyline/2
Examples
Tucan.layers([
Tucan.Geometry.rectangle({1, 5}, {5, 1},
line_color: "black",
stroke_width: 2,
stroke_dash: [5, 5]
),
Tucan.Geometry.rectangle({-2, 10}, {7, -3}, line_color: "green"),
Tucan.Geometry.rectangle({-3.5, 0.1}, {8.1, -4.2}, fill_color: "pink", fill_opacity: 0.3)
])
|> Tucan.Scale.set_xy_domain(-5, 11)
|> Tucan.set_size(400, 300)