| 1 |
/************************************************************************* |
| 2 |
* Copyright (c) 2011 AT&T Intellectual Property |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* https://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: Details at https://graphviz.org |
| 9 |
*************************************************************************/ |
| 10 |
|
| 11 |
#include "config.h" |
| 12 |
|
| 13 |
#include <neatogen/geometry.h> |
| 14 |
#include <math.h> |
| 15 |
#include <stddef.h> |
| 16 |
|
| 17 |
double xmin, xmax, ymin, ymax; /* min and max x and y values of sites */ |
| 18 |
double deltax; // xmax - xmin |
| 19 |
|
| 20 |
size_t nsites; |
| 21 |
int sqrt_nsites; |
| 22 |
|
| 23 |
void geominit(void) |
| 24 |
{ |
| 25 |
double sn; |
| 26 |
|
| 27 |
sn = nsites + 4; |
| 28 |
sqrt_nsites = (int) sqrt(sn); |
| 29 |
} |
| 30 |
|
| 31 |
double dist_2(Point pp, Point qp) { |
| 32 |
const double dx = pp.x - qp.x; |
| 33 |
const double dy = pp.y - qp.y; |
| 34 |
|
| 35 |
return (dx * dx + dy * dy); |
| 36 |
} |
| 37 |
|
| 38 |
void subpt(Point * a, Point b, Point c) |
| 39 |
{ |
| 40 |
a->x = b.x - c.x; |
| 41 |
a->y = b.y - c.y; |
| 42 |
} |
| 43 |
|
| 44 |
void addpt(Point * c, Point a, Point b) |
| 45 |
{ |
| 46 |
c->x = a.x + b.x; |
| 47 |
c->y = a.y + b.y; |
| 48 |
} |
| 49 |
|
| 50 |
double area_2(Point a, Point b, Point c) |
| 51 |
{ |
| 52 |
return ((a.y - b.y) * (c.x - b.x) - (c.y - b.y) * (a.x - b.x)); |
| 53 |
} |
| 54 |
|
| 55 |
int leftOf(Point a, Point b, Point c) |
| 56 |
{ |
| 57 |
return (area_2(a, b, c) > 0); |
| 58 |
} |
| 59 |
|
| 60 |
int intersection(Point a, Point b, Point c, Point d, Point * p) |
| 61 |
{ |
| 62 |
double s, t; /* The two parameters of the parametric eqns. */ |
| 63 |
double denom; /* Denominator of solutions. */ |
| 64 |
|
| 65 |
denom = |
| 66 |
a.x * (d.y - c.y) + |
| 67 |
b.x * (c.y - d.y) + d.x * (b.y - a.y) + c.x * (a.y - b.y); |
| 68 |
|
| 69 |
/* If denom is zero, then the line segments are parallel. */ |
| 70 |
/* In this case, return false even though the segments might overlap. */ |
| 71 |
if (denom == 0.0) |
| 72 |
return 0; |
| 73 |
|
| 74 |
s = (a.x * (d.y - c.y) + c.x * (a.y - d.y) + d.x * (c.y - a.y) |
| 75 |
) / denom; |
| 76 |
t = -(a.x * (c.y - b.y) + b.x * (a.y - c.y) + c.x * (b.y - a.y) |
| 77 |
) / denom; |
| 78 |
|
| 79 |
p->x = a.x + s * (b.x - a.x); |
| 80 |
p->y = a.y + s * (b.y - a.y); |
| 81 |
|
| 82 |
if ((0.0 <= s) && (s <= 1.0) && (0.0 <= t) && (t <= 1.0)) |
| 83 |
return 1; |
| 84 |
else |
| 85 |
return 0; |
| 86 |
} |