Skip to content

Commit

Permalink
New drawing: octoflower, an 8-petal flower. Could be made parametric …
Browse files Browse the repository at this point in the history
…(number of petals, number of layers). Does not scale properly yet. Draws each petal in turn, whereas it may look better of we draw all the petals for one layer, then the next layer, an so on.
  • Loading branch information
anachrocomputer committed Aug 23, 2020
1 parent c6ddf45 commit e6802a3
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ lissajous
lobe
lotus
morphpoly
octoflower
op
op_moire
pappus
Expand All @@ -46,6 +47,7 @@ piscis
pin_and_cotton
pin_circle
plottext
poly_oval
qrplot
rtree
spiralsq
Expand All @@ -56,6 +58,5 @@ superellipse
sutpent
ternary_arcs
tree
poly_oval
twist
zigzag
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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 $@

Expand Down
97 changes: 97 additions & 0 deletions octoflower.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/* octoflower --- plot an eight-petal flower 2020-08-23 */
/* Copyright (c) 2020 John Honniball, Froods Software Development */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#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 <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);
}

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);
}
}

0 comments on commit e6802a3

Please sign in to comment.