-
Notifications
You must be signed in to change notification settings - Fork 4
/
twist.c
85 lines (66 loc) · 2.06 KB
/
twist.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
/* twist --- draw multiple spirals on a common centre 2013-01-26 */
/* Copyright (c) 2013 John Honniball, Froods Software Development */
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <unistd.h>
#include "hpgllib.h"
void spiral(const double xc, const double yc, const double r1, const double r2, const double ang, const int n);
int main(int argc, char * const argv[])
{
/* High-Resolution Computer Graphics Using C, by Ian O. Angell, 1989.
Exercise 1.8, page 19. */
int opt;
double xc, yc;
double maxx, maxy;
double radius;
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;
radius = maxy / 2.0;
/* Draw circular border */
circle(xc, yc, radius);
spiral(xc, yc, 0.0, radius, 0.0, 4);
spiral(xc, yc, 0.0, radius, M_PI / 2.0, 4);
spiral(xc, yc, 0.0, radius, M_PI, 4);
spiral(xc, yc, 0.0, radius, 3.0 * M_PI / 2.0, 4);
plotend();
return (0);
}
/* spiral --- draw a spiral given angle and number of turns */
void spiral(const double xc, const double yc, const double r1, const double r2, const double ang, const int n)
{
const double delta = (2.0 * M_PI) / 72.0;
int i;
const double dr = (r2 - r1) / (72.0 * n);
for (i = 0; i <= (72 * n); i++) {
const double theta = ang + (delta * i);
const double r = r1 + (dr * i);
const double x = (r * cos(theta)) + xc;
const double y = (r * sin(theta)) + yc;
if (i == 0)
moveto(x, y);
else
lineto(x, y);
}
}