-
Notifications
You must be signed in to change notification settings - Fork 4
/
lissajous.c
94 lines (74 loc) · 2.39 KB
/
lissajous.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
/* lissajous --- plot a Lissajous curve in HPGL 2011-10-19 */
/* Copyright (c) 2011 John Honniball, Froods Software Development */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include "hpgllib.h"
void lissajous(const double x0, const double y0, const double side, const double f1, const double f2, const double theta, const int npts);
int main(int argc, char * const argv[])
{
int opt;
double xc, yc;
double h4, w4;
double maxx, maxy;
double side;
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;
h4 = maxy / 4.0;
w4 = maxx / 4.0;
side = maxy / 3.0;
/* Split page into quarters */
moveto(0.0, yc);
lineto(maxx, yc);
moveto(xc, 0.0);
lineto(xc, maxy);
/* Draw four simple Lissajous curves */
lissajous(w4, h4, side, 1.0, 3.0, 0.0, 3 * 72);
lissajous(xc + w4, h4, side, 5.0, 6.0, 0.0, 5 * 6 * 72);
lissajous(w4, yc + h4, side, 3.0, 5.0, 0.0, 3 * 5 * 72);
lissajous(xc + w4, yc + h4, side, 7.0, 9.0, 0.0, 7 * 9 * 72);
plotend();
return (0);
}
void lissajous(const double x0, const double y0, const double side, const double f1, const double f2, const double theta, const int npts)
{
const double delta = (2.0 * M_PI) / (double)npts;
const double sintheta = sin(theta);
const double costheta = cos(theta);
const double r = side / 2.0;
int i;
for (i = 0; i <= npts; i++) {
const double t = (double)i * delta;
const double t1 = t * f1;
const double t2 = t * f2;
const double x = (r * cos(t1) * costheta) - (r * sin(t2) * sintheta);
const double y = (r * cos(t1) * sintheta) + (r * sin(t2) * costheta);
if (i == 0)
moveto(x0 + x, y0 + y);
else
lineto(x0 + x, y0 + y);
}
}