diff --git a/.gitignore b/.gitignore index d10bc22..ee3ced1 100644 --- a/.gitignore +++ b/.gitignore @@ -37,6 +37,7 @@ lissajous lobe lotus morphpoly +octoflower op op_moire pappus @@ -46,6 +47,7 @@ piscis pin_and_cotton pin_circle plottext +poly_oval qrplot rtree spiralsq @@ -56,6 +58,5 @@ superellipse sutpent ternary_arcs tree -poly_oval twist zigzag diff --git a/Makefile b/Makefile index 1330e77..95c0954 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ HPGL=flake.hpgl op.hpgl dala1.hpgl hconic2.hpgl tree.hpgl dome.hpgl \ qrplot.hpgl lotus.hpgl celticstep.hpgl op_moire.hpgl sutpent.hpgl \ morphpoly.hpgl pin_circle.hpgl isogrid.hpgl circle_lines.hpgl \ star_grid.hpgl ternary_arcs.hpgl hexspiral.hpgl poly_oval.hpgl \ - libtest.hpgl linetiles.hpgl a6cardlabels.hpgl + libtest.hpgl linetiles.hpgl a6cardlabels.hpgl octoflower.hpgl SVG=$(HPGL:.hpgl=.svg) @@ -282,6 +282,15 @@ celticstep: celticstep.o hpgllib.o celticstep.o: celticstep.c hpgllib.h $(CC) $(CFLAGS) -o $@ celticstep.c +octoflower.hpgl: octoflower Makefile + ./octoflower $(TITLE) $(BOLDPEN) -o $@ + +octoflower: octoflower.o hpgllib.o + $(LD) -o $@ octoflower.o hpgllib.o -lm + +octoflower.o: octoflower.c hpgllib.h + $(CC) $(CFLAGS) -o $@ octoflower.c + op_moire.hpgl: op_moire Makefile ./op_moire $(TITLE) $(BOLDPEN) -o $@ diff --git a/octoflower.c b/octoflower.c new file mode 100644 index 0000000..10c6ec6 --- /dev/null +++ b/octoflower.c @@ -0,0 +1,97 @@ +/* octoflower --- plot an eight-petal flower 2020-08-23 */ +/* Copyright (c) 2020 John Honniball, Froods Software Development */ + +#include +#include +#include +#include +#include +#include "hpgllib.h" + +void draw_petal(const double xc, const double yc, const double delta, const double theta, const double radius); + +int main(int argc, char * const argv[]) +{ + int opt; + int i; + double xc, yc; + double maxx, maxy; + double delta; + int npetals = 8; + double radius; + + 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); + } + } + + if (plotbegin(1) < 0) { + fputs("Failed to initialise HPGL library\n", stderr); + exit(EXIT_FAILURE); + } + + getplotsize(&maxx, &maxy); + + xc = maxx / 2.0; + yc = maxy / 2.0; + + delta = (2.0 * M_PI) / (double)npetals; + radius = (maxy / 3.0); + + for (i = 0; i < npetals; i++) { + const double theta = (double)i * delta; + + const double x = xc + (radius * cos(theta)); + const double y = yc + (radius * sin(theta)); + + moveto(xc, yc); + lineto(x, y); + } + + for (i = 0; i < npetals; i++) { + const double theta = delta * (double)i; + + draw_petal(xc, yc, delta, theta, radius); + } + + plotend(); + + return (0); +} + + +void draw_petal(const double xc, const double yc, const double delta, const double theta, const double radius) +{ + int i; + const double tipx = xc + (radius * cos(theta)); + const double tipy = yc + (radius * sin(theta)); + + for (i = 1; i <= 6; i++) { + const double len = 4.0 * 40.0 * (double)i; + const double offx = len * sin(theta); + const double offy = -len * cos(theta); + const double arcx = tipx + offx; + const double arcy = tipy + offy; + + const double baslen = len / tan(delta / 2.0); + const double basx = baslen * cos(theta); + const double basy = baslen * sin(theta); + + moveto(xc + basx + offx, yc + basy + offy); + lineto(arcx, arcy); + arc(tipx, tipy, 180.0); + lineto(xc + basx - offx, yc + basy - offy); + } +}