-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreuleaux.c
146 lines (115 loc) · 4.03 KB
/
reuleaux.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
/* reuleaux --- draw some Reuleaux polygons 2020-09-05 */
/* Copyright (c) 2020 John Honniball, Froods Software Development */
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <math.h>
#include "hpgllib.h"
void plot_ul(const double x0, const double y0, const double width, const double height, const double r1, const double r2);
void plot_ur(const double x0, const double y0, const double width, const double height, const double r1, const double r2);
void plot_ll(const double x0, const double y0, const double width, const double height, const double r1, const double r2);
void plot_lr(const double x0, const double y0, const double width, const double height, const double r1, const double r2);
void reuleaux(const double xc, const double yc, const double r, const int n, const bool drawSides);
int main(int argc, char * const argv[])
{
int opt;
double xc, yc;
double maxx, maxy;
double r1; /* Radius of smaller arcs */
double r2; /* Radius of larger arc */
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);
}
}
/* Select first pen and draw border */
if (plotbegin(1) < 0) {
fputs("Failed to initialise HPGL library\n", stderr);
exit(EXIT_FAILURE);
}
getplotsize(&maxx, &maxy);
xc = maxx / 2.0;
yc = maxy / 2.0;
r1 = maxx / 5.0;
r2 = maxy / 5.0;
/* Split page into quarters */
moveto(0.0, yc);
lineto(maxx, yc);
moveto(xc, 0.0);
lineto(xc, maxy);
/* Draw four Reuleaux polygons */
plot_ll(0.0, 0.0, xc, yc, r1, r2);
plot_lr(xc, 0.0, xc, yc, r1, r2);
plot_ul(0.0, yc, xc, yc, r1, r2);
plot_ur(xc, yc, xc, yc, r1, r2);
plotend();
return (0);
}
void plot_ul(const double x0, const double y0, const double width, const double height, const double r1, const double r2)
{
const double xc = x0 + (width / 2.0);
const double yc = y0 + (height / 2.0);
reuleaux(xc, yc, r2, 3, true);
reuleaux(xc, yc, r2 * 1.15, 3, false);
}
void plot_ur(const double x0, const double y0, const double width, const double height, const double r1, const double r2)
{
const double xc = x0 + (width / 2.0);
const double yc = y0 + (height / 2.0);
reuleaux(xc, yc, r2, 5, true);
reuleaux(xc, yc, r2 * 1.15, 5, false);
}
void plot_ll(const double x0, const double y0, const double width, const double height, const double r1, const double r2)
{
const double xc = x0 + (width / 2.0);
const double yc = y0 + (height / 2.0);
reuleaux(xc, yc, r2, 7, true);
reuleaux(xc, yc, r2 * 1.15, 7, false);
}
void plot_lr(const double x0, const double y0, const double width, const double height, const double r1, const double r2)
{
const double xc = x0 + (width / 2.0);
const double yc = y0 + (height / 2.0);
reuleaux(xc, yc, r2, 9, true);
reuleaux(xc, yc, r2 * 1.15, 9, false);
}
/* reuleaux --- draw a Reuleaux polygon centred on (xc, yc) */
void reuleaux(const double xc, const double yc, const double r, const int n, const bool drawSides)
{
/* https://en.wikipedia.org/wiki/Reuleaux_triangle */
int i;
const double delta = (2.0 * M_PI) / (double)n;
const double degrees = 180.0 / (double)n;
double x[32];
double y[32];
for (i = 0; i <= n; i++) {
const double theta = (delta * (double)i) + (M_PI / 2.0);
x[i] = xc + (r * cos(theta));
y[i] = yc + (r * sin(theta));
}
for (i = 0; i < n; i++) {
const int j = (i + (n / 2)) % n;
moveto(x[i], y[i]);
arc(x[j], y[j], -degrees);
}
if (drawSides) {
for (i = 0; i <= n; i++) {
if (i == 0)
moveto(x[i], y[i]);
else
lineto(x[i], y[i]);
}
}
}