-
Notifications
You must be signed in to change notification settings - Fork 4
/
zigzag.c
81 lines (63 loc) · 1.69 KB
/
zigzag.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
/* zigzag --- plot an image in greyscale using zigzags 2013-03-25 */
/* Copyright (c) 2013 John Honniball, Froods Software Development */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include "hpgllib.h"
int main(int argc, char * const argv[])
{
/* Inspired by polargraph plotter at Bristol Mini Maker Faire 2013 */
int opt;
double x, y;
double off;
int i, j;
int pixel;
char lin[128];
FILE *fp;
double maxx, maxy;
const double scale = 40.0;
while ((opt = getopt(argc, argv, "no:p:s:t:v:")) != -1) {
switch (opt) {
case 'n':
case 'o':
case 'p':
case 's':
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(1) < 0) {
fputs("Failed to initialise HPGL library\n", stderr);
exit(EXIT_FAILURE);
}
if ((fp = fopen("zigzag.pgm", "r")) == NULL) {
perror("zigzag.pgm");
exit(EXIT_FAILURE);
}
fgets(lin, 128, fp);
fgets(lin, 128, fp);
fgets(lin, 128, fp);
getplotsize(&maxx, &maxy);
for (i = 0; i < 40; i++) {
x = 10.0 * (double)i;
moveto(x * scale, 0.0);
for (j = 0; j < 108; j++) {
fscanf(fp, "%d", &pixel);
y = 2.5 * (double)j;
off = ((255 - pixel) * 5) / 255.0;
if (j & 1)
off = -off;
lineto((x + off) * scale, y * scale);
}
}
plotend();
fclose(fp);
return (0);
}