From 3ea3a0e58f9098bed7e5ba804ab551456523db49 Mon Sep 17 00:00:00 2001 From: John Honniball Date: Mon, 29 Apr 2019 00:08:38 +0100 Subject: [PATCH] Update to new coding standards. --- flake.c | 2 +- fraserspiral.c | 50 +++++++++++++++++++------------------------ lotus.c | 57 ++++++++++++++++++++++++-------------------------- pin_circle.c | 37 ++++++++++++++++---------------- 4 files changed, 68 insertions(+), 78 deletions(-) diff --git a/flake.c b/flake.c index e87f497..53aad89 100644 --- a/flake.c +++ b/flake.c @@ -11,7 +11,7 @@ struct Finger { double w2; }; -int main (int argc, char * const argv[]) +int main(int argc, char * const argv[]) { int i; int j; diff --git a/fraserspiral.c b/fraserspiral.c index 18b442b..7bb1994 100644 --- a/fraserspiral.c +++ b/fraserspiral.c @@ -9,10 +9,10 @@ #define DEGREES (180.0 / M_PI) -void ringoshapes (double x0, double y0, double radius, int nshapes); +void ringoshapes(const double x0, const double y0, const double radius, const int nshapes); -int main (int argc, char * const argv[]) +int main(int argc, char * const argv[]) { int opt; int i; @@ -22,7 +22,7 @@ int main (int argc, char * const argv[]) double radius; double r; - 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': @@ -30,19 +30,18 @@ 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", - argv[0]); - fprintf (stderr, " ::= A1 | A2 | A3 | A4 | A5\n"); -// exit (EXIT_FAILURE); + fprintf(stderr, "Usage: %s [-p pen] [-s ] [-t title]\n", argv[0]); + fprintf(stderr, " ::= A1 | A2 | A3 | A4 | A5\n"); +// exit(EXIT_FAILURE); } } - plotbegin (0); + plotbegin(0); - getplotsize (&maxx, &maxy); + getplotsize(&maxx, &maxy); xc = maxx / 2.0; yc = maxy / 2.0; @@ -50,46 +49,41 @@ int main (int argc, char * const argv[]) height = maxy; /* Draw square border */ - rectangle (xc - (height / 2.0), 0.0, xc + (height / 2.0), maxy); + rectangle(xc - (height / 2.0), 0.0, xc + (height / 2.0), maxy); radius = height / 24.0; /* Draw eleven concentric circles of shapes */ for (i = 0; i < 11; i++) { r = radius * (i + 1); - ringoshapes (xc, yc, r, 16 + i); + ringoshapes(xc, yc, r, 16 + i); } - plotend (); + plotend(); return (0); } -void ringoshapes (double x0, double y0, double radius, int nshapes) +void ringoshapes(const double x0, const double y0, const double radius, const int nshapes) { int i; - double theta; double x, y; - double delta; double xc, yc; - double d; - - delta = 2.0 * M_PI / (double)nshapes; + const double d = radius / 4.0; + const double delta = 2.0 * M_PI / (double)nshapes; for (i = 0; i < nshapes; i++) { - theta = i * delta; + const double theta = i * delta; - x = (radius * cos (theta)) + x0; - y = (radius * sin (theta)) + y0; + x = (radius * cos(theta)) + x0; + y = (radius * sin(theta)) + y0; - moveto (x, y); - - d = radius / 4.0; + moveto(x, y); - xc = (d * cos (theta + (M_PI / 2.0))) + x0; - yc = (d * sin (theta + (M_PI / 2.0))) + y0; + xc = (d * cos(theta + (M_PI / 2.0))) + x0; + yc = (d * sin(theta + (M_PI / 2.0))) + y0; - arc (xc, yc, (delta * 1.2) * DEGREES); + arc(xc, yc, (delta * 1.2) * DEGREES); } } diff --git a/lotus.c b/lotus.c index e408f70..4a8b4ae 100644 --- a/lotus.c +++ b/lotus.c @@ -9,10 +9,10 @@ #include "hpgllib.h" -double zigzagring (double x0, double y0, double r1, double r2, int npts, int incr, int flag); +double zigzagring(const double x0, const double y0, const double r1, const double r2, const int npts, const int incr, const int flag); -int main (int argc, char * const argv[]) +int main(int argc, char * const argv[]) { /* Mosaic and Tessellated Patterns, by John Willson, 1983. ISBN: 0-486-24379-6. Plate 23. */ @@ -25,16 +25,16 @@ int main (int argc, char * const argv[]) double radius; double incrad; - 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 's': - if (strchr (optarg, '1')) + if (strchr(optarg, '1')) scale = 80.0; - else if (strchr (optarg, '2')) + else if (strchr(optarg, '2')) scale = 56.57; - else if (strchr (optarg, '4')) + else if (strchr(optarg, '4')) scale = 28.28; - else if (strchr (optarg, '5')) + else if (strchr(optarg, '5')) scale = 20.0; case 'n': @@ -42,19 +42,18 @@ int main (int argc, char * const argv[]) case 'p': case 't': case 'v': - plotopt (opt, optarg); + plotopt(opt, optarg); break; default: /* '?' */ - fprintf (stderr, "Usage: %s [-p pen] [-s ] [-t title]\n", - argv[0]); - fprintf (stderr, " ::= A1 | A2 | A3 | A4 | A5\n"); + fprintf(stderr, "Usage: %s [-p pen] [-s ] [-t title]\n", argv[0]); + fprintf(stderr, " ::= A1 | A2 | A3 | A4 | A5\n"); exit (EXIT_FAILURE); } } - plotbegin (0); + plotbegin(0); - getplotsize (&maxx, &maxy); + getplotsize(&maxx, &maxy); xc = maxx / 2.0; yc = maxy / 2.0; @@ -62,30 +61,28 @@ int main (int argc, char * const argv[]) height = maxy; /* Draw square border */ - rectangle (xc - (height / 2.0), 0.0, xc + (height / 2.0), maxy); + rectangle(xc - (height / 2.0), 0.0, xc + (height / 2.0), maxy); - radius = 15.0 * scale; /* Inital radius 15mm */ + radius = 15.0 * scale; /* Initial radius 15mm */ incrad = 15.0 * scale; /* Each zig-zag 15mm bigger than the previous one */ for (i = 0; i < 8; i++) - radius = zigzagring (xc, yc, radius, radius + incrad, 18, 1, i & 1); + radius = zigzagring(xc, yc, radius, radius + incrad, 18, 1, i & 1); - plotend (); + plotend(); return (0); } -double zigzagring (double x0, double y0, double r1, double r2, int npts, int incr, int flag) +double zigzagring(const double x0, const double y0, const double r1, const double r2, const int npts, const int incr, const int flag) { int i; double x1[128], y1[128]; double x2[128], y2[128]; double theta1, theta2; - double delta; int n1, n2; - - delta = (2.0 * M_PI) / (double)npts; + const double delta = (2.0 * M_PI) / (double)npts; for (i = 0; i < npts; i++) { if (flag) { @@ -97,26 +94,26 @@ double zigzagring (double x0, double y0, double r1, double r2, int npts, int inc theta2 = (delta * (double)i) + (delta / 2.0); } - x1[i] = (cos (theta1) * r1) + x0; - y1[i] = (sin (theta1) * r1) + y0; + x1[i] = (cos(theta1) * r1) + x0; + y1[i] = (sin(theta1) * r1) + y0; - x2[i] = (cos (theta2) * r2) + x0; - y2[i] = (sin (theta2) * r2) + y0; + x2[i] = (cos(theta2) * r2) + x0; + y2[i] = (sin(theta2) * r2) + y0; } - moveto (x1[0], y1[0]); + moveto(x1[0], y1[0]); n1 = 0; n2 = incr / 2; for (i = 0; i < npts; i++) { - lineto (x1[n1], y1[n1]); - lineto (x2[n2], y2[n2]); + lineto(x1[n1], y1[n1]); + lineto(x2[n2], y2[n2]); n1 = (n1 + incr) % npts; n2 = (n2 + incr) % npts; } - lineto (x1[0], y1[0]); - + lineto(x1[0], y1[0]); + return (r2); } diff --git a/pin_circle.c b/pin_circle.c index 23b3604..ac132e4 100644 --- a/pin_circle.c +++ b/pin_circle.c @@ -12,7 +12,7 @@ int draw_pin_circle(const double xc, const double yc, const double r, const int n1, const int n2); -int main (int argc, char * const argv[]) +int main(int argc, char * const argv[]) { /* https://twitter.com/janjarfalk/status/991291613338132480 */ int n1, n2; @@ -22,7 +22,7 @@ int main (int argc, char * const argv[]) double xc, yc; 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': @@ -30,25 +30,24 @@ 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", - argv[0]); - fprintf (stderr, " ::= A1 | A2 | A3 | A4 | A5\n"); - exit (EXIT_FAILURE); + fprintf(stderr, "Usage: %s [-p pen] [-s ] [-t title]\n", argv[0]); + fprintf(stderr, " ::= A1 | A2 | A3 | A4 | A5\n"); + exit(EXIT_FAILURE); } } - plotbegin (0); + plotbegin(0); - getplotsize (&maxx, &maxy); + getplotsize(&maxx, &maxy); xc = maxx / 2.0; yc = maxy / 2.0; /* Draw square border */ - rectangle (xc - (maxy / 2.0), 0.0, xc + (maxy / 2.0), maxy); + rectangle(xc - (maxy / 2.0), 0.0, xc + (maxy / 2.0), maxy); n1 = 24; n2 = 5; @@ -56,13 +55,14 @@ int main (int argc, char * const argv[]) nlines = draw_pin_circle(xc, yc, r, n1, n2); - plotend (); + plotend(); - printf ("%d+%d pins, %d lines drawn\n", n1, n2, nlines); + printf("%d+%d pins, %d lines drawn\n", n1, n2, nlines); return (0); } + int draw_pin_circle(const double xc, const double yc, const double r, const int n1, const int n2) { struct coord { @@ -80,21 +80,20 @@ int draw_pin_circle(const double xc, const double yc, const double r, const int int nlines = 0; int pin1, pin2; const double delta = (M_PI * 2.0) / (double)n1; - double theta; int needmove; const int n3 = n2 + 1; const int n4 = n1 + n2; /* Pre-compute coordinates of pins */ for (i = 0; i < n1; i++) { - theta = (double)i * delta; + const double theta = (double)i * delta; pin[i].x = xc + (cos(theta) * r); pin[i].y = yc + (sin(theta) * r); } for (i = 0; i < n2; i++) { - theta = ((double)i * delta) + (delta / 2.0); + const double theta = ((double)i * delta) + (delta / 2.0); pin[i + n1].x = xc + (cos(theta) * r); pin[i + n1].y = yc + (sin(theta) * r); @@ -139,19 +138,19 @@ int draw_pin_circle(const double xc, const double yc, const double r, const int else pin2 = line[j].p1; -// printf ("Drawing to pin %d.\n", pin2); +// printf("Drawing to pin %d.\n", pin2); } else { pin1 = (pin1 + 1) % (n1 * 4); needmove = 1; -// printf ("Moving to pin %d\n", pin1); +// printf("Moving to pin %d\n", pin1); } } while (j >= nlines); if (needmove) - moveto (pin[pin1].x, pin[pin1].y); + moveto(pin[pin1].x, pin[pin1].y); - lineto (pin[pin2].x, pin[pin2].y); + lineto(pin[pin2].x, pin[pin2].y); line[j].drawn = 1; pin1 = pin2; needmove = 0;