-
Notifications
You must be signed in to change notification settings - Fork 4
/
allover13.c
167 lines (136 loc) · 4.09 KB
/
allover13.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
/* allover13 --- draw all-over pattern, page 13 2013-06-23 */
/* Copyright (c) 2013 John Honniball, Froods Software Development */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include "hpgllib.h"
/* Geometric Allover Patterns, by Ian O. Angell
* Dover Publications, Inc., 1985, ISBN 0-486-24855-0
* Page 13
*/
void draw_module(const double x, const double y, const double side, const int firstx, const int lastx, const int firsty, const int lasty);
void square_triangle(const double x, const double y, const double side, const double ht);
double Scale; /* Plotter units per mm */
int main(int argc, char * const argv[])
{
int opt;
int ix, iy;
int nx, ny;
double pitch;
double x0, y0;
double width, height;
double xoffset, yoffset;
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 */
nx = 8;
ny = 6;
Scale = 40.0;
pitch = 45.0 * Scale;
if (plotbegin(0) < 0) {
fputs("Failed to initialise HPGL library\n", stderr);
exit(EXIT_FAILURE);
}
getplotsize(&maxx, &maxy);
width = nx * pitch;
height = ny * pitch;
xoffset = (maxx - width) / 2.0;
yoffset = (maxy - height) / 2.0;
rectangle(xoffset, yoffset, xoffset + width, yoffset + height);
for (ix = 0; ix < nx; ix++) {
for (iy = 0; iy < ny; iy++) {
x0 = xoffset + ((double)ix * pitch);
y0 = yoffset + ((double)iy * pitch);
draw_module(x0, y0, pitch, ix == 0, ix == nx - 1, iy == 0, iy == ny - 1);
}
}
plotend();
return (0);
}
/* draw_module --- draw a single module of the pattern at (x, y) */
void draw_module(const double x, const double y, const double side, const int firstx, const int lastx, const int firsty, const int lasty)
{
int i;
double trueside;
const double step = 5.5 * Scale;
const double radius = sqrt ((step * step) + (step * step));
double hside = side / 2.0;
double offset = 3.0 * step;
/* Draw central cross */
moveto(x + offset, y + hside);
lineto(x + side - offset, y + hside);
moveto(x + hside, y + offset);
lineto(x + hside, y + side - offset);
/* Draw squares surrounded by triangles */
for (i = 0; i < 3; i++) {
offset = (double)(i + 1) * step;
trueside = side - (2.0 * step * (double)(i + 1));
square_triangle(x + offset, y + offset, trueside, step);
}
/* Draw circles, allowing for special cases that draw arcs at the edges */
if (firsty) {
moveto(x + radius, y);
if (firstx)
arc(x, y, 90.0);
else
arc(x, y, 180.0);
}
else if (firstx) {
moveto(x, y - radius);
arc(x, y, 180.0);
}
else
circle(x, y, radius);
if (lastx) {
moveto(x + side, y + radius);
if (firsty)
arc(x + side, y, 90.0);
else
arc(x + side, y, 180.0);
}
if (lasty) {
if (firstx) {
moveto(x, y + side - radius);
arc(x, y + side, 90.0);
}
else {
moveto(x - radius, y + side);
arc(x, y + side, 180.0);
}
}
if (lastx && lasty) {
moveto(x + side - radius, y + side);
arc(x + side, y + side, 90.0);
}
}
/* square_triangle --- draw a square with triangles on each side */
void square_triangle(const double x, const double y, const double side, const double ht)
{
const double hside = side / 2.0;
rectangle(x, y, x + side, y + side);
moveto(x, y);
lineto(x - ht, y + hside);
lineto(x, y + side);
lineto(x + hside, y + ht + side);
lineto(x + side, y + side);
lineto(x + side + ht, y + hside);
lineto(x + side, y);
lineto(x + hside, y - ht);
lineto(x, y);
}