-
Notifications
You must be signed in to change notification settings - Fork 3
/
truchet1.c
150 lines (120 loc) · 3.38 KB
/
truchet1.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
147
148
149
150
/* truchet1 --- draw Truchet tiles with arcs on them 2020-08-26 */
/* 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;
gridx0 = xc - (height / 2.0);
gridw = maxy / ngridx;
gridh = maxy / ngridy;
const double bigr = gridw / sqrt(2.0);
const double smallr = gridw - bigr;
for (i = 0; i < ngridx; i++) {
x = gridx0 + (i * gridw);
moveto(x + smallr, maxy);
lineto(x + bigr, maxy);
}
for (i = 0; i < ngridy; i++) {
y = i * gridh;
moveto(gridx0, y + smallr);
lineto(gridx0, y + bigr);
}
for (i = 0; i < ngridx; i++) {
x = gridx0 + (i * gridw);
moveto(x + smallr, 0.0);
lineto(x + bigr, 0.0);
}
for (i = 0; i < ngridy; i++) {
y = i * gridh;
moveto(gridx0 + maxy, y + smallr);
lineto(gridx0 + maxy, y + bigr);
}
/* 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) % 2;
tile_type = rand() / (RAND_MAX / 2);
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)
{
const double bigr = wd / sqrt(2.0);
const double smallr = wd - bigr;
switch (tile_type) {
case 0:
moveto(x + wd, y + smallr);
arc(x + wd, y, 90.0);
moveto(x + smallr, y);
arc(x + wd, y, -90.0);
moveto(x + bigr, y + ht);
arc(x, y + ht, -90.0);
moveto(x, y + bigr);
arc(x, y + ht, 90.0);
break;
case 1:
moveto(x, y + smallr);
arc(x, y, -90.0);
moveto(x + bigr, y);
arc(x, y, 90.0);
moveto(x + smallr, y + ht);
arc(x + wd, y + ht, 90.0);
moveto(x + wd, y + bigr);
arc(x + wd, y + ht, -90.0);
break;
}
}