-
Notifications
You must be signed in to change notification settings - Fork 4
/
sutpent.c
154 lines (120 loc) · 3.82 KB
/
sutpent.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
/* sutpent --- plot a Sutcliffe pentagon 2016-10-31 */
/* Copyright (c) 2016 John Honniball, Froods Software Development */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include "hpgllib.h"
/* Inspired by Section 8.4 of "Generative Art" by Matt Pearson, ISBN 1935182625 */
struct Vertex {
double x;
double y;
};
void sutcliffepolygon(const double xc, const double yc, const double radius, const int nsides, const int levels, const double ratio);
void sutpoly(const int nsides, const struct Vertex vertex[], const int level, const double ratio);
void drawpoly(const int nsides, const struct Vertex vertex[]);
int main(int argc, char * const argv[])
{
int opt;
double w4, h4;
double xc;
double maxx, maxy;
double radius;
while ((opt = getopt(argc, argv, "no:p:s:t:v:")) != -1) {
switch (opt) {
case 's':
case 'n':
case 'o':
case 'p':
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(1) < 0) {
fputs("Failed to initialise HPGL library\n", stderr);
exit(EXIT_FAILURE);
}
getplotsize(&maxx, &maxy);
w4 = maxx / 4.0;
h4 = maxy / 4.0;
radius = maxy / 4.0;
xc = sqrt(((2.0 * radius) * (2.0 * radius)) - (h4 * h4));
xc = (2.0 * w4) - xc;
sutcliffepolygon(maxx - xc, h4 * 3.0, radius, 5, 6, 0.15);
sutcliffepolygon(xc, h4 * 3.0, radius, 6, 7, 0.15);
sutcliffepolygon(maxx - xc, h4, radius, 7, 7, 0.15);
sutcliffepolygon(xc, h4, radius, 8, 7, 0.15);
sutcliffepolygon(w4 * 2.0, h4 * 2.0, radius, 9, 7, 0.15);
plotend();
return (0);
}
void sutcliffepolygon(const double xc, const double yc, const double radius, const int nsides, const int levels, const double ratio)
{
const double delta = (2.0 * M_PI) / (double)nsides;
double theta;
struct Vertex vertex[32];
int i;
for (i = 0; i < nsides; i++) {
theta = (double)i * delta;
vertex[i].x = xc + (cos(theta) * radius);
vertex[i].y = yc + (sin(theta) * radius);
}
sutpoly(nsides, vertex, levels, ratio);
}
void sutpoly(const int nsides, const struct Vertex vertex[], const int level, const double ratio)
{
int i;
int i1;
int opp, opp1;
double dx, dy;
double x1, y1;
double x2, y2;
struct Vertex newv[32];
drawpoly(nsides, vertex);
for (i = 0; i < nsides; i++) {
i1 = (i + 1) % nsides;
/* Midpoint of the current side of the polygon */
x1 = (vertex[i].x + vertex[i1].x) / 2.0;
y1 = (vertex[i].y + vertex[i1].y) / 2.0;
/* Locate the point that we will draw towards */
if (nsides & 1) {
opp = (i + (nsides / 2) + 1) % nsides;
x2 = vertex[opp].x;
y2 = vertex[opp].y;
}
else {
opp = (i + (nsides / 2)) % nsides;
opp1 = (opp + 1) % nsides;
x2 = (vertex[opp].x + vertex[opp1].x) / 2.0;
y2 = (vertex[opp].y + vertex[opp1].y) / 2.0;
}
/* Shorten the line (x1,y1)-(x2,y2) by the ratio */
dx = (x2 - x1) * ratio;
dy = (y2 - y1) * ratio;
x2 = x1 + dx;
y2 = y1 + dy;
moveto(x1, y1);
lineto(x2, y2);
newv[i].x = x2;
newv[i].y = y2;
}
if (level > 0)
sutpoly(nsides, newv, level - 1, ratio);
else
drawpoly(nsides, newv);
}
void drawpoly(const int nsides, const struct Vertex vertex[])
{
int i;
moveto(vertex[0].x, vertex[0].y);
for (i = 1; i < nsides; i++)
lineto(vertex[i].x, vertex[i].y);
lineto(vertex[0].x, vertex[0].y);
}