-
Notifications
You must be signed in to change notification settings - Fork 4
/
cs_rosette.c
244 lines (194 loc) · 6.06 KB
/
cs_rosette.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
/* cs_rosette --- draw curve-stitching rosette 2013-06-22 */
/* Copyright (c) 2013 John Honniball, Froods Software Development */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include "hpgllib.h"
void draw_curve(const double x1, const double y1, const double x2, const double y2,
const double x3, const double y3, const int nlines);
void drawline(const double wx1, const double wy1, const double wx2, const double wy2);
#define MAXLINES (20)
struct vector {
double x;
double y;
};
int main(int argc, char * const argv[])
{
int opt;
int i, j, k;
int j1, k1;
int nrings, nsectors, nlines;
struct vector v1, v2;
double side;
double delta, theta;
double xc, yc;
double x1, y1;
double x2, y2;
double x3, y3;
double maxx, maxy;
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);
}
}
/* Basic drawing parameters */
nsectors = 6;
nrings = 4;
nlines = 16;
if (plotbegin(0) < 0) {
fputs("Failed to initialise HPGL library\n", stderr);
exit(EXIT_FAILURE);
}
getplotsize(&maxx, &maxy);
xc = maxx / 2.0;
yc = maxy / 2.0;
side = maxy / 2.0;
/* Draw circular border */
circle(xc, yc, yc);
/* Draw radials */
delta = (2.0 * M_PI) / (double)nsectors;
for (i = 0; i < nsectors; i++) {
theta = (double)i * delta;
x1 = xc + (side * cos(theta));
y1 = yc + (side * sin(theta));
drawline(xc, yc, x1, y1);
}
for (i = 0; i < nsectors; i++) {
theta = (double)i * delta;
/* Compute vectors, length side/nrings */
v1.x = (side / (double)nrings) * cos(theta);
v1.y = (side / (double)nrings) * sin(theta);
v2.x = (side / (double)nrings) * cos(theta + delta);
v2.y = (side / (double)nrings) * sin(theta + delta);
/* Draw lines parallel to v2 */
for (j = 1; j < nrings; j++) {
k = nrings - j;
x1 = xc + (double)j * v1.x;
y1 = yc + (double)j * v1.y;
x2 = x1 + ((double)k * v2.x);
y2 = y1 + ((double)k * v2.y);
drawline(x1, y1, x2, y2);
}
/* Draw lines parallel to v1 */
for (j = 1; j < nrings; j++) {
k = nrings - j;
x1 = xc + (double)j * v2.x;
y1 = yc + (double)j * v2.y;
x2 = x1 + ((double)k * v1.x);
y2 = y1 + ((double)k * v1.y);
drawline(x1, y1, x2, y2);
}
}
/* Loop around the n-agon */
for (i = 0; i < nsectors; i++) {
theta = (double)i * delta;
/* Vectors of length side/nrings again */
v1.x = (side / (double)nrings) * cos(theta);
v1.y = (side / (double)nrings) * sin(theta);
v2.x = (side / (double)nrings) * cos(theta + delta);
v2.y = (side / (double)nrings) * sin(theta + delta);
for (j = 0; j < nrings; j++) {
for (k = 0; k < (nrings - j); k++) {
/* Draw curves with vertex nearest centre */
x2 = xc + ((double)j * v1.x) + ((double)k * v2.x);
y2 = yc + ((double)j * v1.y) + ((double)k * v2.y);
x1 = x2 + v1.x;
y1 = y2 + v1.y;
x3 = x2 + v2.x;
y3 = y2 + v2.y;
draw_curve(x1, y1, x2, y2, x3, y3, nlines);
j1 = j + 1;
k1 = k + 1;
/* If there's room, draw curve with vertex away from centre */
if ((j1 < nrings) && (k1 < (nrings - j))) {
x2 = xc + ((double)j1 * v1.x) + ((double)k1 * v2.x);
y2 = yc + ((double)j1 * v1.y) + ((double)k1 * v2.y);
x1 = x2 - v1.x;
y1 = y2 - v1.y;
x3 = x2 - v2.x;
y3 = y2 - v2.y;
draw_curve(x1, y1, x2, y2, x3, y3, nlines);
}
}
}
}
plotend();
return (0);
}
/* draw_curve --- draw a single curve-stitch */
void draw_curve(const double x1, const double y1, const double x2, const double y2,
const double x3, const double y3, const int nlines)
{
int i;
double alpha;
double dx, dy;
double xpt1[MAXLINES];
double ypt1[MAXLINES];
double xpt2[MAXLINES];
double ypt2[MAXLINES];
dx = x2 - x1;
dy = y2 - y1;
for (i = 1; i < nlines; i++) {
alpha = (1.0 / (double)nlines) * (double)i;
xpt1[i] = x1 + (dx * alpha);
ypt1[i] = y1 + (dy * alpha);
}
dx = x3 - x2;
dy = y3 - y2;
for (i = 1; i < nlines; i++) {
alpha = (1.0 / (double)nlines) * (double)i;
xpt2[i] = x2 + (dx * alpha);
ypt2[i] = y2 + (dy * alpha);
}
for (i = 1; i < nlines; i++)
drawline(xpt1[i], ypt1[i], xpt2[i], ypt2[i]);
}
void drawline(const double wx1, const double wy1, const double wx2, const double wy2)
{
static int cx = -32767, cy = -32767;
static double wcx = -1.0, wcy = -1.0;
int x1, y1, x2, y2;
const double dx1 = wx1 - wcx;
const double dy1 = wy1 - wcy;
const double dx2 = wx2 - wcx;
const double dy2 = wy2 - wcy;
const double d1 = sqrt((dx1 * dx1) + (dy1 * dy1));
const double d2 = sqrt((dx2 * dx2) + (dy2 * dy2));
char hpgl[32];
if (d1 < d2) {
x1 = getdevx(wx1);
y1 = getdevy(wy1);
x2 = getdevx(wx2);
y2 = getdevy(wy2);
wcx = wx2;
wcy = wy2;
}
else {
x1 = getdevx(wx2);
y1 = getdevy(wy2);
x2 = getdevx(wx1);
y2 = getdevy(wy1);
wcx = wx1;
wcy = wy1;
}
if ((cx != x1) || (cy != y1)) {
snprintf(hpgl, sizeof (hpgl), "\nPU;PA%d,%d;", x1, y1);
hpglout(hpgl);
}
snprintf(hpgl, sizeof (hpgl), "PD;PA%d,%d;", x2, y2);
hpglout(hpgl);
cx = x2;
cy = y2;
}