|
27 | 27 | /// circle((0,0)) |
28 | 28 | /// // Draws an ellipse |
29 | 29 | /// circle((0,-2), radius: (0.75, 0.5)) |
| 30 | +/// // Draws a circle at (0, 2) through point (1, 3) |
| 31 | +/// circle((0,2), (rel: (1,1))) |
30 | 32 | /// ``` |
31 | 33 | /// |
32 | | -/// - position (coordinate): The position to place the circle on. |
| 34 | +/// - ..points-style (coordinate, style): The position to place the circle on. |
| 35 | +/// If given two coordinates, the distance between them is used as radius. |
| 36 | +/// If given a single coordinate, the radius can be set via the `radius` (style) |
| 37 | +/// argument. |
33 | 38 | /// - name (none,str): |
34 | 39 | /// - anchor (none, str): |
35 | | -/// - ..style (style): |
36 | 40 | /// |
37 | 41 | /// ### Styling |
38 | 42 | /// *Root*: `circle` |
|
42 | 46 | /// ### Anchors |
43 | 47 | /// Supports border and path anchors. The `"center"` anchor is the default. |
44 | 48 | /// |
45 | | -#let circle(position, name: none, anchor: none, ..style) = { |
46 | | - // No extra positional arguments from the style sink |
47 | | - assert.eq( |
48 | | - style.pos(), |
49 | | - (), |
50 | | - message: "Unexpected positional arguments: " + repr(style.pos()), |
51 | | - ) |
52 | | - let style = style.named() |
| 49 | +#let circle(..points-style, name: none, anchor: none) = { |
| 50 | + let style = points-style.named() |
| 51 | + let points = points-style.pos() |
| 52 | + assert(points.len() in (1, 2), |
| 53 | + message: "circle expects one or two points, got " + repr(points)) |
| 54 | + assert(points.len() != 2 or "radius" not in style, |
| 55 | + message: "unexpected radius for circle constructed by two points") |
53 | 56 |
|
54 | 57 | (ctx => { |
55 | | - let (ctx, pos) = coordinate.resolve(ctx, position) |
| 58 | + let (center, outer) = if points.len() == 1 { |
| 59 | + (points.at(0), none) |
| 60 | + } else { |
| 61 | + points |
| 62 | + } |
| 63 | + |
| 64 | + let (ctx, center) = coordinate.resolve(ctx, center) |
56 | 65 | let style = styles.resolve(ctx.style, merge: style, root: "circle") |
57 | | - let (rx, ry) = util.resolve-radius(style.radius).map(util.resolve-number.with(ctx)) |
58 | | - let (cx, cy, cz) = pos |
| 66 | + |
| 67 | + // If we got two points, use the second one to calculate |
| 68 | + // the radius. |
| 69 | + let (rx, ry) = if outer != none { |
| 70 | + (ctx, outer) = coordinate.resolve(ctx, outer, update: false) |
| 71 | + (vector.dist(center, outer),) * 2 |
| 72 | + } else { |
| 73 | + util.resolve-radius(style.radius).map(util.resolve-number.with(ctx)) |
| 74 | + } |
| 75 | + let (cx, cy, cz) = center |
59 | 76 |
|
60 | 77 | let drawables = drawable.ellipse( |
61 | 78 | cx, cy, cz, |
|
65 | 82 | ) |
66 | 83 |
|
67 | 84 | let (transform, anchors) = anchor_.setup( |
68 | | - (_) => pos, |
| 85 | + (_) => center, |
69 | 86 | ("center",), |
70 | 87 | default: "center", |
71 | 88 | name: name, |
|
0 commit comments