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