-
Notifications
You must be signed in to change notification settings - Fork 4
/
linetiles.c
142 lines (120 loc) · 3.36 KB
/
linetiles.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
/* linetiles --- draw randomised tiles made up of lines 2020-08-12 */
/* Copyright (c) 2020 John Honniball, Froods Software Development */
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include "hpgllib.h"
void drawTile(const double x, const double y, const double wd, const double ht, const int tile_type);
int main(int argc, char * const argv[])
{
/* Inspired by a tweet on #plottertwitter */
int opt;
int i, j;
int ngridx = 12;
int ngridy = 12;
int tile_type;
double x, y;
double xc;
double maxx, maxy;
double height;
double gridw, gridh;
double gridx0;
while ((opt = getopt(argc, argv, "no:p:s:t:v:")) != -1) {
switch (opt) {
case 's':
if (strchr(optarg, '1')) {
ngridx = ngridy = 32;
}
else if (strchr(optarg, '2')) {
ngridx = ngridy = 14; // If A1 is 32, A2 should be 22, but that's too slow
}
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);
}
}
srand((unsigned int)time(NULL));
if (plotbegin(0) < 0) {
fputs("Failed to initialise HPGL library\n", stderr);
exit(EXIT_FAILURE);
}
getplotsize(&maxx, &maxy);
xc = maxx / 2.0;
height = maxy;
/* Draw square border */
rectangle(xc - (height / 2.0), 0.0, xc + (height / 2.0), maxy);
gridx0 = xc - (height / 2.0);
gridw = maxy / ngridx;
gridh = maxy / ngridy;
/* Draw the main grid */
for (i = 0; i < ngridy; i++) {
for (j = 0; j < ngridx; j++) {
x = gridx0 + (j * gridw);
y = i * gridh;
// tile_type = (i + j) % 6;
tile_type = rand() / (RAND_MAX / 6);
drawTile(x, y, gridw, gridh, tile_type);
}
}
plotend();
return (0);
}
/* drawTile --- draw a single tile at (x, y) */
void drawTile(const double x, const double y, const double wd, const double ht, const int tile_type)
{
int i;
const double step = wd / 8.0;
switch (tile_type) {
case 0:
for (i = 0; i < 9; i++) {
moveto(x, y + (step * i));
lineto(x + wd, y + (step * i));
}
break;
case 1:
for (i = 0; i < 9; i++) {
moveto(x + (step * i), y);
lineto(x + (step * i), y + ht);
}
break;
case 2:
for (i = 1; i < 9; i++) {
moveto(x, y + (step * i));
lineto(x + (step * i), y + (step * i));
lineto(x + (step * i), y);
}
break;
case 3:
for (i = 1; i < 9; i++) {
moveto(x, y + ht - (step * i));
lineto(x + (step * i), y + ht - (step * i));
lineto(x + (step * i), y + ht);
}
break;
case 4:
for (i = 1; i < 9; i++) {
moveto(x + wd, y + (step * i));
lineto(x + wd - (step * i), y + (step * i));
lineto(x + wd - (step * i), y);
}
break;
case 5:
for (i = 1; i < 9; i++) {
moveto(x + wd, y + ht - (step * i));
lineto(x + wd - (step * i), y + ht - (step * i));
lineto(x + wd - (step * i), y + ht);
}
break;
}
}