Graphviz 14.1.2~dev.20260123.1158
Loading...
Searching...
No Matches
find_ints.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 "config.h"
12
13#include "simple.h"
14#include <stdio.h>
15#include <stdlib.h>
16#include <util/alloc.h>
17#include <util/exit.h>
18#include <util/list.h>
19
20static int gt(const void *a, const void *b);
21
22void find_ints(struct vertex vertex_list[], size_t nvertices,
23 intersections_t *ilist) {
24 int k;
25 LIST(struct active_edge *) all = {.dtor = LIST_DTOR_FREE};
26 struct active_edge *tempa;
27 struct vertex *pt1, *pt2, *templ;
28
29 struct vertex **pvertex = gv_calloc(nvertices, sizeof(struct vertex*));
30
31 for (size_t i = 0; i < nvertices; i++)
32 pvertex[i] = vertex_list + i;
33
34/* sort vertices by x coordinate */
35 qsort(pvertex, nvertices, sizeof(struct vertex *), gt);
36
37/* walk through the vertices in order of increasing x coordinate */
38 for (size_t i = 0; i < nvertices; i++) {
39 pt1 = pvertex[i];
40 templ = pt2 = prior(pvertex[i]);
41 for (k = 0; k < 2; k++) { /* each vertex has 2 edges */
42 switch (gt(&pt1, &pt2)) {
43
44 case -1: /* forward edge, test and insert */
45 for (size_t j = 0; j < LIST_SIZE(&all); ++j) {
46 tempa = LIST_GET(&all, j);
47 find_intersection(tempa->name, templ, ilist); // test
48 }
49
50 struct active_edge *new = gv_alloc(sizeof(struct active_edge));
51 LIST_APPEND(&all, new);
52
53 new->name = templ;
54 templ->active = new;
55 break;
56
57 case 1: /* backward edge, delete */
58
59 if ((tempa = templ->active) == NULL) {
60 fprintf(stderr,
61 "\n***ERROR***\n trying to delete a non line\n");
63 }
64 LIST_REMOVE(&all, tempa);
65 templ->active = NULL;
66 break;
67
68 default:
69 break;
70 }
71
72 pt2 = after(pvertex[i]);
73 templ = pvertex[i]; /*second neighbor */
74 }
75 }
76 LIST_FREE(&all);
77 free(pvertex);
78}
79
80static int gt(const void *a, const void *b) {
81 const struct vertex *const *i = a;
82 const struct vertex *const *j = b;
83 if ((*i)->pos.x > (*j)->pos.x) {
84 return 1;
85 }
86 if ((*i)->pos.x < (*j)->pos.x) {
87 return -1;
88 }
89 if ((*i)->pos.y > (*j)->pos.y) {
90 return 1;
91 }
92 if ((*i)->pos.y < (*j)->pos.y) {
93 return -1;
94 }
95 return 0;
96}
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
Definition alloc.h:26
static void * gv_alloc(size_t size)
Definition alloc.h:47
static NORETURN void graphviz_exit(int status)
Definition exit.h:23
static int gt(const void *a, const void *b)
Definition find_ints.c:80
void find_ints(struct vertex vertex_list[], size_t nvertices, intersections_t *ilist)
Definition find_ints.c:22
void free(void *)
node NULL
Definition grammar.y:181
#define after(v)
Definition legal.c:29
static bool find_intersection(vertex *l, vertex *m)
Definition legal.c:217
#define prior(v)
Definition legal.c:30
type-generic dynamically expanding list
#define LIST_DTOR_FREE
Definition list.h:70
#define LIST(type)
Definition list.h:55
#define LIST_SIZE(list)
Definition list.h:80
#define LIST_APPEND(list, item)
Definition list.h:120
#define LIST_FREE(list)
Definition list.h:370
#define LIST_REMOVE(list, item)
Definition list.h:218
#define LIST_GET(list, index)
Definition list.h:155
struct vertex * name
Definition simple.h:44
Definition legal.c:34
struct vertex * active
Definition legal.c:37