| 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/neato.h> |
| 14 |
#include <stdio.h> |
| 15 |
#include <neatogen/info.h> |
| 16 |
#include <stddef.h> |
| 17 |
#include <util/alloc.h> |
| 18 |
|
| 19 |
Info_t *nodeInfo; /* Array of node info */ |
| 20 |
|
| 21 |
/* returns -1 if p < q.p |
| 22 |
* 0 if p = q.p |
| 23 |
* 1 if p > q.p |
| 24 |
* if q if NULL, returns -1 |
| 25 |
* Ordering is by angle from -pi/2 to 3pi/4. |
| 26 |
* For equal angles (which should not happen in our context) |
| 27 |
* ordering is by closeness to origin. |
| 28 |
*/ |
| 29 |
static int compare(Point o, Point p, Point q) { |
| 30 |
double x0; |
| 31 |
double y0; |
| 32 |
double x1; |
| 33 |
double y1; |
| 34 |
double a, b; |
| 35 |
|
| 36 |
if (p.x == q.x && p.y == q.y) |
| 37 |
return 0; |
| 38 |
|
| 39 |
x0 = (double)p.x - (double)o.x; |
| 40 |
y0 = (double)p.y - (double)o.y; |
| 41 |
x1 = (double)q.x - (double)o.x; |
| 42 |
y1 = (double)q.y - (double)o.y; |
| 43 |
if (x0 >= 0.0) { |
| 44 |
if (x1 < 0.0) |
| 45 |
return -1; |
| 46 |
else if (x0 > 0.0) { |
| 47 |
if (x1 > 0.0) { |
| 48 |
a = y1 / x1; |
| 49 |
b = y0 / x0; |
| 50 |
if (b < a) |
| 51 |
return -1; |
| 52 |
else if (b > a) |
| 53 |
return 1; |
| 54 |
else if (x0 < x1) |
| 55 |
return -1; |
| 56 |
else |
| 57 |
return 1; |
| 58 |
} else { /* x1 == 0.0 */ |
| 59 |
if (y1 > 0.0) |
| 60 |
return -1; |
| 61 |
else |
| 62 |
return 1; |
| 63 |
} |
| 64 |
} else { /* x0 == 0.0 */ |
| 65 |
if (x1 > 0.0) { |
| 66 |
if (y0 <= 0.0) |
| 67 |
return -1; |
| 68 |
else |
| 69 |
return 1; |
| 70 |
} else { /* x1 == 0.0 */ |
| 71 |
if (y0 < y1) { |
| 72 |
if (y1 <= 0.0) |
| 73 |
return 1; |
| 74 |
else |
| 75 |
return -1; |
| 76 |
} else { |
| 77 |
if (y0 <= 0.0) |
| 78 |
return -1; |
| 79 |
else |
| 80 |
return 1; |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
} else { |
| 85 |
if (x1 >= 0.0) |
| 86 |
return 1; |
| 87 |
else { |
| 88 |
a = y1 / x1; |
| 89 |
b = y0 / x0; |
| 90 |
if (b < a) |
| 91 |
return -1; |
| 92 |
else if (b > a) |
| 93 |
return 1; |
| 94 |
else if (x0 > x1) |
| 95 |
return -1; |
| 96 |
else |
| 97 |
return 1; |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
void addVertex(Site * s, double x, double y) |
| 103 |
{ |
| 104 |
Info_t *ip; |
| 105 |
const Point origin_point = s->coord; |
| 106 |
|
| 107 |
ip = nodeInfo + s->sitenbr; |
| 108 |
|
| 109 |
const Point tmp = {.x = x, .y = y}; |
| 110 |
|
| 111 |
size_t i; |
| 112 |
for (i = 0; i < ip->n_verts; ++i) { |
| 113 |
int cmp = compare(origin_point, tmp, ip->verts[i]); |
| 114 |
if (cmp == 0) { // we already know this vertex; ignore |
| 115 |
return; |
| 116 |
} |
| 117 |
if (cmp < 0) { // found where to insert this vertex |
| 118 |
break; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
ip->verts = gv_recalloc(ip->verts, ip->n_verts, ip->n_verts + 1, |
| 123 |
sizeof(ip->verts[0])); |
| 124 |
// shuffle existing entries upwards to make room |
| 125 |
memmove(&ip->verts[i + 1], &ip->verts[i], |
| 126 |
(ip->n_verts - i) * sizeof(ip->verts[0])); |
| 127 |
ip->verts[i] = tmp; |
| 128 |
++ip->n_verts; |
| 129 |
} |