-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpaths.ml
335 lines (324 loc) · 12.6 KB
/
paths.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
(*---------------------------------------------------------------------------
Copyright (c) 2013 The vg programmers. All rights reserved.
SPDX-License-Identifier: ISC
---------------------------------------------------------------------------*)
open Gg
open Vg
;;
(** Test images for path areas. *)
Db.image "path-sq-outline" __POS__ ~author:Db.dbuenzli
~title:"Square outline in gray"
~tags:["path"]
~note:"Line width is 0.1 as we are on the surface edge."
~size:(Size2.v 50. 50.)
~view:Box2.unit
begin fun _ ->
let square = P.empty |> P.rect (Box2.v P2.o (Size2.v 1. 1.)) in
let area = `O { P.o with P.width = 0.2 } in
I.const (Color.gray 0.3) |> I.cut ~area square
end;
Db.image "path-sq-outline-dashed" __POS__ ~author:Db.dbuenzli
~title:"Dashed square outline in gray"
~tags:["path"; "dashes";]
~note:"Line width is 0.1 as we are on the surface edge."
~size:(Size2.v 50. 50.)
~view:Box2.unit
begin fun _ ->
let square = P.empty |> P.rect (Box2.v P2.o (P2.v 1. 1.)) in
let area = `O { P.o with P.width = 0.2; dashes = Some (0., [0.05]); } in
I.const (Color.gray 0.3) |> I.cut ~area square
end;
Db.image "path-cubics" __POS__ ~author:Db.dbuenzli
~title:"Cubic paths cases"
~tags:["path"]
~note:"Geometric cases for cubic curves. Except in the bottom row, only \
the end point moves."
~size:(Size2.v 115. 105.)
~view:(Box2.v (P2.v (-0.75) (0.625)) (Size2.v 5.75 5.25))
begin fun _ ->
let square = P.empty |> P.rect (Box2.v_mid P2.o (Size2.v 0.06 0.06)) in
let lgray = Color.gray 0.5 |> I.const in
let mgray = Color.gray 0.3 |> I.const in
let dgray = Color.gray 0.1 |> I.const in
let blue = Color.blue |> I.const in
let ctrl_pt pt = blue |> I.cut square |> I.move pt in
let end_pt pt = dgray |> I.cut square |> I.move pt in
let tangent p0 p1 =
let t = P.empty |> P.sub p0 |> P.line p1 in
lgray |> I.cut ~area:(`O { P.o with P.width = 0.01 }) t
in
let cubic ~at p0 c0 c1 p1 =
let curve = P.empty |> P.sub p0 |> P.ccurve c0 c1 p1 in
mgray |> I.cut ~area:(`O { P.o with P.width = 0.02 }) curve |>
I.blend (tangent p0 c0) |> I.blend (tangent p1 c1) |>
I.blend (ctrl_pt c0) |> I.blend (ctrl_pt c1) |>
I.blend (end_pt p0) |> I.blend (end_pt p1) |>
I.move at
in
let p0 = P2.v 0.0 0.5 in
let c1 = P2.v 0.3 1.5 in
let c2 = P2.v 1.1 0.9 in
let pa = P2.v 1.5 0.0 in
let pb = P2.v (0.8) 1.8 in
let pc = P2.v (-0.7) 0.7 in
let pd = P2.v (-0.4) 1.2 in
let b00 = cubic ~at:(P2.v 0.00 4.00) p0 c1 c2 pa in
let b01 = cubic ~at:(P2.v 2.85 3.60) p0 c1 c2 pb in
let b10 = cubic ~at:(P2.v 0.50 1.75) p0 c1 c2 pc in
let b11 = cubic ~at:(P2.v 3.15 1.75) p0 c1 c2 pd in
let p0 = P2.o in
let c1 = P2.v 0.3 0.0 in
let c2 = P2.v 1.2 0.0 in
let p1 = P2.v 1.5 0.0 in
let b20 = cubic ~at:(P2.v (-0.1) 1.25) p0 c1 c2 p1 in
let c1 = P2.v (-. 0.3) 0.0 in
let c2 = P2.v 1.8 0.0 in
let b21 = cubic ~at:(P2.v 2.7 1.25) p0 c1 c2 p1 in
b00 |> I.blend b01 |> I.blend
b10 |> I.blend b11 |> I.blend
b20 |> I.blend b21
end;
Db.image "paths-smooths" __POS__
~author:("François Thiré", "mailto:[email protected]")
~title:"Smooth paths"
~tags:["path"]
~note:"Geometric cases for smooth paths"
~size:(Size2.v 115. 105.)
~view:(Box2.v (P2.v (-0.75) (0.625)) (Size2.v 5.75 5.25))
begin fun _ ->
let square = P.empty |> P.rect (Box2.v_mid P2.o (Size2.v 0.06 0.06)) in
let lgray = Color.gray 0.5 |> I.const in
let mgray = Color.gray 0.3 |> I.const in
let dgray = Color.gray 0.1 |> I.const in
let blue = Color.blue |> I.const in
let red = Color.red |> I.const in
let ctrl_pt pt = blue |> I.cut square |> I.move pt in
let smooth_pt pt =
red |> I.cut square |> I.move pt in
let end_pt pt = dgray |> I.cut square |> I.move pt in
let tangent p0 p1 =
let t = P.empty |> P.sub p0 |> P.line p1 in
lgray |> I.cut ~area:(`O { P.o with P.width = 0.01 }) t
in
let t = 0.55191502449 in
let smooth_cubic ?(rel=false) ~at p0 c0 c1 p1 c2 p2 =
let curve = P.empty |> P.sub p0 |>
P.ccurve ~rel c0 c1 p1 |> P.smooth_ccurve ~rel c2 p2
in
let smooth = P2.tr (M3.rot2 ~pt:p1 Float.pi) c1 in
let c0 = if rel then V2.(c0 + p0) else c0 in
let c1 = if rel then V2.(c1 + p0) else c1 in
let p1 = if rel then V2.(p1 + p0) else p1 in
let c2 = if rel then V2.(c2 + p1) else c2 in
let p2 = if rel then V2.(p2 + p1) else p2 in
mgray |> I.cut ~area:(`O { P.o with P.width = 0.02 }) curve |>
I.blend (tangent p0 c0) |> I.blend (tangent p1 c1) |>
I.blend (ctrl_pt c0) |> I.blend (ctrl_pt c1) |>
I.blend (end_pt p0) |> I.blend (end_pt p1) |>
I.blend (smooth_pt smooth) |> I.blend (tangent p1 smooth) |>
I.blend (ctrl_pt c2) |> I.blend (tangent p2 c2) |>
I.blend (end_pt p2) |> I.move at
in
let smooth_quadratic ?(rel=false) ~at p0 c0 p1 p2 =
let curve = P.empty |> P.sub p0 |>
P.qcurve ~rel c0 p1 |> P.smooth_qcurve ~rel p2
in
let smooth = P2.tr (M3.rot2 ~pt:p1 Float.pi) c0 in
let c0 = if rel then V2.(c0 + p0) else c0 in
let p1 = if rel then V2.(p1 + p0) else p1 in
let p2 = if rel then V2.(p2 + p1) else p2 in
mgray |> I.cut ~area:(`O { P.o with P.width = 0.02 }) curve |>
I.blend (tangent p0 c0) |> I.blend (tangent p1 c0) |>
I.blend (ctrl_pt c0) |> I.blend (ctrl_pt c0) |>
I.blend (end_pt p0) |> I.blend (end_pt p1) |>
I.blend (smooth_pt smooth) |> I.blend (tangent p1 smooth) |>
I.blend (tangent p2 smooth) |>
I.blend (end_pt p2) |> I.move at
in
let tr p =
p |> P2.tr (M3.rot2 (-. Float.pi_div_4)) |> P2.tr (M3.scale2 (V2.v 0.75 0.50))
in
let p0 = P2.v 0.0 0.0 in
let c0 = P2.v 0.0 t in
let c1 = P2.v (1. -. t) 1. in
let p1 = P2.v 1. 1. in
let c2 = P2.v 2. t in
let p2 = P2.v 2. 0. in
let b00 = smooth_cubic ~at:(P2.v 0.00 4.00) p0 c0 c1 p1 c2 p2 in
let p0 = tr p0 in
let c0 = tr c0 in
let c1 = tr c1 in
let p1 = tr p1 in
let c2 = tr c2 in
let p2 = tr p2 in
let b01 = smooth_cubic ~at:(P2.v 2.5 5.) p0 c0 c1 p1 c2 p2 in
let p0 = P2.v 0. 0. in
let c0 = P2.v 0. 1. in
let p1 = P2.v 1. 1. in
let p2 = P2.v 2. 0. in
let b10 = smooth_quadratic ~at:(P2.v 0. 2.5) p0 c0 p1 p2 in
let p0 = tr p0 in
let c0 = tr c0 in
let p1 = tr p1 in
let p2 = tr p2 in
let b11 = smooth_quadratic ~at:(P2.v 2.5 3.5) p0 c0 p1 p2 in
let q0 = P2.v 0.0 0.0 in
let d0 = P2.v 0.0 t in
let d1 = P2.v (1. -. t) 1. in
let q1 = P2.v 1. 1. in
let d2 = P2.v 1. (-. (1. -. t)) in
let q2 = P2.v 1. (-. 1.) in
let b20 = smooth_cubic ~rel:true ~at:(P2.v 0. 1.5) q0 d0 d1 q1 d2 q2 in
let q0 = P2.v 0. 0. in
let d0 = P2.v 0. 1. in
let q1 = P2.v 1. 1. in
let q2 = P2.v 1. (-. 1.) in
let b21 = smooth_quadratic ~rel:true ~at:(P2.v 2.5 1.5) q0 d0 q1 q2 in
(* let b01 = cubic ~at:(P2.v 2.85 3.60) p0 c1 c2 pb in *)
(* let b10 = cubic ~at:(P2.v 0.50 1.75) p0 c1 c2 pc in *)
(* let b11 = cubic ~at:(P2.v 3.15 1.75) p0 c1 c2 pd in *)
b00 |> I.blend b01 |> I.blend b10 |> I.blend b11 |> I.blend b20 |> I.blend b21
end;
Db.image "path-dashes" __POS__ ~author:Db.dbuenzli
~title:"Dash patterns"
~tags:["path"; "dashes";]
~note:"Miscellaneous dash patterns and offsets. "
~size:(Size2.v 100. 100.)
~view:(Box2.v P2.o (Size2.v 26. 26.))
begin fun _ ->
let path = P.empty |> P.sub (P2.v 1. 0.) |> P.line (P2.v 25. 0.) in
let line y d =
let area = `O { P.o with P.dashes = Some d } in
Color.gray 0.3 |> I.const |> I.cut ~area path |> I.move (V2.v 0. y)
in
line 25. (0., []) |>
I.blend (line 23. (0., [1.])) |>
I.blend (line 21. (1., [1.])) |>
I.blend (line 19. (0., [2.])) |>
I.blend (line 17. (1., [2.])) |>
I.blend (line 15. (1., [2.; 1.])) |>
I.blend (line 13. (6., [3.; 5.])) |>
I.blend (line 11. (0., [2.; 3.])) |>
I.blend (line 9. (11., [2.; 3.])) |>
I.blend (line 7. (0., [2.; 1.; 3.])) |>
I.blend (line 5. (0., [2.; 1.; 3.; 2.])) |>
I.blend (line 3. (1., [1.; 5.])) |>
I.blend (line 1. (0., [5.; 1.]))
end;
Db.image "path-cantor-dashes" __POS__ ~author:Db.dbuenzli
~title:"Cantor set with dashes"
~tags:["path"; "fractal"; "dashes";]
~note:"The Cantor set is drawn with dashes to represent its elements. \
Maximal dash pattern size is a largely undocumented parameter of \
the renderer backends, the line renderings may quickly become \
incorrect (e.g. ghostscript says the file is broken)."
~size:(Size2.v 120. 90.)
~view:(Box2.v P2.o (Size2.v 1.2 0.9))
begin fun _ ->
(* Cantor set http://mathworld.wolfram.com/CantorSet.html *)
let unit = P.empty |> P.line V2.ox in
let o = { P.o with P.width = 0.05 } in
let cantor max =
let rec split odd acc = function
| [] -> acc
| d :: l ->
if odd then split false (d :: acc) l else
let d' = d /. 3. in
split true (d' :: d' :: d' :: acc) l
in
let rec loop level i dashes =
if level < 0 then i else
let pat = (* only half + 1 of the dashes are needed. *)
let rec keep c l acc =
if c = 0 then List.rev acc else
keep (c - 1) (List.tl l) ((List.hd l) :: acc)
in
keep (List.length dashes / 2 + 1) dashes []
in
let area = `O { o with P.dashes = Some (0., pat); } in
let i' =
I.const (Color.gray 0.3) |> I.cut ~area unit |>
I.move (P2.v 0. (0.1 *. float level)) |> I.blend i
in
loop (level - 1) i' (split false [] dashes)
in
loop max I.void [ 1. ]
in
cantor 6 |> I.move (P2.v 0.1 0.15)
end;
Db.image "path-derived" __POS__ ~author:Db.dbuenzli
~title:"Derived subpath of Vg.P"
~tags:["path";]
~note:"From inward to outward, ellipse, circle, rectangle, rectangle \
with elliptic corners."
~size:(Size2.v 50. 50.)
~view:(Box2.v P2.o (Size2.v 1. 1.))
begin fun _ ->
let c = P2.v 0.5 0.5 in
let p =
P.empty |>
P.ellipse c (V2.v 0.1 0.2) |>
P.circle c 0.25 |>
P.rect (Box2.v (P2.v 0.2 0.15) (Size2.v 0.6 0.7)) |>
P.rrect (Box2.v (P2.v 0.1 0.05) (Size2.v 0.8 0.9)) (Size2.v 0.2 0.1)
in
let area = `O { P.o with P.width = 0.01 } in
I.const (Color.gray 0.3) |> I.cut ~area p
end;
Db.image "path-miter-angle" __POS__ ~author:Db.dbuenzli
~title:"Miter angle limit"
~tags:["path";]
~note:"In the left column the miter angle is set below the angle made by \
the path, all joins should be pointy. In the right column the miter \
angle is set above the angle made by the path, all joins should be \
bevelled."
~size:(Size2.v 120. 570.)
~view:(Box2.v P2.o (Size2.v 4.0 19.0))
begin fun _ ->
let gray = I.const (Color.gray 0.3) in
let white = I.const Color.white in
let wedge a =
P.empty |> P.sub (V2.polar 0.6 a) |> P.line P2.o |> P.line (V2.v 0.6 0.)
in
let path x y miter_angle a =
let area = (`O { P.o with P.width = 0.1; miter_angle }) in
let wedge = wedge a in
let outline = I.cut ~area wedge gray in
let data = I.cut ~area:(`O { P.o with P.width = 0.01 }) wedge white in
outline |> I.blend data |> I.move (P2.v x (y +. 0.2))
in
let acc = ref I.void in
for i = 0 to 18 do
let y = float i in
let base = y *. 10. in
let a = Float.rad_of_deg base in
let less = Float.rad_of_deg (base -. 1.) in
let more = Float.rad_of_deg (base +. 1.) in
acc := !acc |> I.blend (path 1. y less a) |> I.blend (path 3. y more a)
done;
!acc
end;
Db.image "path-circle-ellipse" __POS__ ~author:Db.dbuenzli
~note:"Shows dilation on line width due to scaling \
(from Christophe Troestler ocaml-cairo example tips_ellipse.ml).
The form on the left is a scaled circle. The form on the right is
an ellipse."
~title:"Line width dilation"
~tags:["path"]
~size:(Size2.v 120. 120.)
~view:(Box2.v P2.o (Size2.v 1. 1.))
begin fun _ ->
let circle =
let circle = P.(empty |> circle (P2.v 0.5 0.5) 0.4) in
let area = `O { P.o with P.width = 0.1 } in
I.cut ~area circle (I.const Color.black) |>
I.scale (Size2.v 0.5 1.)
in
let ellipse =
let ellipse = P.(empty |> ellipse (P2.v 0.5 0.5) (Size2.v 0.2 0.4)) in
let area = `O { P.o with P.width = 0.1 } in
I.cut ~area ellipse (I.const Color.black) |>
I.move (V2.v 0.25 0.)
in
I.blend circle ellipse
end