The shape table: each box shape's outline, for the painters that draw
strokes as geometry. mermaid.pixel builds a distance function from a
shape's description. mermaid.svg builds a path from it. One table holds
both, so the two pictures cannot drift apart.
A description is in the caller's units. The caller passes the centres of the box's border cells, already placed on its grid: the pixel painter snaps them to whole pixels, the SVG painter does not.
kind | Shapes | Fields |
|---|---|---|
box | rect, subroutine, round, stadium, circle, double_circle | cx, cy, hx, hy, rad; rules and inner for two of them |
polygon | rhombus, hexagon, lean_r, lean_l, trapezoid, trapezoid_alt | points, convex |
cylinder | cylinder | cx, a, half, lid_y, floor_y |
bar | bar | cx, cy, hx, hy, top, bottom |
A box is a rectangle of centre cx, cy and half sizes hx, hy, with
corners of radius rad. A subroutine's rules are the x of its two inner
rules. A double circle's inner is the second outline,
{ hx, hy, rad, gap }, gap inside the first.
A cylinder's lid is an ellipse of half axes a and half round
(cx, lid_y). Its floor is the lower half of the same ellipse round
(cx, floor_y). Two sides join them.
A bar is a rectangle 2 * hx wide, its stroke's outer edges on top and
bottom.
Each description but a cylinder's holds ix and iy: how far its
outline reaches in from the sides of its bounding box, along a row and
along a column.
| Name | Signature |
|---|---|
| rounded_box | rounded_box(cx, cy, hx, hy, rad) -> dist |
| polygon | polygon(points) -> dist |
| ellipse | ellipse(cx, cy, a, b) -> dist |
| describe | describe(shape, g) -> desc |
| distance | distance(desc) -> dist |
| meets | meets(desc) -> yes |
| reach | reach(dist, point, dx, dy, cw, ch) |
rounded_box(
cx,cy,hx,hy,rad) ->dist
Build the signed distance to a box with round corners
dist(px, py) is negative inside the box, nought on its outline.
polygon(
points) ->dist
Build the signed distance to a convex polygon
points are { x, y } pairs in either turn. The distance is the
greatest distance to one of the sides' lines. It is exact inside and
along a side, and gives a mitred corner outside.
ellipse(
cx,cy,a,b) ->dist
Build the distance to an ellipse, near its edge
The implicit function over its gradient's length: close to the true distance near the edge, which is where a stroke is drawn.
describe(
shape,g) ->desc
Describe a shape's outline for a box
g holds the box's measures in the caller's units:
| Field | Holds |
|---|---|
x0, x1, y0, y1 | the centres of the border cells: the first and last column, the first and last row |
ix0, ix1 | the centres of the second and the last-but-one column |
top, bottom | the box's outer edges: its first row's top, its last row's bottom |
cw, ch | a cell's size |
sw | a stroke's width |
unit | the measure a round corner is sized by |
The shape must be one mermaid.frame.check takes.
distance(
desc) ->dist
Build a description's distance function
Negative inside the outline, nought on it. A cylinder has none: its outline is two ellipses and two sides, and a painter draws each apart.
meets(
desc) ->yes
Report whether a route that ends on a box is carried to its outline
True for a box and a polygon. A route that ends on a cylinder or a
bar stops at the border cell's centre.
reach(
dist,point,dx,dy,cw,ch)
Carry a route's end on to an outline
A route ends at the centre of a border cell, and a side that leans or
curves runs inside that centre. point is the end, { x, y }, reached
in direction (dx, dy). It is moved on in steps of half a unit, up to
two cells along a row or one along a column, to the first point not
outside the outline. Where none is found it stays.