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