Graphviz 12.0.1~dev.20240716.0800
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 <cgraph/alloc.h>
15#include <cgraph/exit.h>
16
17static int gt(const void *a, const void *b);
18
19void find_ints(struct vertex vertex_list[],
20 struct data *input,
21 struct intersection ilist[])
22{
23 int j, k;
24 struct active_edge_list all;
25 struct active_edge *tempa;
26 struct vertex *pt1, *pt2, *templ;
27
28 input->ninters = 0;
29 all.first = all.final = NULL;
30 all.number = 0;
31
32 struct vertex **pvertex = gv_calloc(input->nvertices, sizeof(struct vertex*));
33
34 for (size_t i = 0; i < input->nvertices; i++)
35 pvertex[i] = vertex_list + i;
36
37/* sort vertices by x coordinate */
38 qsort(pvertex, input->nvertices, sizeof(struct vertex *), gt);
39
40/* walk through the vertices in order of increasing x coordinate */
41 for (size_t i = 0; i < input->nvertices; i++) {
42 pt1 = pvertex[i];
43 templ = pt2 = prior(pvertex[i]);
44 for (k = 0; k < 2; k++) { /* each vertex has 2 edges */
45 switch (gt(&pt1, &pt2)) {
46
47 case -1: /* forward edge, test and insert */
48
49 for (tempa = all.first, j = 0; j < all.number;
50 j++, tempa = tempa->next)
51 find_intersection(tempa->name, templ, ilist, input); /* test */
52
53 struct active_edge *new = gv_alloc(sizeof(struct active_edge));
54 if (all.number == 0) {
55 all.first = new;
56 new->last = NULL;
57 } /* insert */
58 else {
59 all.final->next = new;
60 new->last = all.final;
61 }
62
63 new->name = templ;
64 new->next = NULL;
65 templ->active = new;
66 all.final = new;
67 all.number++;
68
69 break; /* end of case -1 */
70
71 case 1: /* backward edge, delete */
72
73 if ((tempa = templ->active) == NULL) {
74 fprintf(stderr,
75 "\n***ERROR***\n trying to delete a non line\n");
77 }
78 if (all.number == 1)
79 all.final = all.first = NULL; /* delete the line */
80 else if (tempa == all.first) {
81 all.first = all.first->next;
82 all.first->last = NULL;
83 } else if (tempa == all.final) {
84 all.final = all.final->last;
85 all.final->next = NULL;
86 } else {
87 tempa->last->next = tempa->next;
88 tempa->next->last = tempa->last;
89 }
90 free(tempa);
91 all.number--;
92 templ->active = NULL;
93 break; /* end of case 1 */
94
95 default:
96 break;
97 } /* end switch */
98
99 pt2 = after(pvertex[i]);
100 templ = pvertex[i]; /*second neighbor */
101 } /* end k for loop */
102 } /* end i for loop */
103 free(pvertex);
104}
105
106static int gt(const void *a, const void *b) {
107 const struct vertex *const *i = a;
108 const struct vertex *const *j = b;
109 if ((*i)->pos.x > (*j)->pos.x) {
110 return 1;
111 }
112 if ((*i)->pos.x < (*j)->pos.x) {
113 return -1;
114 }
115 if ((*i)->pos.y > (*j)->pos.y) {
116 return 1;
117 }
118 if ((*i)->pos.y < (*j)->pos.y) {
119 return -1;
120 }
121 return 0;
122}
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:106
void find_ints(struct vertex vertex_list[], struct data *input, struct intersection ilist[])
Definition find_ints.c:19
void free(void *)
node NULL
Definition grammar.y:149
#define after(v)
Definition legal.c:25
static int find_intersection(vertex *l, vertex *m)
Definition legal.c:233
#define prior(v)
Definition legal.c:26
active_edge * final
Definition legal.c:47
active_edge * first
Definition legal.c:47
vertex * name
Definition legal.c:43
struct active_edge * next
Definition legal.c:44
struct active_edge * last
Definition legal.c:44
Definition legal.c:50
int ninters
Definition legal.c:51
int nvertices
Definition legal.c:51
Definition legal.c:31
active_edge * active
Definition legal.c:34