-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patharches.c
270 lines (206 loc) · 7.22 KB
/
arches.c
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
/* arches --- plot various kinds of arches 2011-10-01 */
/* Copyright (c) 2011 John Honniball, Froods Software Development */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include "hpgllib.h"
void plot_ur(const double x0, const double y0, const double width, const double height, const double r1, const double r2);
void circulararch(const double xc, const double y0, const double yc, const double r);
void plot_ll(const double x0, const double y0, const double width, const double height, const double r1, const double r2);
void ellipticalarch(const double xc, const double y0, const double yc, const double a, const double b);
void plot_ul(const double x0, const double y0, const double width, const double height, const double r1, const double r2);
void threecentredarch(const double xc, const double y0, const double yc, const double d, const double r);
void half_ellipse(const double x0, const double y0, const double a, const double b, const double theta);
void plot_lr(const double x0, const double y0, const double width, const double height, const double r1, const double r2);
void pointedarch(const double xc, const double y0, const double yc, const double d1, const double d2);
int main(int argc, char * const argv[])
{
int opt;
double maxx, maxy;
double xc, yc;
double r1, r2;
while ((opt = getopt(argc, argv, "no:p:s:t:v:")) != -1) {
switch (opt) {
case 'n':
case 'o':
case 'p':
case 's':
case 't':
case 'v':
plotopt(opt, optarg);
break;
default: /* '?' */
fprintf(stderr, "Usage: %s [-p pen] [-s <size>] [-t title]\n", argv[0]);
fprintf(stderr, " <size> ::= A1 | A2 | A3 | A4 | A5\n");
exit(EXIT_FAILURE);
}
}
/* Select first pen and draw border */
if (plotbegin(1) < 0) {
fputs("Failed to initialise HPGL library\n", stderr);
exit(EXIT_FAILURE);
}
getplotsize(&maxx, &maxy);
xc = maxx / 2.0;
yc = maxy / 2.0;
r1 = maxx / 5.0;
r2 = maxy / 5.0;
/* Split page into quarters */
moveto(0.0, yc);
lineto(maxx, yc);
moveto(xc, 0.0);
lineto(xc, maxy);
/* Draw four arch plots */
plot_ll(0.0, 0.0, xc, yc, r1, r2);
plot_lr(xc, 0.0, xc, yc, r1, r2);
plot_ul(0.0, yc, xc, yc, r1, r2);
plot_ur(xc, yc, xc, yc, r1, r2);
plotend();
return (0);
}
void plot_ur(const double x0, const double y0, const double width, const double height, const double r1, const double r2)
{
const double xc = x0 + (width / 2.0);
const double yc = y0 + (height / 2.0);
/* Centre lines */
moveto(x0, yc);
lineto(x0 + width, yc);
moveto(xc, y0);
lineto(xc, y0 + height);
/* Circle forming arch, drawn as full circle */
circle(xc, yc, r2);
/* Thicker pen for outline of arch */
// pencolr(1);
circulararch(xc, y0, yc, r2);
circulararch(xc, y0, yc, r2 * 1.15);
}
void circulararch(const double xc, const double y0, const double yc, const double r)
{
moveto(xc - r, y0);
lineto(xc - r, yc);
/* The arch itself, a half circle */
arc(xc, yc, -180.0);
lineto(xc + r, y0);
}
void plot_ul(const double x0, const double y0, const double width, const double height, const double r1, const double r2)
{
/* r3: Distance from central axis to centre of smaller arcs */
/* r4: Radius of smaller arcs */
const double r3 = r1 / 2.0;
const double r4 = r2 / 2.0;
const double r5 = sqrt((r3 * r3) + (r3 * r3)) + r4; /* Radius of larger arc */
const double xc = x0 + (width / 2.0);
const double yc = y0 + (height / 2.0);
/* Centre lines */
moveto(x0, yc);
lineto(x0 + width, yc);
moveto(xc, y0);
lineto(xc, y0 + height);
/* LH centre mark */
moveto(xc - r3, yc + (3.0 * 40.0));
lineto(xc - r3, yc - (3.0 * 40.0));
/* LH small circle */
circle(xc - r3, yc, r4);
/* RH centre mark */
moveto(xc + r3, yc + (3.0 * 40.0));
lineto(xc + r3, yc - (3.0 * 40.0));
/* RH small circle */
circle(xc + r3, yc, r4);
/* 45 degree construction lines */
moveto(xc - (2.0 * r3), yc + r3);
lineto(xc, yc - r3);
lineto(xc + (2.0 * r3), yc + r3);
/* Bottom centre mark */
moveto(xc - (3.0 * 40.0), yc - r3);
lineto(xc + (3.0 * 40.0), yc - r3);
/* Upper large circle, only drawn as half-circle */
moveto(xc + r5, yc - r3);
arc(xc, yc - r3, 180.0);
/* Thicker pen for outline of arch */
// pencolr(1);
threecentredarch(xc, y0, yc, r3, r4);
threecentredarch(xc, y0, yc, r3, r4 * 1.3);
}
void threecentredarch(const double xc, const double y0, const double yc, const double d, const double r)
{
moveto(xc - (d + r), y0);
lineto(xc - (d + r), yc);
arc(xc - d, yc, -45.0);
arc(xc, yc - d, -90.0);
arc(xc + d, yc, -45.0);
lineto(xc + (d + r), y0);
}
void plot_ll(const double x0, const double y0, const double width, const double height, const double r1, const double r2)
{
const double xc = x0 + (width / 2.0);
const double yc = y0 + (height / 2.0);
const double thickness = r1 / 10.0;
/* Centre lines */
moveto(x0, yc);
lineto(x0 + width, yc);
moveto(xc, y0);
lineto(xc, y0 + height);
/* Ellipse forming arch, drawn in full */
ellipse(xc, yc, r1, r2, 0.0);
/* Thicker pen for outline of arch */
// pencolr(1);
ellipticalarch(xc, y0, yc, r1, r2);
ellipticalarch(xc, y0, yc, r1 + thickness, r2 + thickness);
}
void ellipticalarch(const double xc, const double y0, const double yc, const double a, const double b)
{
moveto(xc + a, y0);
lineto(xc + a, yc);
/* The arch itself, a half ellipse */
half_ellipse(xc, yc, a, b, 0.0);
lineto(xc - a, y0);
}
void half_ellipse(const double x0, const double y0, const double a, const double b, const double theta)
{
const int npts = 36;
const double delta = M_PI / (double)npts;
const double sintheta = sin(theta);
const double costheta = cos(theta);
int i;
for (i = 0; i <= npts; i++) {
const double t = (double)i * delta;
const double x = (a * cos(t) * costheta) - (b * sin(t) * sintheta);
const double y = (a * cos(t) * sintheta) + (b * sin(t) * costheta);
if (i == 0)
moveto(x0 + x, y0 + y);
else
lineto(x0 + x, y0 + y);
}
}
void plot_lr(const double x0, const double y0, const double width, const double height, const double r1, const double r2)
{
const double xc = x0 + (width / 2.0);
const double yc = y0 + (height / 2.0);
const double d = r2 / 2.0;
const double thickness = r2 / 10.0;
/* Centre lines */
moveto(x0, yc);
lineto(x0 + width, yc);
moveto(xc, y0);
lineto(xc, y0 + height);
/* Left-hand quarter-circle */
moveto(xc - d, yc);
arc(xc + d, yc, -90.0);
/* Right-hand quarter-circle */
moveto(xc + d, yc);
arc(xc - d, yc, 90.0);
/* Thicker pen for outline of arch */
// pencolr(1);
pointedarch(xc, y0, yc, d, d);
pointedarch(xc, y0, yc, d + thickness, d + thickness);
}
void pointedarch(const double xc, const double y0, const double yc, const double d1, const double d2)
{
const double degrees = acos(d2 / (d1 + d2)) * (180.0 / M_PI);
moveto(xc - d1, y0);
lineto(xc - d1, yc);
arc(xc + d2, yc, -degrees);
arc(xc - d2, yc, -degrees);
lineto(xc + d1, y0);
}