Skip to content

Commit

Permalink
New drawing: poly_oval, a polygon made from multiple overlapping oval…
Browse files Browse the repository at this point in the history
…s. By default, a pentagon but should be possible to add command-line arguments to set number of sides and other variables.
  • Loading branch information
anachrocomputer committed Aug 6, 2020
1 parent 8848a49 commit d947011
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,6 @@ 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 @@ -20,7 +20,7 @@ HPGL=flake.hpgl op.hpgl dala1.hpgl hconic2.hpgl tree.hpgl dome.hpgl \
curve_stitching.hpgl cs_rosette.hpgl allover13.hpgl allover12.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
star_grid.hpgl ternary_arcs.hpgl hexspiral.hpgl poly_oval.hpgl

PNG=$(HPGL:.hpgl=.png)

Expand Down Expand Up @@ -131,6 +131,15 @@ ternary_arcs: ternary_arcs.o hpgllib.o
ternary_arcs.o: ternary_arcs.c hpgllib.h
$(CC) $(CFLAGS) -o $@ ternary_arcs.c

poly_oval.hpgl: poly_oval Makefile
./poly_oval $(TITLE) $(BOLDPEN) -o $@

poly_oval: poly_oval.o hpgllib.o
$(LD) -o $@ poly_oval.o hpgllib.o -lm

poly_oval.o: poly_oval.c hpgllib.h
$(CC) $(CFLAGS) -o $@ poly_oval.c

morphpoly.hpgl: morphpoly Makefile
./morphpoly $(TITLE) $(BOLDPEN) -o $@

Expand Down
109 changes: 109 additions & 0 deletions poly_oval.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/* poly_oval --- plot polygon of ovals 2020-08-05 */
/* 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"

struct Point {
double x;
double y;
};

void multi_oval(struct Point *p1, struct Point *p2, const double inner, const double outer, const int n);
void draw_oval(const struct Point *const p1, const struct Point *const p2, const struct Point *const vec90);

int main(int argc, char * const argv[])
{
int opt;
int i, j;
double xc, yc;
double maxx, maxy;
double delta;
int nsides = 5;
double outer = 40.0;
double inner = 15.0;
struct Point pt[16];
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);
}
}

plotbegin(1);

getplotsize(&maxx, &maxy);

xc = maxx / 2.0;
yc = maxy / 2.0;

delta = (2.0 * M_PI) / (double)nsides;
radius = (maxy / 2.0) - (outer * 40.0);

for (i = 0; i < nsides; i++) {
const double theta = (double)i * delta;

pt[i].x = xc + (radius * cos(theta));
pt[i].y = yc + (radius * sin(theta));
}

for (i = 0; i < nsides; i++) {
j = (i + 1) % nsides;

multi_oval(&pt[i], &pt[j], inner * 40.0, outer * 40.0, 12);
}

plotend();

return (0);
}


void multi_oval(struct Point *p1, struct Point *p2, const double inner, const double outer, const int n)
{
int i;
struct Point vec;
struct Point vec90;
const double dx = p1->x - p2->x;
const double dy = p1->y - p2->y;
const double mag = sqrt((dx * dx) + (dy * dy));
const double delta_r = (outer - inner) / (double)(n - 1);

vec.x = dx / mag;
vec.y = dy / mag;

for (i = 0; i < n; i++) {
const double r = inner + (i * delta_r);

vec90.x = vec.y * r;
vec90.y = -vec.x * r;

draw_oval(p1, p2, &vec90);
}
}


void draw_oval(const struct Point *const p1, const struct Point *const p2, const struct Point *const vec90)
{
moveto(p1->x + vec90->x, p1->y + vec90->y);
arc(p1->x, p1->y, 180.0);
lineto(p2->x - vec90->x, p2->y - vec90->y);
arc(p2->x, p2->y, 180.0);
lineto(p1->x + vec90->x, p1->y + vec90->y);
}

0 comments on commit d947011

Please sign in to comment.