-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlotus.c
119 lines (94 loc) · 2.88 KB
/
lotus.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
/* lotus --- draw lotus flower pattern 2013-07-25 */
/* Copyright (c) 2013 John Honniball, Froods Software Development */
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "hpgllib.h"
double zigzagring(const double x0, const double y0, const double r1, const double r2, const int npts, const int incr, const int flag);
int main(int argc, char * const argv[])
{
/* Mosaic and Tessellated Patterns, by John Willson, 1983.
ISBN: 0-486-24379-6. Plate 23. */
int i;
int opt;
double xc, yc;
double scale = 40.0;
double maxx, maxy;
double radius;
double incrad;
while ((opt = getopt(argc, argv, "no:p:s:t:v:")) != -1) {
switch (opt) {
case 's':
if (strchr(optarg, '1'))
scale = 80.0;
else if (strchr(optarg, '2'))
scale = 56.57;
else if (strchr(optarg, '4'))
scale = 28.28;
else if (strchr(optarg, '5'))
scale = 20.0;
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(0) < 0) {
fputs("Failed to initialise HPGL library\n", stderr);
exit(EXIT_FAILURE);
}
getplotsize(&maxx, &maxy);
xc = maxx / 2.0;
yc = maxy / 2.0;
/* Draw circular border */
circle(xc, yc, yc);
radius = 15.0 * scale; /* Initial radius 15mm */
incrad = 15.0 * scale; /* Each zig-zag 15mm bigger than the previous one */
for (i = 0; i < 8; i++)
radius = zigzagring(xc, yc, radius, radius + incrad, 18, 1, i & 1);
plotend();
return (0);
}
double zigzagring(const double x0, const double y0, const double r1, const double r2, const int npts, const int incr, const int flag)
{
int i;
double x1[128], y1[128];
double x2[128], y2[128];
double theta1, theta2;
int n1, n2;
const double delta = (2.0 * M_PI) / (double)npts;
for (i = 0; i < npts; i++) {
if (flag) {
theta2 = delta * (double)i;
theta1 = (delta * (double)i) - (delta / 2.0);
}
else {
theta1 = delta * (double)i;
theta2 = (delta * (double)i) + (delta / 2.0);
}
x1[i] = (cos(theta1) * r1) + x0;
y1[i] = (sin(theta1) * r1) + y0;
x2[i] = (cos(theta2) * r2) + x0;
y2[i] = (sin(theta2) * r2) + y0;
}
moveto(x1[0], y1[0]);
n1 = 0;
n2 = incr / 2;
for (i = 0; i < npts; i++) {
lineto(x1[n1], y1[n1]);
lineto(x2[n2], y2[n2]);
n1 = (n1 + incr) % npts;
n2 = (n2 + incr) % npts;
}
lineto(x1[0], y1[0]);
return (r2);
}