~ubuntu-branches/ubuntu/lucid/graphviz/lucid-updates

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* $Id: wrapper.c,v 1.3 2004/12/11 19:26:11 ellson Exp $ $Revision: 1.3 $ */
/* vim:set shiftwidth=4 ts=8: */

/**********************************************************
*      This software is part of the graphviz package      *
*                http://www.graphviz.org/                 *
*                                                         *
*            Copyright (c) 1994-2004 AT&T Corp.           *
*                and is licensed under the                *
*            Common Public License, Version 1.0           *
*                      by AT&T Corp.                      *
*                                                         *
*        Information and Software Systems Research        *
*              AT&T Research, Florham Park NJ             *
**********************************************************/

#include <stdio.h>
#include <stdlib.h>
#include "simple.h"

/* this is all out of pathgeom.h and will eventually get included */

typedef struct Pxy_t {
    double x, y;
} Pxy_t;

typedef struct Pxy_t Ppoint_t;
typedef struct Pxy_t Pvector_t;

typedef struct Ppoly_t {
    Ppoint_t *ps;
    int pn;
} Ppoly_t;

typedef Ppoly_t Ppolyline_t;

typedef struct Pedge_t {
    Ppoint_t a, b;
} Pedge_t;


#ifdef NOTDEF
int main(int argc, char **argv)
{
    Ppoly_t polys[2], *polys_ptr[2];

    polys[0].ps = (Ppoint_t *) malloc(3 * sizeof(Ppoint_t));
    polys[1].ps = (Ppoint_t *) malloc(3 * sizeof(Ppoint_t));
    polys[0].pn = 3;
    polys[1].pn = 3;

    polys[0].ps[0].x = 0.0;
    polys[0].ps[0].y = 0.0;
    polys[0].ps[1].x = 0.0;
    polys[0].ps[1].y = 100.0;
    polys[0].ps[2].x = 100.0;
    polys[0].ps[2].y = 0.0;

    polys[1].ps[0].x = 70.0;
    polys[1].ps[0].y = 70.0;
    polys[1].ps[1].x = 70.0;
    polys[1].ps[1].y = 100.0;
    polys[1].ps[2].x = 100.0;
    polys[1].ps[2].y = 70.0;

    polys_ptr[0] = &polys[0];
    polys_ptr[1] = &polys[1];

    if (Plegal_arrangement(polys_ptr, 2))
	printf(" it is legal\n");
    else
	printf(" it is not legal\n");
}
#endif

#ifdef NOTDEF
struct vertex *after(struct vertex *v)
{
    if (v == v->poly->finish)
	return v->poly->start;
    else
	return v + 1;
}

struct vertex *before(struct vertex *v)
{
    if (v == v->poly->start)
	return v->poly->finish;
    else
	return v - 1;
}
#endif

void find_ints(struct vertex vertex_list[], struct polygon polygon_list[],
	       struct data *input, struct intersection ilist[]);

int Plegal_arrangement(Ppoly_t ** polys, int n_polys)
{

    int i, j, vno, nverts, rv;

    struct vertex *vertex_list;
    struct polygon *polygon_list;
    struct data input;
    struct intersection ilist[10000];

    polygon_list = (struct polygon *)
	malloc(n_polys * sizeof(struct polygon));

    for (i = nverts = 0; i < n_polys; i++)
	nverts += polys[i]->pn;

    vertex_list = (struct vertex *)
	malloc(nverts * sizeof(struct vertex));

    for (i = vno = 0; i < n_polys; i++) {
	polygon_list[i].start = &vertex_list[vno];
	for (j = 0; j < polys[i]->pn; j++) {
	    vertex_list[vno].pos.x = polys[i]->ps[j].x;
	    vertex_list[vno].pos.y = polys[i]->ps[j].y;
	    vertex_list[vno].poly = &polygon_list[i];
	    vno++;
	}
	polygon_list[i].finish = &vertex_list[vno - 1];
    }

    input.nvertices = nverts;
    input.npolygons = n_polys;

    find_ints(vertex_list, polygon_list, &input, ilist);


#define EQ_PT(v,w) (((v).x == (w).x) && ((v).y == (w).y))
    rv = 1;
    {
	int i;
	struct position vft, vsd, avft, avsd;
	for (i = 0; i < input.ninters; i++) {
	    vft = ilist[i].firstv->pos;
	    avft = after(ilist[i].firstv)->pos;
	    vsd = ilist[i].secondv->pos;
	    avsd = after(ilist[i].secondv)->pos;
	    if (((vft.x != avft.x) && (vsd.x != avsd.x)) ||
		((vft.x == avft.x) &&
		 !EQ_PT(vft, ilist[i]) &&
		 !EQ_PT(avft, ilist[i])) ||
		((vsd.x == avsd.x) &&
		 !EQ_PT(vsd, ilist[i]) && !EQ_PT(avsd, ilist[i]))) {
		rv = 0;
		fprintf(stderr, "\nintersection %d at %.3f %.3f\n",
			i, ilist[i].x, ilist[i].y);
		fprintf(stderr, "seg#1 : (%.3f, %.3f) (%.3f, %.3f)\n",
			(double) (ilist[i].firstv->pos.x)
			, (double) (ilist[i].firstv->pos.y)
			, (double) (after(ilist[i].firstv)->pos.x)
			, (double) (after(ilist[i].firstv)->pos.y));
		fprintf(stderr, "seg#2 : (%.3f, %.3f) (%.3f, %.3f)\n",
			(double) (ilist[i].secondv->pos.x)
			, (double) (ilist[i].secondv->pos.y)
			, (double) (after(ilist[i].secondv)->pos.x)
			, (double) (after(ilist[i].secondv)->pos.y));
	    }
	}
    }
    free(polygon_list);
    free(vertex_list);
    return rv;
}