-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoctoflower.c
97 lines (77 loc) · 2.42 KB
/
octoflower.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
/* octoflower --- plot an eight-petal flower 2020-08-23 */
/* Copyright (c) 2020 John Honniball, Froods Software Development */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include "hpgllib.h"
void draw_petal(const double xc, const double yc, const double delta, const double theta, const double radius);
int main(int argc, char * const argv[])
{
int opt;
int i;
double xc, yc;
double maxx, maxy;
double delta;
int npetals = 8;
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);
xc = maxx / 2.0;
yc = maxy / 2.0;
delta = (2.0 * M_PI) / (double)npetals;
radius = (maxy / 3.0);
for (i = 0; i < npetals; i++) {
const double theta = (double)i * delta;
const double x = xc + (radius * cos(theta));
const double y = yc + (radius * sin(theta));
moveto(xc, yc);
lineto(x, y);
}
for (i = 0; i < npetals; i++) {
const double theta = delta * (double)i;
draw_petal(xc, yc, delta, theta, radius);
}
plotend();
return (0);
}
void draw_petal(const double xc, const double yc, const double delta, const double theta, const double radius)
{
int i;
const double tipx = xc + (radius * cos(theta));
const double tipy = yc + (radius * sin(theta));
for (i = 1; i <= 6; i++) {
const double len = 4.0 * 40.0 * (double)i;
const double offx = len * sin(theta);
const double offy = -len * cos(theta);
const double arcx = tipx + offx;
const double arcy = tipy + offy;
const double baslen = len / tan(delta / 2.0);
const double basx = baslen * cos(theta);
const double basy = baslen * sin(theta);
moveto(xc + basx + offx, yc + basy + offy);
lineto(arcx, arcy);
arc(tipx, tipy, 180.0);
lineto(xc + basx - offx, yc + basy - offy);
}
}