-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathspiralsq.c
172 lines (133 loc) · 3.81 KB
/
spiralsq.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
/* spiraqlsq --- plot a spiral optical illusion 2013-05-11 */
/* Copyright (c) 2013 John Honniball, Froods Software Development */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include "hpgllib.h"
#define RADIANS (M_PI / 180.0)
#define SLANT_DEGREES (20.0)
void ringoboxes2(const double x0, const double y0, const double radius, int nboxes, const int ninner, const int offset, const double slant);
int main(int argc, char * const argv[])
{
int opt;
int i;
double xc, yc;
double maxx, maxy;
double height;
double radius;
double r;
double slant;
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);
}
}
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;
height = maxy;
/* Draw circular border */
circle(xc, yc, yc);
radius = height / 12.0;
/* Draw four concentric circles of squares */
for (i = 0; i < 4; i++) {
if (i & 1)
slant = SLANT_DEGREES;
else
slant = -SLANT_DEGREES;
r = radius * (i + 1.5);
ringoboxes2(xc, yc, r, 18 * (i + 1), 1, 0, slant * RADIANS);
}
pencolr(1);
/* Draw four concentric circles of squares */
for (i = 0; i < 4; i++) {
if (i & 1)
slant = SLANT_DEGREES;
else
slant = -SLANT_DEGREES;
r = radius * (i + 1.5);
ringoboxes2(xc, yc, r, 18 * (i + 1), 1, 1, slant * RADIANS);
}
plotend();
return (0);
}
void ringoboxes2(const double x0, const double y0, const double radius, int nboxes, const int ninner, const int offset, const double slant)
{
int i, j, k, n;
double side, s2;
double theta;
double delta;
double s, c;
double x[4], y[4];
double rx[4], ry[4];
double inc;
double sr, cr;
side = (2.0 * M_PI * radius) / (double)nboxes;
side *= 0.8;
s2 = side / 2.0;
inc = s2 / (double)ninner;
delta = (2.0 * M_PI) / (double)nboxes;
delta *= 2.0;
nboxes /= 2;
for (i = 0; i < nboxes; i++) {
theta = (double)i * delta;
if (offset)
theta += delta / 2.0;
s = sin(theta);
c = cos(theta);
for (k = 0; k < ninner; k++) {
sr = sin(slant);
cr = cos(slant);
/* Set up a square */
x[0] = -s2;
y[0] = -s2;
x[1] = s2;
y[1] = -s2;
x[2] = s2;
y[2] = s2;
x[3] = -s2;
y[3] = s2;
for (n = 0; n < 4; n++) {
x[n] = (x[n] * cr) - (y[n] * sr);
y[n] = (x[n] * sr) + (y[n] * cr);
}
/* Shrink, rotate and translate square */
for (j = 0; j < 4; j++) {
if (x[j] < 0)
x[j] += k * inc;
else
x[j] -= k * inc;
if (y[j] < 0)
y[j] += k * inc;
else
y[j] -= k * inc;
rx[j] = (x[j] * c) - (y[j] * s);
ry[j] = (x[j] * s) + (y[j] * c);
rx[j] += x0 + (c * (radius + s2));
ry[j] += y0 + (s * (radius + s2));
}
/* Draw the rotated square */
moveto(rx[0], ry[0]);
lineto(rx[1], ry[1]);
lineto(rx[2], ry[2]);
lineto(rx[3], ry[3]);
lineto(rx[0], ry[0]);
}
}
}