-
Notifications
You must be signed in to change notification settings - Fork 3
/
sqinsq.c
94 lines (78 loc) · 2.04 KB
/
sqinsq.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
/* sqinsq -- draw square-in-square pattern 2013-01-26 */
/* Copyright (c) 2013 John Honniball, Froods Software Development */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include "hpgllib.h"
int main(int argc, char * const argv[])
{
/* High-Resolution Computer Graphics Using C, by Ian O. Angell, 1989.
Example 3.1, page 50. */
int opt;
double maxx, maxy;
struct vector2 {
double x;
double y;
};
struct vector2 ptd[4];
struct vector2 pt[4];
int n = 80;
int t = 7;
double q;
double mu, um;
double xoff;
int i, j, nextj;
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);
pt[0].x = maxy;
pt[0].y = maxy;
pt[1].x = maxy;
pt[1].y = 0.0;
pt[2].x = 0.0;
pt[2].y = 0.0;
pt[3].x = 0.0;
pt[3].y = maxy;
xoff = (maxx - maxy) / 2.0;
q = tan(t * M_PI / (4.0 * n));
mu = q / (q + 1.0);
um = 1.0 - mu;
/* Draw inner squares */
for (i = 0; i <= n; i++) {
moveto(pt[0].x + xoff, pt[0].y);
for (j = 1; j < 4; j++)
lineto(pt[j].x + xoff, pt[j].y);
lineto(pt[0].x + xoff, pt[0].y);
for (j = 0; j < 4; j++) {
nextj = (j + 1) % 4;
ptd[j].x = (um * pt[j].x) + (mu * pt[nextj].x);
ptd[j].y = (um * pt[j].y) + (mu * pt[nextj].y);
}
/* Reset square coordinates */
for (j = 0; j < 4; j++) {
pt[j].x = ptd[j].x;
pt[j].y = ptd[j].y;
}
}
plotend();
return (0);
}