-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfraserspiral.c
92 lines (70 loc) · 1.99 KB
/
fraserspiral.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
/* fraserspiral --- plot the Fraser Spiral optical illusion 2013-05-16 */
/* Copyright (c) 2013 John Honniball, Froods Software Development */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include "hpgllib.h"
#define DEGREES (180.0 / M_PI)
void ringoshapes(const double x0, const double y0, const double radius, const int nshapes);
int main(int argc, char * const argv[])
{
int opt;
int i;
double xc, yc;
double maxx, maxy;
double height;
double radius;
double r;
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 / 24.0;
/* Draw eleven concentric circles of shapes */
for (i = 0; i < 11; i++) {
r = radius * (i + 1);
ringoshapes(xc, yc, r, 16 + i);
}
plotend();
return (0);
}
void ringoshapes(const double x0, const double y0, const double radius, const int nshapes)
{
int i;
double x, y;
double xc, yc;
const double d = radius / 4.0;
const double delta = 2.0 * M_PI / (double)nshapes;
for (i = 0; i < nshapes; i++) {
const double theta = i * delta;
x = (radius * cos(theta)) + x0;
y = (radius * sin(theta)) + y0;
moveto(x, y);
xc = (d * cos(theta + (M_PI / 2.0))) + x0;
yc = (d * sin(theta + (M_PI / 2.0))) + y0;
arc(xc, yc, (delta * 1.2) * DEGREES);
}
}