diff --git a/allover13.c b/allover13.c index c4dd9ba..ac8d773 100644 --- a/allover13.c +++ b/allover13.c @@ -12,22 +12,24 @@ * Page 13 */ -void draw_module (double x1, double y1, double side, int lastx, int lasty); -void square_triangle (double x, double y, double side, double ht); -void drawline (double x1, double y1, double x2, double y2); +void draw_module(const double x, const double y, const double side, const int firstx, const int lastx, const int firsty, const int lasty); +void square_triangle(const double x, const double y, const double side, const double ht); +void drawline(const double wx1, const double wy1, const double wx2, const double wy2); double Scale; -int main (int argc, char * const argv[]) +int main(int argc, char * const argv[]) { int opt; int ix, iy; int nx, ny; double pitch; - double x1, y1; + double x0, y0; + double width, height; + double xoffset, yoffset; double maxx, maxy; - while ((opt = getopt (argc, argv, "no:p:s:t:v:")) != -1) { + while ((opt = getopt(argc, argv, "no:p:s:t:v:")) != -1) { switch (opt) { case 'n': case 'o': @@ -35,13 +37,13 @@ int main (int argc, char * const argv[]) case 's': case 't': case 'v': - plotopt (opt, optarg); + plotopt(opt, optarg); break; default: /* '?' */ - fprintf (stderr, "Usage: %s [-p pen] [-s ] [-t title]\n", + fprintf(stderr, "Usage: %s [-p pen] [-s ] [-t title]\n", argv[0]); - fprintf (stderr, " ::= A1 | A2 | A3 | A4 | A5\n"); - exit (EXIT_FAILURE); + fprintf(stderr, " ::= A1 | A2 | A3 | A4 | A5\n"); + exit(EXIT_FAILURE); } } @@ -51,120 +53,151 @@ int main (int argc, char * const argv[]) Scale = 40.0; pitch = 45.0 * Scale; - plotbegin (1); + plotbegin(0); - getplotsize (&maxx, &maxy); + getplotsize(&maxx, &maxy); + + width = nx * pitch; + height = ny * pitch; + + xoffset = (maxx - width) / 2.0; + yoffset = (maxy - height) / 2.0; + + rectangle(xoffset, yoffset, xoffset + width, yoffset + height); for (ix = 0; ix < nx; ix++) { for (iy = 0; iy < ny; iy++) { - x1 = (double)ix * pitch; - y1 = (double)iy * pitch; - draw_module (x1, y1, pitch, ix == nx - 1, iy == ny - 1); + x0 = xoffset + ((double)ix * pitch); + y0 = yoffset + ((double)iy * pitch); + draw_module(x0, y0, pitch, ix == 0, ix == nx - 1, iy == 0, iy == ny - 1); } } - plotend (); + plotend(); return (0); } -void draw_module (double x, double y, double side, int lastx, int lasty) +void draw_module(const double x, const double y, const double side, const int firstx, const int lastx, const int firsty, const int lasty) { int i; - double radius; - double offset; double trueside; - double step = 5.5 * Scale; + const double step = 5.5 * Scale; + const double radius = sqrt ((step * step) + (step * step)); double hside = side / 2.0; + double offset = 3.0 * step; - offset = (double)3 * step; - - drawline (x + offset, y + hside, x + side - offset, y + hside); - drawline (x + hside, y + offset, x + hside, y + side - offset); + drawline(x + offset, y + hside, x + side - offset, y + hside); + drawline(x + hside, y + offset, x + hside, y + side - offset); for (i = 0; i < 3; i++) { offset = (double)(i + 1) * step; trueside = side - (2.0 * step * (double)(i + 1)); - square_triangle (x + offset, y + offset, trueside, step); + square_triangle(x + offset, y + offset, trueside, step); } - radius = sqrt ((step * step) + (step * step)); - - circle (x, y, radius); - - if (lastx) - circle (x + side, y, radius); - - if (lasty) - circle (x, y + side, radius); - - if (lastx && lasty) - circle (x + side, y + side, radius); + if (firsty) { + moveto(x + radius, y); + lineto(x + radius, y); + if (firstx) + arc(x, y, 90.0); + else + arc(x, y, 180.0); + } + else if (firstx) { + moveto(x, y - radius); + lineto(x, y - radius); + arc(x, y, 180.0); + } + else + circle(x, y, radius); + + if (lastx) { + moveto(x + side, y + radius); + lineto(x + side, y + radius); + if (firsty) + arc(x + side, y, 90.0); + else + arc(x + side, y, 180.0); + } + + if (lasty) { + if (firstx) { + moveto(x, y + side - radius); + lineto(x, y + side - radius); + arc(x, y + side, 90.0); + } + else { + moveto(x - radius, y + side); + lineto(x - radius, y + side); + arc(x, y + side, 180.0); + } + } + + if (lastx && lasty) { + moveto(x + side - radius, y + side); + lineto(x + side - radius, y + side); + arc(x + side, y + side, 90.0); + } } -void square_triangle (double x, double y, double side, double ht) +void square_triangle(const double x, const double y, const double side, const double ht) { - double hside; + const double hside = side / 2.0; - rectangle (x, y, x + side, y + side); + rectangle(x, y, x + side, y + side); - hside = side / 2.0; - - moveto (x, y); - lineto (x - ht, y + hside); - lineto (x, y + side); - lineto (x + hside, y + ht + side); - lineto (x + side, y + side); - lineto (x + side + ht, y + hside); - lineto (x + side, y); - lineto (x + hside, y - ht); - lineto (x, y); + moveto(x, y); + lineto(x - ht, y + hside); + lineto(x, y + side); + lineto(x + hside, y + ht + side); + lineto(x + side, y + side); + lineto(x + side + ht, y + hside); + lineto(x + side, y); + lineto(x + hside, y - ht); + lineto(x, y); } -void drawline (double wx1, double wy1, double wx2, double wy2) +void drawline(const double wx1, const double wy1, const double wx2, const double wy2) { static int cx = -32767, cy = -32767; static double wcx = -1.0, wcy = -1.0; int x1, y1, x2, y2; - double dx1, dy1, dx2, dy2; - double d1, d2; + const double dx1 = wx1 - wcx; + const double dy1 = wy1 - wcy; + const double dx2 = wx2 - wcx; + const double dy2 = wy2 - wcy; + const double d1 = sqrt((dx1 * dx1) + (dy1 * dy1)); + const double d2 = sqrt((dx2 * dx2) + (dy2 * dy2)); char hpgl[32]; - dx1 = wx1 - wcx; - dy1 = wy1 - wcy; - dx2 = wx2 - wcx; - dy2 = wy2 - wcy; - - d1 = sqrt ((dx1 * dx1) + (dy1 * dy1)); - d2 = sqrt ((dx2 * dx2) + (dy2 * dy2)); - if (d1 < d2) { - x1 = getdevx (wx1); - y1 = getdevy (wy1); - x2 = getdevx (wx2); - y2 = getdevy (wy2); + x1 = getdevx(wx1); + y1 = getdevy(wy1); + x2 = getdevx(wx2); + y2 = getdevy(wy2); wcx = wx2; wcy = wy2; } else { - x1 = getdevx (wx2); - y1 = getdevy (wy2); - x2 = getdevx (wx1); - y2 = getdevy (wy1); + x1 = getdevx(wx2); + y1 = getdevy(wy2); + x2 = getdevx(wx1); + y2 = getdevy(wy1); wcx = wx1; wcy = wy1; } if ((cx != x1) || (cy != y1)) { - snprintf (hpgl, 32, "\nPU;PA%d,%d;", x1, y1); - hpglout (hpgl); + snprintf(hpgl, sizeof (hpgl), "\nPU;PA%d,%d;", x1, y1); + hpglout(hpgl); } - - snprintf (hpgl, 32, "PD;PA%d,%d;", x2, y2); - hpglout (hpgl); + + snprintf(hpgl, sizeof (hpgl), "PD;PA%d,%d;", x2, y2); + hpglout(hpgl); cx = x2; cy = y2;