From 34f45bff52c90983d8242fa718f81adbf80e9f82 Mon Sep 17 00:00:00 2001 From: John Honniball Date: Sat, 15 Aug 2020 15:00:19 +0100 Subject: [PATCH] Convert 'lobe.c' to use the HPGL library. Should now make it scale up/down properly too. TODO: draw four different lobe patterns. --- Makefile | 18 +++---- TODO.md | 4 +- lobe.c | 145 +++++++++++++++++++------------------------------------ 3 files changed, 62 insertions(+), 105 deletions(-) diff --git a/Makefile b/Makefile index b3757e5..0ff6f22 100644 --- a/Makefile +++ b/Makefile @@ -49,15 +49,6 @@ op: op.o op.o: op.c $(CC) $(CFLAGS) -o $@ op.c -lobe.hpgl: lobe - ./lobe >lobe.hpgl - -lobe: lobe.o - $(LD) -o $@ lobe.o -lm - -lobe.o: lobe.c - $(CC) $(CFLAGS) -o $@ lobe.c - # HPGL library programs piscis.hpgl: piscis Makefile @@ -177,6 +168,15 @@ lissajous: lissajous.o hpgllib.o lissajous.o: lissajous.c hpgllib.h $(CC) $(CFLAGS) -o $@ lissajous.c +lobe.hpgl: lobe + ./lobe $(TITLE) $(BOLDPEN) -o $@ + +lobe: lobe.o hpgllib.o + $(LD) -o $@ lobe.o hpgllib.o -lm + +lobe.o: lobe.c hpgllib.h + $(CC) $(CFLAGS) -o $@ lobe.c + arches.hpgl: arches Makefile ./arches $(TITLE) $(BOLDPEN) -o $@ diff --git a/TODO.md b/TODO.md index bdeda8e..84f5c1a 100644 --- a/TODO.md +++ b/TODO.md @@ -63,9 +63,9 @@ plotter (using shell I/O redirection) when plotting Turtle programs. * Write a library test plot that exercises all the drawing commands. Useful if/when we add support for BMC or other non-HPGL plotters. * Convert some of the older drawings to fully use the HPGL library. -E.g. op, pconic, hconic, lobe. +E.g. op, pconic, hconic. * Finish off some of the incomplete plots. E.g. hexagon, fraserspiral, -flake, arches, lissajous, piscis, qrplot, spiralsq, superellipse, +flake, arches, piscis, qrplot, spiralsq, superellipse, twist, zigzag. * Clean up the various 'dala' drawings. Eliminate the 'dala2c' variant by adding colour to 'dala2.c'. diff --git a/lobe.c b/lobe.c index f1b10fc..8334108 100644 --- a/lobe.c +++ b/lobe.c @@ -2,132 +2,89 @@ /* Copyright (c) 1997 John Honniball, Froods Software Development */ #include +#include +#include #include +#include "hpgllib.h" #define RADIANS (M_PI / 180.0) -#define BLACK (1) -#define RED (2) -#define GREEN (3) -#define BLUE (4) -#define DEV_HPGL (1) -#define DEV_BMC (2) +void drawlobes(const double x0, const double y0, const double a, const double b, const double l); -static double Maxx, Maxy; -static double Homex, Homey; -static int Pencol; -static int Pltdev = DEV_HPGL; -void drawlobes(const double a, const double b, const double l); -void moveto(const double x, const double y); -void drawto(const double x, const double y); -void setpen(const int pen); - -int main(int argc, const char *argv[]) +int main(int argc, char *const argv[]) { - double a, b, l; + int opt; + double a, b; + double maxx, maxy; + double xc, yc; + double w4, h4; int i; - if (Pltdev == DEV_HPGL) { - Maxx = 10870; - Maxy = 7600; - - Maxx = 15970; - Maxy = 10870; + while ((opt = getopt(argc, argv, "no:p:s:t:v:")) != -1) { + switch (opt) { + case 's': + case 'n': + case 'o': + case 'p': + case 't': + case 'v': + 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); + } } - else { - Maxx = 3800.0; - Maxy = 2550.0; - - Maxx /= 1.4142; - Maxy /= 1.4142; + + if (plotbegin(1) < 0) { + fputs("Failed to initialise HPGL library\n", stderr); + exit(EXIT_FAILURE); } + + getplotsize(&maxx, &maxy); - Homex = Maxx / 2.0; - Homey = Maxy / 2.0; + xc = maxx / 2.0; + yc = maxy / 2.0; - Pencol = BLACK; + w4 = maxx / 4.0; + h4 = maxy / 4.0; - if (Pltdev == DEV_HPGL) - printf("SP%d;\n", Pencol); - else - printf("IP1;PS%d;\n", Pencol); + moveto(0.0, yc); + lineto(maxx, yc); - a = Maxy / 4.0; + moveto(xc, 0.0); + lineto(xc, maxy); + + a = maxy / 4.0; b = a / 3.0; for (i = 0; i < 9; i++) { - l = (double)i; - switch (i % 4) { - case 0: - setpen(BLACK); - break; - case 1: - setpen(RED); - break; - case 2: - setpen(GREEN); - break; - case 3: - setpen(BLUE); - break; - } - - drawlobes(a, b, l); + pencolr(i % 4); + drawlobes(xc, yc, a, b, (double)i); } - if (Pltdev == DEV_HPGL) - printf("SP0;\n"); - else - printf("MA0,0;CH;\n"); + plotend(); return (0); } -void drawlobes(const double a, const double b, const double l) +void drawlobes(const double x0, const double y0, const double a, const double b, const double l) { - double r, theta; - double x, y; int i; - moveto(Homex + a + b, Homey); + moveto(x0 + a + b, y0); for (i = 1; i <= 360; i++) { - theta = i * RADIANS; - r = a + (b * cos(l * theta)); + const double theta = i * RADIANS; + const double r = a + (b * cos(l * theta)); - x = r * cos(theta); - y = r * sin(theta); + const double x = r * cos(theta); + const double y = r * sin(theta); - drawto(Homex + x, Homey + y); + lineto(x0 + x, y0 + y); } } - - -void moveto(const double x, const double y) -{ - if (Pltdev == DEV_HPGL) - printf("PU;PA%d,%d\n", (int)x, (int)y); - else - printf("MA%d,%d\n", (int)x, (int)y); -} - - -void drawto(const double x, const double y) -{ - if (Pltdev == DEV_HPGL) - printf("PD;PA%d,%d\n", (int)x, (int)y); - else - printf("DA%d,%d\n", (int)x, (int)y); -} - - -void setpen(const int pen) -{ - if (Pltdev == DEV_HPGL) - printf("SP%d;\n", pen); - else - printf("PS%d\n", pen); -}