-
Notifications
You must be signed in to change notification settings - Fork 1
/
tplot.c
123 lines (97 loc) · 1.84 KB
/
tplot.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
#include <sys/ioctl.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <stdio.h>
#include <err.h>
#include "tplot.h"
static unsigned char *cells;
static struct winsize ws;
/*
* print a braille "pixel"
*/
void
dot(int rx, int ry) {
int y, x;
unsigned char *p;
if (rx > (ws.ws_col * 2) || rx < 1 || ry > (ws.ws_row * 4) || ry < 1) {
warnx("out of bounds");
return;
}
x = (rx - 1) / 2;
y = (ry - 1) / 4;
/* A B
* C D
* E F
* G H
*/
p = cells;
p += (y * ws.ws_col) + x;
switch (ry % 4) {
case 1: /* A B */
*p |= rx & 1 ? FIELD_A : FIELD_B;
break;
case 2: /* C D */
*p |= rx & 1 ? FIELD_C : FIELD_D;
break;
case 3: /* E F */
*p |= rx & 1 ? FIELD_E : FIELD_F;
break;
case 0: /* G H */
*p |= rx & 1 ? FIELD_G : FIELD_H;
break;
}
/* find the right char to print via table */
printf("\033[%u;%uH%lc", y + 1, x + 1, *p | BRAILLE_EMPTY);
}
/*
* dot() a line from x0, y0 to x1, y1
*/
void
line(int x0, int y0, int x1, int y1) {
int dx = abs(x1 - x0);
int dy = abs(y1 - y0);
int sx = x0 < x1 ? 1 : -1;
int sy = y0 < y1 ? 1 : -1;
int er = (dx > dy ? dx : -dy) / 2;
int e2;
for (;;) {
dot(x0, y0);
if (x0 == x1 && y0 == y1)
break;
e2 = er;
if (e2 > -dx) {
er -= dy;
x0 += sx;
}
if (e2 < dy) {
er += dx;
y0 += sy;
}
}
}
int
main(void) {
char buf[BUFSIZ];
int x1, x2, y1, y2;
if (ioctl(1, TIOCGWINSZ, &ws) < 0)
err(1, "ioctl()");
cells = malloc(sizeof(unsigned char) * (ws.ws_col * ws.ws_row));
if (cells == NULL)
err(1, "malloc()");
setlocale(LC_ALL, "");
setbuf(stdout, NULL);
while (fgets(buf, BUFSIZ, stdin)) {
sscanf(buf, "%u %u %*s %u %u", &x1, &y1, &x2, &y2);
if (x2 && y2)
line(x1, y1, x2, y2);
else if (x1 && y1)
dot(x1, y1);
else
/* if input is not valid, quit */
break;
x1 = x2 = y1 = y2 = 0;
}
free(cells);
return 0;
}