polygons with variably many vertices

This commit is contained in:
Florian Stecker 2019-01-29 16:50:33 +01:00
parent 78ad483918
commit 587a96e54a

View File

@ -23,7 +23,7 @@ void drawPoint(DrawingContext *ctx, point_t p);
void drawSegment(DrawingContext *ctx, point_t a, point_t b);
void drawLine(DrawingContext *ctx, point_t a, point_t b);
void drawImplicitLine(DrawingContext *ctx, double a, double b, double c);
void drawPolygon(DrawingContext *ctx, int sides, point_t a, point_t b, point_t c, point_t d);
void drawPolygon(DrawingContext *ctx, int sides, ...);
void drawAttractorConnection(DrawingContext *ctx, const char *word1, const char *word2);
void drawBox(DrawingContext *ctx, const char *word1, const char *word2);
void drawBoxStd(DrawingContext *ctx, const char *word, char base);
@ -151,12 +151,23 @@ void drawImplicitLine(DrawingContext *ctx, double a, double b, double c)
drawSegment(ctx, xminus, xplus);
}
void drawPolygon(DrawingContext *ctx, int sides, point_t a, point_t b, point_t c, point_t d) // only quadrilaterals so far
void drawPolygon(DrawingContext *ctx, int sides, ...)
{
drawSegment(ctx, a, b);
drawSegment(ctx, b, c);
drawSegment(ctx, c, d);
drawSegment(ctx, d, a);
va_list args;
point_t first, prev, current;
va_start(args, sides);
first = va_arg(args, point_t);
current = first;
for(int i = 0; i < sides-1; i++) {
prev = current;
current = va_arg(args, point_t);
drawSegment(ctx, prev, current);
}
drawSegment(ctx, current, first);
va_end(args);
}
int fixedPoints(const char *word, point_t *out)
@ -189,6 +200,14 @@ void drawAttractorConnection(DrawingContext *ctx, const char *word1, const char
drawSegment(ctx, p1[0], p2[0]);
}
void drawTriangle(DrawingContext *ctx, const char *word)
{
point_t p[3];
fixedPoints(word, p);
drawPolygon(ctx, 3, p[0], p[1], p[2]);
}
void drawBox(DrawingContext *ctx, const char *word1, const char *word2)
{
point_t p[2][3],i[2];
@ -293,6 +312,18 @@ void drawBoxes(DrawingContext *ctx)
{
cairo_t *C = ctx->cairo;
cairo_set_source_rgb(C, 1, 0, 0);
drawTriangle(ctx, "abc");
cairo_set_source_rgb(C, 0, 0, 1);
drawTriangle(ctx, "aca abc aca");
drawTriangle(ctx, "acac abc caca");
drawTriangle(ctx, "acaca abc acaca");
cairo_set_source_rgb(C, 0, 0.8, 0);
drawTriangle(ctx, "cac abc cac");
drawTriangle(ctx, "caca abc acac");
drawTriangle(ctx, "cacac abc cacac");
/*
cairo_set_source_rgb(C, 1, 0, 0);
drawBoxStd(ctx, "c", 'C');
drawBoxStd(ctx, "", 'B');
@ -307,7 +338,7 @@ void drawBoxes(DrawingContext *ctx)
drawBoxStd(ctx, "acac", 'C');
drawBoxStd(ctx, "aca", 'A');
drawBoxStd(ctx, "ac", 'C');
/*
drawBoxStd(ctx, "aca cb", 'B');
drawBoxStd(ctx, "aca cbc", 'C');
drawBoxStd(ctx, "aca cbcb", 'B');
@ -321,7 +352,6 @@ void drawBoxes(DrawingContext *ctx)
drawBoxStd(ctx, "caca bcbc", 'C');
drawBoxStd(ctx, "caca bcb", 'B');
drawBoxStd(ctx, "caca bc", 'C');
*/
cairo_set_source_rgb(C, 1, 0, 1);
drawBoxStd(ctx, "ca bc", 'C');
@ -339,6 +369,7 @@ void drawBoxes(DrawingContext *ctx)
drawBoxStd(ctx, "cabc abab", 'B');
drawBoxStd(ctx, "cabc aba", 'A');
drawBoxStd(ctx, "cabc ab", 'B');
*/
}
void drawReflectors(DrawingContext *ctx)