00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <render.h>
00019 #include "simple.h"
00020 #include <stdlib.h>
00021
00022 extern void
00023 find_intersection(struct vertex *l,
00024 struct vertex *m,
00025 struct intersection ilist[], struct data *input);
00026 static int gt(struct vertex **i, struct vertex **j);
00027
00028 void
00029 find_ints(struct vertex vertex_list[],
00030 struct polygon polygon_list[],
00031 struct data *input, struct intersection ilist[])
00032 {
00033 int i, j, k;
00034 struct active_edge_list all;
00035 struct active_edge *new, *tempa;
00036 struct vertex *pt1, *pt2, *templ, **pvertex;
00037
00038 input->ninters = 0;
00039 all.first = all.final = 0;
00040 all.number = 0;
00041
00042 pvertex = N_GNEW(input->nvertices, struct vertex *);
00043
00044 for (i = 0; i < input->nvertices; i++)
00045 pvertex[i] = vertex_list + i;
00046
00047
00048 qsort(pvertex, input->nvertices, sizeof(struct vertex *),
00049 (int (*)(const void *, const void *))gt);
00050
00051
00052 for (i = 0; i < input->nvertices; i++) {
00053 pt1 = pvertex[i];
00054 templ = pt2 = prior(pvertex[i]);
00055 for (k = 0; k < 2; k++) {
00056 switch (gt(&pt1, &pt2)) {
00057
00058 case -1:
00059
00060 for (tempa = all.first, j = 0; j < all.number;
00061 j++, tempa = tempa->next)
00062 find_intersection(tempa->name, templ, ilist, input);
00063
00064 new = GNEW(struct active_edge);
00065 if (all.number == 0) {
00066 all.first = new;
00067 new->last = 0;
00068 }
00069 else {
00070 all.final->next = new;
00071 new->last = all.final;
00072 }
00073
00074 new->name = templ;
00075 new->next = 0;
00076 templ->active = new;
00077 all.final = new;
00078 all.number++;
00079
00080 break;
00081
00082 case 1:
00083
00084 if ((tempa = templ->active) == 0) {
00085 agerr(AGERR, "trying to delete a non line\n");
00086 exit(1);
00087 }
00088 if (all.number == 1)
00089 all.final = all.first = 0;
00090 else if (tempa == all.first) {
00091 all.first = all.first->next;
00092 all.first->last = 0;
00093 } else if (tempa == all.final) {
00094 all.final = all.final->last;
00095 all.final->next = 0;
00096 } else {
00097 tempa->last->next = tempa->next;
00098 tempa->next->last = tempa->last;
00099 }
00100 free((char *) tempa);
00101 all.number--;
00102 templ->active = 0;
00103 break;
00104
00105 }
00106
00107 pt2 = after(pvertex[i]);
00108 templ = pvertex[i];
00109 }
00110 }
00111 }
00112
00113 static int gt(struct vertex **i, struct vertex **j)
00114 {
00115 double t;
00116 if ((t = (*i)->pos.x - (*j)->pos.x) != 0.)
00117 return ((t > 0.) ? 1 : -1);
00118 if ((t = (*i)->pos.y - (*j)->pos.y) == 0.)
00119 return (0);
00120 else
00121 return ((t > 0.) ? 1 : -1);
00122 }