Skip to content

Commit 257428d

Browse files
committed
Add primitive polar plot support
1 parent 3e3af57 commit 257428d

File tree

3 files changed

+69
-13
lines changed

3 files changed

+69
-13
lines changed

src/plot/util.typ

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
} else {
2121
max = calc.max(max, dom.at(i).at(1))
2222
}
23+
if min == max {
24+
min -= float-epsilon
25+
max += float-epsilon
26+
}
2327
ptx.axes.at(ax.name).auto-domain = (min, max)
2428
}
2529
return ptx

src/projection.typ

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#import "/src/cetz.typ": vector
12

23
/// Create a new cartesian projection between two vectors, low and high
34
///
@@ -23,19 +24,24 @@
2324
)
2425
}
2526

26-
/// - center (vector): Center vector
27-
/// - start (angle): Start angle (0deg for full circle)
27+
/// - low (vector): low vector
28+
/// - high (angle): Start angle (0deg for full circle)
2829
/// - stop (angle): Stop angle (360deg for full circle)
29-
/// - theta (axis): Theta axis
30-
/// - r (axis): R axis
30+
/// - axes (list): Axis array (angular, distal)
3131
/// -> function Transformation for one or more vectors
32-
#let polar(center, radius, start, stop, theta, r) = {
32+
#let polar(low, high, (angular, distal, ..), start: 0deg, stop: 360deg) = {
33+
let center = vector.lerp(low, high, .5)
34+
let radius = calc.min(..vector.sub(high, low).slice(0, 2)) / 2
35+
3336
return (
34-
axes: axes,
37+
axes: (angular, distal),
3538
transform: (..v) => {
36-
let v = v.pos()
37-
// TODO
38-
return v
39+
return v.pos().map(v => {
40+
let theta = (angular.transform)(angular, v.at(0), start, stop)
41+
let r = (distal.transform)(distal, v.at(1), 0, radius)
42+
43+
vector.add(center, (calc.cos(theta) * r, calc.sin(theta) * r, 0))
44+
})
3945
},
4046
)
4147
}

src/spine.typ

+50-4
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@
106106
),
107107
),
108108
),
109+
distal: (
110+
tick: (
111+
flip: true,
112+
label: (
113+
anchor: "east",
114+
)
115+
)
116+
),
109117
)
110118

111119
/// Default schoolbook style
@@ -160,9 +168,9 @@
160168

161169

162170
///
163-
#let cartesian-scientific(projections: none, style: (:)) = {
171+
#let cartesian-scientific(projections: none, name: none, style: (:)) = {
164172
return (
165-
name: none,
173+
name: name,
166174
draw: (ptx) => {
167175
let proj = projections.at(0)
168176
let axes = proj.axes
@@ -224,9 +232,9 @@
224232
}
225233

226234
///
227-
#let schoolbook(projections: none, zero: (0, 0), ..style) = {
235+
#let schoolbook(projections: none, name: none, zero: (0, 0), ..style) = {
228236
return (
229-
name: none,
237+
name: name,
230238
draw: (ptx) => {
231239
let proj = projections.at(0)
232240
let axes = proj.axes
@@ -288,3 +296,41 @@
288296
}
289297
)
290298
}
299+
300+
/// Polar frame
301+
#let polar(projections: none, name: none, ..style) = {
302+
assert(projections.len() == 1,
303+
message: "Unexpected number of projections!")
304+
305+
return (
306+
name: name,
307+
draw: (ptx) => {
308+
let proj = projections.first()
309+
let angular = proj.axes.at(0)
310+
let distal = proj.axes.at(1)
311+
312+
let (origin, outer) = (proj.transform)((0, distal.min), (0, distal.max))
313+
let radius = vector.dist(origin, outer)
314+
315+
let style = _prepare-style(ptx, cetz.styles.resolve(ptx.cetz-ctx.style,
316+
root: "axes", merge: style.named(), base: default-style))
317+
let angular-style = _get-axis-style(ptx, style, "angular")
318+
let distal-style = _get-axis-style(ptx, style, "distal")
319+
320+
let r-start = origin
321+
let r-end = vector.add(origin, (0, radius))
322+
draw.line(r-start, r-end, stroke: distal-style.stroke)
323+
if "computed-ticks" in distal {
324+
//ticks.draw-cartesian-grid(min-y, max-y, 1, y, y.computed-ticks, min-x, max-x, y-style)
325+
ticks.draw-cartesian(r-start, r-end, distal.computed-ticks, distal-style)
326+
}
327+
328+
let padding = angular-style.padding.first()
329+
draw.circle(origin, radius: radius + padding,
330+
stroke: angular-style.stroke)
331+
if "computed-ticks" in angular {
332+
// TODO
333+
}
334+
},
335+
)
336+
}

0 commit comments

Comments
 (0)