-
Notifications
You must be signed in to change notification settings - Fork 4
/
star_grid.c
146 lines (117 loc) · 3.47 KB
/
star_grid.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
/* star_grid --- plot a grid of stars with offset centres 2019-04-16 */
/* Copyright (c) 2019 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_star(const double x0, const double y0, const double gridsz, const double xc, const double yc, const int n);
int main(int argc, char * const argv[])
{
/* Inspired by Paul Rickards' tweet:
https://twitter.com/paulrickards/status/1096544602050514944
*/
int opt;
int i, j;
int ncells;
double height;
double xc;
double maxx, maxy;
double left, right;
double gridsz;
ncells = 7; /* Default for A3 */
while ((opt = getopt(argc, argv, "no:p:s:t:v:")) != -1) {
switch (opt) {
case 's':
if (strchr(optarg, '1'))
ncells = 15;
else if (strchr(optarg, '2'))
ncells = 9;
else if (strchr(optarg, '4'))
ncells = 7;
else if (strchr(optarg, '5'))
ncells = 7;
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(0) < 0) {
fputs("Failed to initialise HPGL library\n", stderr);
exit(EXIT_FAILURE);
}
getplotsize(&maxx, &maxy);
xc = maxx / 2.0;
height = maxy;
gridsz = maxy / (double)ncells;
left = xc - (height / 2.0);
right = xc + (height / 2.0);
/* Draw square border */
rectangle(left, 0.0, right, maxy);
/* Draw grid */
for (i = 1; i < ncells; i++) {
const double x = left + (i * gridsz);
moveto(x, 0.0);
lineto(x, maxy);
}
for (i = 1; i < ncells; i++) {
const double y = i * gridsz;
moveto(left, y);
lineto(right, y);
}
/* Draw stars */
for (i = 0; i < ncells; i++) {
for (j = 0; j < ncells; j++) {
const double x0 = left + (i * gridsz);
const double y0 = j * gridsz;
const double xc = x0 + ((gridsz * (i + 1)) / (double)(ncells + 1));
const double yc = y0 + ((gridsz * (j + 1)) / (double)(ncells + 1));
draw_star(x0, y0, gridsz, xc, yc, 9);
}
}
plotend();
return (0);
}
/* draw_star --- draw a single star at (x0, y0) */
void draw_star(const double x0, const double y0, const double gridsz, const double xc, const double yc, const int n)
{
int i;
const double d = gridsz / (double)(n - 1);
for (i = 0; i < (n / 2); i++) {
const double x1 = x0 + (i * d * 2.0);
const double x2 = x1 + d;
moveto(x1, y0);
lineto(xc, yc);
lineto(x2, y0);
}
for (i = 0; i < (n / 2); i++) {
const double y1 = y0 + (i * d * 2.0);
const double y2 = y1 + d;
moveto(x0 + gridsz, y1);
lineto(xc, yc);
lineto(x0 + gridsz, y2);
}
for (i = 0; i < (n / 2); i++) {
const double x1 = x0 + gridsz - (i * d * 2.0);
const double x2 = x1 - d;
moveto(x1, y0 + gridsz);
lineto(xc, yc);
lineto(x2, y0 + gridsz);
}
for (i = 0; i < (n / 2); i++) {
const double y1 = y0 + gridsz - (i * d * 2.0);
const double y2 = y1 - d;
moveto(x0, y1);
lineto(xc, yc);
lineto(x0, y2);
}
}