-
Notifications
You must be signed in to change notification settings - Fork 4
/
pin_and_cotton.c
174 lines (141 loc) · 3.81 KB
/
pin_and_cotton.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
/* pin_and_cotton --- draw pin-and-cotton 2013-01-24 */
/* Copyright (c) 2013 John Honniball, Froods Software Development */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include "hpgllib.h"
#define MAXPINS (400)
#define MAXFIBS (30)
#define BOTTOM 1
#define RIGHT 2
#define TOP 4
#define LEFT 8
int main(int argc, char * const argv[])
{
/* High-Resolution Computer Graphics Using C, by Ian O. Angell, 1989.
Exercise 1.10, page 21. */
struct coord {
double x;
double y;
int side;
};
struct coord pin[MAXPINS];
struct cotton {
int p1;
int p2;
int drawn;
};
struct cotton line[1024];
int i, j;
int n;
int pin1, pin2;
int needmove;
int nfib;
int fibonacci[MAXFIBS];
int nlines;
double d;
double xoff;
int opt;
double xc;
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);
}
}
if (plotbegin(0) < 0) {
fputs("Failed to initialise HPGL library\n", stderr);
exit(EXIT_FAILURE);
}
getplotsize(&maxx, &maxy);
xc = maxx / 2.0;
/* Draw square border */
rectangle(xc - (maxy / 2.0), 0.0, xc + (maxy / 2.0), maxy);
xoff = (maxx - maxy) / 2.0;
/* Pre-compute coordinates of pins */
/* n = 13; */
n = 12;
for (i = 0; i < n; i++) {
d = (double)i * (maxy / (double)n);
pin[i].x = d + xoff;
pin[i].y = 0.0;
pin[i].side = BOTTOM;
pin[i + n].x = maxy + xoff;
pin[i + n].y = d;
pin[i + n].side = RIGHT;
pin[i + (n * 2)].x = (maxy - d) + xoff;
pin[i + (n * 2)].y = maxy;
pin[i + (n * 2)].side = TOP;
pin[i + (n * 3)].x = xoff;
pin[i + (n * 3)].y = maxy - d;
pin[i + (n * 3)].side = LEFT;
}
pin[0].side |= LEFT;
pin[n].side |= BOTTOM;
pin[n * 2].side |= RIGHT;
pin[n * 3].side |= TOP;
/* Fill in array of Fibonacci numbers */
fibonacci[0] = 1;
fibonacci[1] = 2;
for (i = 2; i < MAXFIBS; i++) {
fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2];
if (fibonacci[i] < (4 * n))
nfib = i + 1;
}
/* Draw lines between pins */
nlines = 0;
for (pin1 = 0; pin1 < (n * 4); pin1++) {
for (i = 0; i < nfib; i++) {
pin2 = (pin1 + fibonacci[i]) % (n * 4);
if ((pin[pin1].side & pin[pin2].side) == 0) {
line[nlines].p1 = pin1;
line[nlines].p2 = pin2;
line[nlines].drawn = 0;
nlines++;
}
}
}
pin1 = 0;
needmove = 1;
for (i = 0; i < nlines; i++) {
do {
for (j = 0; j < nlines; j++) {
if (line[j].drawn == 0 && (line[j].p1 == pin1 || line[j].p2 == pin1))
break;
}
if (j < nlines) {
if (line[j].p1 == pin1)
pin2 = line[j].p2;
else
pin2 = line[j].p1;
// printf("Drawing to pin %d.\n", pin2);
}
else {
pin1 = (pin1 + 1) % (n * 4);
needmove = 1;
// printf("Moving to pin %d\n", pin1);
}
} while (j >= nlines);
if (needmove)
moveto(pin[pin1].x, pin[pin1].y);
lineto(pin[pin2].x, pin[pin2].y);
line[j].drawn = 1;
pin1 = pin2;
needmove = 0;
}
plotend();
printf("%d pins, %d lines drawn\n", n * 4, nlines);
return (0);
}