2012-05-05 03:40:55 +03:00
|
|
|
/*
|
|
|
|
* p2d_attrib.c - Determine various polygon attributes
|
|
|
|
*
|
2015-01-11 02:16:47 +02:00
|
|
|
* Written 2012, 2015 by Werner Almesberger
|
|
|
|
* Copyright 2012, 2015 Werner Almesberger
|
2012-05-05 03:40:55 +03:00
|
|
|
*
|
2015-01-19 01:43:08 +02:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2012-05-05 03:40:55 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2015-01-11 02:16:47 +02:00
|
|
|
#include <stdbool.h>
|
2015-01-19 07:57:40 +02:00
|
|
|
#include <stdio.h>
|
2012-05-05 03:40:55 +03:00
|
|
|
#include <math.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "poly2d.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Angle in counter-clockwise direction to turn at point B when coming from A
|
|
|
|
* in order to face towards C.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static double angle_3(const struct v2d *a, const struct v2d *b,
|
|
|
|
const struct v2d *c)
|
|
|
|
{
|
|
|
|
double ax, ay, bx, by;
|
2015-01-19 07:57:40 +02:00
|
|
|
double cs, aa, bb;
|
2015-01-19 01:27:26 +02:00
|
|
|
double cross, angle;
|
2012-05-05 03:40:55 +03:00
|
|
|
|
2015-01-19 01:17:58 +02:00
|
|
|
ax = b->x - a->x;
|
|
|
|
ay = b->y - a->y;
|
|
|
|
bx = c->x - b->x;
|
|
|
|
by = c->y - b->y;
|
2012-05-05 03:40:55 +03:00
|
|
|
|
2015-01-19 01:27:26 +02:00
|
|
|
cross = ax * by - ay * bx;
|
|
|
|
if (!cross)
|
|
|
|
return 0;
|
|
|
|
|
2012-05-05 03:40:55 +03:00
|
|
|
aa = hypot(ax, ay);
|
|
|
|
bb = hypot(bx, by);
|
|
|
|
|
2015-01-19 07:57:40 +02:00
|
|
|
cs = (ax * bx + ay * by) / aa / bb;
|
|
|
|
if (cs <= -1)
|
|
|
|
angle = 180;
|
|
|
|
else if (cs >= 1)
|
|
|
|
angle = 0;
|
|
|
|
else
|
|
|
|
angle = acos(cs) / M_PI * 180.0;
|
2015-01-19 01:27:26 +02:00
|
|
|
return cross >= 0 ? angle : -angle;
|
2012-05-05 03:40:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we predominantly turn to the right, then the path must be clockwise.
|
|
|
|
*/
|
|
|
|
|
2015-01-11 02:16:47 +02:00
|
|
|
bool p2d_is_cw(const struct p2d *p)
|
2012-05-05 03:40:55 +03:00
|
|
|
{
|
|
|
|
const struct v2d *v;
|
|
|
|
double a = 0;
|
|
|
|
|
|
|
|
assert(p2d_vertices(p) >= 3);
|
|
|
|
assert(p2d_is_closed(p));
|
|
|
|
assert(p2d_no_intersect(p));
|
|
|
|
|
|
|
|
v = p->v;
|
|
|
|
do {
|
|
|
|
a += angle_3(v, v->next, v->next->next);
|
|
|
|
v = v->next;
|
|
|
|
}
|
|
|
|
while (v != p->v);
|
2015-01-19 07:59:43 +02:00
|
|
|
assert(!isnan(a)); /* require -std=c99 or -std=gnu99 */
|
2012-05-05 03:40:55 +03:00
|
|
|
return a < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-11 02:16:47 +02:00
|
|
|
bool p2d_is_closed(const struct p2d *p)
|
2012-05-05 03:40:55 +03:00
|
|
|
{
|
|
|
|
return p->v == p->last || p->last->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Known bug: if the polygon intersects on a vertex, the intersection may
|
|
|
|
* go unnoticed.
|
|
|
|
*/
|
|
|
|
|
2015-01-11 02:16:47 +02:00
|
|
|
bool p2d_no_intersect(const struct p2d *p)
|
2012-05-05 03:40:55 +03:00
|
|
|
{
|
|
|
|
const struct v2d *v, *u;
|
|
|
|
|
|
|
|
v = p->v;
|
|
|
|
while (v) {
|
|
|
|
u = v->next;
|
|
|
|
if (!u || u == p->v)
|
|
|
|
return 1;
|
|
|
|
u = u->next;
|
|
|
|
if (!u || u == p->v)
|
|
|
|
return 1;
|
2015-01-18 14:09:43 +02:00
|
|
|
while (u && u->next && u->next != v) {
|
2012-05-05 03:40:55 +03:00
|
|
|
if (v2d_intersect(v, v->next, u, u->next,
|
|
|
|
NULL, NULL) > 0)
|
|
|
|
return 0;
|
|
|
|
u = u->next;
|
|
|
|
if (u == p->v)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
v = v->next;
|
|
|
|
if (v == p->v)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-11 02:16:47 +02:00
|
|
|
unsigned p2d_vertices(const struct p2d *p)
|
2012-05-05 03:40:55 +03:00
|
|
|
{
|
|
|
|
const struct v2d *v;
|
2015-01-11 02:16:47 +02:00
|
|
|
unsigned n = 0;
|
2012-05-05 03:40:55 +03:00
|
|
|
|
|
|
|
v = p->v;
|
|
|
|
while (v) {
|
|
|
|
n++;
|
|
|
|
v = v->next;
|
|
|
|
if (v == p->v)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|