/
gr.satyh
205 lines (162 loc) · 7.18 KB
/
gr.satyh
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
@require: pervasives
@require: geom
@require: list
module Gr : sig
val rectangle : point -> point -> path
val rectangle-round : length -> point -> point -> path
val rectangle-round-left : length -> point -> point -> path
val rectangle-round-left-lower : length -> point -> point -> path
val rectangle-round-left-upper : length -> point -> point -> path
val rectangle-round-right : length -> point -> point -> path
val poly-line : point -> point list -> path
val polygon : point -> point list -> path
val line : point -> point -> path
val circle : point -> length -> path
val text-centering : point -> inline-boxes -> graphics
val text-leftward : point -> inline-boxes -> graphics
val text-rightward : point -> inline-boxes -> graphics
val arrow : length -> color -> length -> length -> length -> point -> point -> graphics list
val dashed-arrow : length -> length * length * length -> color -> length -> length -> length -> point -> point -> graphics list
val rotate-path : point -> float -> path -> path
val scale-path : point -> float -> float -> path -> path
val rotate-graphics : point -> float -> graphics -> graphics
val scale-graphics : point -> float -> float -> graphics -> graphics
end = struct
let rectangle (x1, y1) (x2, y2) =
start-path (x1, y1)
|> line-to (x1, y2)
|> line-to (x2, y2)
|> line-to (x2, y1)
|> close-with-line
let rectangle-round r (xA, yA) (xB, yB) =
let t = r *' 0.4 in
let x1 = length-min xA xB in
let x2 = length-max xA xB in
let y1 = length-min yA yB in
let y2 = length-max yA yB in
start-path (x1, y1 +' r)
|> bezier-to (x1, y1 +' t) (x1 +' t, y1) (x1 +' r, y1)
|> line-to (x2 -' r, y1)
|> bezier-to (x2 -' t, y1) (x2, y1 +' t) (x2, y1 +' r)
|> line-to (x2, y2 -' r)
|> bezier-to (x2, y2 -' t) (x2 -' t, y2) (x2 -' r, y2)
|> line-to (x1 +' r, y2)
|> bezier-to (x1 +' t, y2) (x1, y2 -' t) (x1, y2 -' r)
|> close-with-line
let rectangle-round-left r (xA, yA) (xB, yB) =
let t = r *' 0.4 in
let x1 = length-min xA xB in
let x2 = length-max xA xB in
let y1 = length-min yA yB in
let y2 = length-max yA yB in
start-path (x1, y1 +' r)
|> bezier-to (x1, y1 +' t) (x1 +' t, y1) (x1 +' r, y1)
|> line-to (x2, y1)
|> line-to (x2, y2)
|> line-to (x1 +' r, y2)
|> bezier-to (x1 +' t, y2) (x1, y2 -' t) (x1, y2 -' r)
|> close-with-line
let rectangle-round-left-lower r (xA, yA) (xB, yB) =
let t = r *' 0.4 in
let x1 = length-min xA xB in
let x2 = length-max xA xB in
let y1 = length-min yA yB in
let y2 = length-max yA yB in
start-path (x1, y1 +' r)
|> bezier-to (x1, y1 +' t) (x1 +' t, y1) (x1 +' r, y1)
|> line-to (x2, y1)
|> line-to (x2, y2)
|> line-to (x1, y2)
|> close-with-line
let rectangle-round-left-upper r (xA, yA) (xB, yB) =
let t = r *' 0.4 in
let x1 = length-min xA xB in
let x2 = length-max xA xB in
let y1 = length-min yA yB in
let y2 = length-max yA yB in
start-path (x1, y1)
|> line-to (x2, y1)
|> line-to (x2, y2)
|> line-to (x1 +' r, y2)
|> bezier-to (x1 +' t, y2) (x1, y2 -' t) (x1, y1 -' t)
|> close-with-line
let rectangle-round-right r (xA, yA) (xB, yB) =
let t = r *' 0.4 in
let x1 = length-min xA xB in
let x2 = length-max xA xB in
let y1 = length-min yA yB in
let y2 = length-max yA yB in
start-path (x1, y1)
|> line-to (x2 -' r, y1)
|> bezier-to (x2 -' t, y1) (x2, y1 +' t) (x2, y1 +' r)
|> line-to (x2, y2 -' r)
|> bezier-to (x2, y2 -' t) (x2 -' t, y2) (x2 -' r, y2)
|> line-to (x1, y2)
|> close-with-line
let poly-line ptinit ptlst =
ptlst |> List.fold-left (fun acc pt -> (
acc |> line-to pt
)) (start-path ptinit) |> terminate-path
let polygon ptinit ptlst =
ptlst |> List.fold-left (fun acc pt -> (
acc |> line-to pt
)) (start-path ptinit) |> close-with-line
let line pt1 pt2 =
start-path pt1 |> line-to pt2 |> terminate-path
let circle (cx, cy) r =
let t = r *' 0.55228 in
start-path (cx -' r, cy)
|> bezier-to (cx -' r, cy +' t) (cx -' t, cy +' r) (cx, cy +' r)
|> bezier-to (cx +' t, cy +' r) (cx +' r, cy +' t) (cx +' r, cy)
|> bezier-to (cx +' r, cy -' t) (cx +' t, cy -' r) (cx, cy -' r)
|> close-with-bezier (cx -' t, cy -' r) (cx -' r, cy -' t)
let text-centering pt ib =
let gr = draw-text pt ib in
let ((xmin, _), (xmax, _)) = get-graphics-bbox gr in
let wid = xmax -' xmin in
shift-graphics (0pt -' wid *' 0.5, 0pt) gr
let text-rightward =
draw-text
let text-leftward pt ib =
let gr = draw-text pt ib in
let ((xmin, _), (xmax, _)) = get-graphics-bbox gr in
let wid = xmax -' xmin in
shift-graphics (0pt -' wid, 0pt) gr
let arrow-scheme strokef color lenL lenM lenP ((x1, y1) as pt1) ((x2, y2) as pt2) =
let theta = Geom.atan2-point pt2 pt1 in
let (cx, cy) = (x2 +' lenL *' (cos theta), y2 +' lenL *' (sin theta)) in
let (mx, my) = (x2 +' lenM *' (cos theta), y2 +' lenM *' (sin theta)) in
let phi = theta +. math-pi /. 2. in
let (p1, q1) = (cx +' lenP *' (cos phi), cy +' lenP *' (sin phi)) in
let (p2, q2) = (cx -' lenP *' (cos phi), cy -' lenP *' (sin phi)) in
[
strokef color (line pt1 (mx, my));
fill color (polygon pt2 [(p1, q1); (mx, my); (p2, q2)]);
]
let arrow thkns =
arrow-scheme (stroke thkns)
let dashed-arrow thkns dash =
arrow-scheme (dashed-stroke thkns dash)
let rotate-path centpt angle path =
let (centx, centy) = centpt in
let rad = angle *. math-pi /. 180. in
path |> shift-path (0pt -' centx, 0pt -' centy)
|> linear-transform-path (cos rad) (0. -. (sin rad)) (sin rad) (cos rad)
|> shift-path centpt
let scale-path centpt scalex scaley path =
let (centx, centy) = centpt in
path |> shift-path (0pt -' centx, 0pt -' centy)
|> linear-transform-path scalex 0. 0. scaley
|> shift-path centpt
let rotate-graphics centpt angle gr =
let (centx, centy) = centpt in
let rad = angle *. math-pi /. 180. in
gr |> shift-graphics (0pt -' centx, 0pt -' centy)
|> linear-transform-graphics (cos rad) (0. -. (sin rad)) (sin rad) (cos rad)
|> shift-graphics centpt
let scale-graphics centpt scalex scaley gr =
let (centx, centy) = centpt in
gr |> shift-graphics (0pt -' centx, 0pt -' centy)
|> linear-transform-graphics scalex 0. 0. scaley
|> shift-graphics centpt
end