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