Skip to content

Commit

Permalink
New drawing: truchet1, the first Truchet tile plot. This plot draws a…
Browse files Browse the repository at this point in the history
… 12x12 grid of Truchet tiles using arcs. Might be slow to draw on some plotters.
  • Loading branch information
anachrocomputer committed Aug 26, 2020
1 parent 3eda065 commit f5c7a16
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,6 @@ superellipse
sutpent
ternary_arcs
tree
truchet1
twist
zigzag
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ HPGL=flake.hpgl op.hpgl dala1.hpgl hconic2.hpgl tree.hpgl dome.hpgl \
morphpoly.hpgl pin_circle.hpgl isogrid.hpgl circle_lines.hpgl \
star_grid.hpgl ternary_arcs.hpgl hexspiral.hpgl poly_oval.hpgl \
libtest.hpgl linetiles.hpgl a6cardlabels.hpgl octoflower.hpgl \
circle4.hpgl
circle4.hpgl truchet1.hpgl

SVG=$(HPGL:.hpgl=.svg)

Expand Down Expand Up @@ -148,6 +148,15 @@ ternary_arcs: ternary_arcs.o hpgllib.o
ternary_arcs.o: ternary_arcs.c hpgllib.h
$(CC) $(CFLAGS) -o $@ ternary_arcs.c

truchet1.hpgl: truchet1 Makefile
./truchet1 $(TITLE) $(BOLDPEN) -o $@

truchet1: truchet1.o hpgllib.o
$(LD) -o $@ truchet1.o hpgllib.o -lm

truchet1.o: truchet1.c hpgllib.h
$(CC) $(CFLAGS) -o $@ truchet1.c

poly_oval.hpgl: poly_oval Makefile
./poly_oval $(TITLE) $(BOLDPEN) -o $@

Expand Down
147 changes: 147 additions & 0 deletions truchet1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/* 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);
}

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);
}


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;
}
}

0 comments on commit f5c7a16

Please sign in to comment.