summaryrefslogtreecommitdiffstats
path: root/voronoi/voronoi.java
blob: f699e73322ce3acd10a5fe07c9323cfe21b25cb8 (plain) (blame)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
package voronoi;

//code kommt von http://shaneosullivan.wordpress.com/2007/04/05/fortunes-sweep-line-voronoi-algorithm-implemented-in-java/

/*
 * The author of this software is Steven Fortune.  Copyright (c) 1994 by AT&T
 * Bell Laboratories.
 * Permission to use, copy, modify, and distribute this software for any
 * purpose without fee is hereby granted, provided that this entire notice
 * is included in all copies of any software which is or includes a copy
 * or modification of this software and in all copies of the supporting
 * documentation for such software.
 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHORS NOR AT&T MAKE ANY
 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
 */

/* 
 * This code was originally written by Stephan Fortune in C code.  I, Shane O'Sullivan, 
 * have since modified it, encapsulating it in a C++ class and, fixing memory leaks and 
 * adding accessors to the Voronoi Edges.
 * Permission to use, copy, modify, and distribute this software for any
 * purpose without fee is hereby granted, provided that this entire notice
 * is included in all copies of any software which is or includes a copy
 * or modification of this software and in all copies of the supporting
 * documentation for such software.
 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHORS NOR AT&T MAKE ANY
 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
 */

/* 
 * Java Version by Zhenyu Pan
 * Permission to use, copy, modify, and distribute this software for any
 * purpose without fee is hereby granted, provided that this entire notice
 * is included in all copies of any software which is or includes a copy
 * or modification of this software and in all copies of the supporting
 * documentation for such software.
 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHORS NOR AT&T MAKE ANY
 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
 */

import helper.ListBTS;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import DataStructure.GSMMap;

class Freenode {
	Freenode nextfree;

	Freenode getnext() {
		return nextfree;
	}

	public void setnext(Freenode newf) {
		nextfree = new Freenode();
		nextfree = newf;
	}
}

class Freelist {
	public Freenode head;

	Freelist() {
		head = null;
	}

	public void free() {
		while (head != null) {
			head = head.nextfree;
		}
	}
}

/**
 * Punkt im Raum (double)
 * 
 * 
 */
class Point {
	double x, y;

	public Point() {
	}

	public Point(int x, int y) {
		this.x = x;
		this.y = y;
	}

	public Point(double x, double y) {
		this.x = x;
		this.y = y;
	}

	public void setPoint(double x, double y) {
		this.x = x;
		this.y = y;
	}
}

/**
 * used both for sites and for vertices. its a point: Site.Point Vertices auf
 * deutsch: Eckpunkt, Ecke, Knoten
 * 
 * 
 */

class Site {
	Point coord;

	int sitenumbr;

	public Site() {
		coord = new Point();
	}

	/**
	 * Richy. True if s matches current Site
	 * 
	 * @param s
	 * @return
	 */
	public boolean equals(Site s) {
		return ((int) s.coord.x == (int) this.coord.x && (int) s.coord.y == (int) this.coord.y);
	}
}

/**
 * Stores 4 points in 2 arrays. EP: Endpoints: Lines reg: the two points this
 * line bisects! The two points this line belongs to
 * 
 */
class Edge {
	public double a = 0, b = 0, c = 0;

	Site[] ep;

	Site[] reg;

	int edgenumbr;

	Edge() {
		ep = new Site[2];
		reg = new Site[2];
	}
}

/**
 * Two points in 2d space make a line WATCHPOINT here!
 */
class GraphEdge {
	public double x1, y1, x2, y2;
	public Site correspondingPt;

	GraphEdge next;
}

/**
 * Speichert zu jeder HalfEdge weitere HalfEdges für Links und Rechts. Speichert
 * zusätzlich eine Edge:ELedge Speichert auch ein vertex: vertex
 * 
 * @author richy
 * 
 */
class Halfedge {
	Halfedge ELleft, ELright;

	Edge ELedge;

	boolean deleted;

	int ELpm;

	Site vertex;

	double ystar;

	Halfedge PQnext;

	Halfedge() {
		PQnext = null;
	}
}

class Hfreelist extends Freelist {
	Halfedge hfl;

	Hfreelist() {
		hfl = new Halfedge();
	}
}

class Sfreelist extends Freelist {
	Site sfl;

	Sfreelist() {
		sfl = new Site();
	}
}

class Efreelist extends Freelist {
	Edge efl;

	Efreelist() {
		efl = new Edge();
	}
}

public class voronoi {

	double borderMinX, borderMaxX, borderMinY, borderMaxY;

	int siteidx;
	// ArrayList<PointAndLines> corresponding = new ArrayList<PointAndLines>();
	// // richy
	// ArrayList<PointAndVertices> polygon = new ArrayList<PointAndVertices>();
	// // richy
	ArrayList<Edge> edges = new ArrayList<Edge>(); // edges sortieren!!!

	int zoomfactor; // richy

	boolean isGenerated = false;

	double xmin, xmax, ymin, ymax, deltax, deltay;

	int nvertices;

	int nedges;

	int nsites;

	Hfreelist hfl;

	Efreelist efl;

	Sfreelist sfl;

	Site[] sites; // watchpoint here!

	Site bottomsite;

	int sqrt_nsites;

	double minDistanceBetweenSites;

	int PQcount;

	int PQmin;

	int PQhashsize;

	Halfedge PQhash[];

	public static int le = 0;

	public static int re = 1;

	int ELhashsize;

	Halfedge ELhash[];

	Halfedge ELleftend, ELrightend;

	GraphEdge allEdges;

	GraphEdge iteratorEdges;

	Boolean VorSim;

	Graphics g;

	Canvas c;

	int w;

	int h;

	double lasty;

	public voronoi() {
		siteidx = 0;
		sites = null;

		allEdges = null;
		iteratorEdges = null;
		minDistanceBetweenSites = 0;

		VorSim = false;
		g = null;
		c = null;
		w = h = 0;
		lasty = 0;
	}

	public Point2D getNearestPoint(int x, int y) {
		if (!isGenerated) {
			generateVoronoi(0, 2000, 0, 2000);
		}
		double dist = Double.MAX_VALUE;
		double temp_dist = Double.MAX_VALUE;
		double distX = 0;
		double distY = 0;
		int index = 0;
		for (int i = sites.length - 1; i >= 0; i--) {
			distX = x - sites[i].coord.x;
			distY = y - sites[i].coord.y;
			temp_dist = Math.sqrt(Math.pow(distX, 2) + Math.pow(distY, 2));
			if (temp_dist < dist) {
				dist = temp_dist;
				index = i;
			}
		}
		Point2D result = new Point2D.Double();
		result.setLocation(sites[index].coord.x, sites[index].coord.y);
		return result;
	}

	/**
	 * Calculates the area of the given polygon
	 * 
	 * @param poly
	 * @return area of poly
	 */
	public double area(Path2D poly) {
		try {
			double area = 0;
			ArrayList<Point> points = new ArrayList<Point>(20);
			PathIterator itr = poly.getPathIterator(null);
			// make list out of polygon. see
			// http://anklick-bar.de/matheprojekt/kurbel-gauss.pdf
			double[] itrresult = new double[6];
			while (!itr.isDone()) {

				itr.currentSegment(itrresult);
				points.add(new Point(itrresult[0], itrresult[1]));
				itr.next();
			}
			Point[] list = new Point[points.size() + 2];

			for (int i = 0; i < points.size(); i++) {
				list[i] = points.get(i);
			}
			list[points.size()] = points.get(0);
			list[points.size() + 1] = points.get(1);
			for (int i = 1; i <= points.size(); i++) {
				// area of polygon is left to the line
				// area = area + (list[i + 1].y - list[i - 1].y) * list[i].x;
				area = area + (list[i + 1].x - list[i - 1].x) * list[i].y;
			}

			// testing: count number of pixel that are in the polygon
			// boolean[][] containing = new boolean[poly.getBounds().width][poly
			// .getBounds().height];
			// int x = poly.getBounds().x;
			// int y = poly.getBounds().y;
			// int xlimit = poly.getBounds().width;
			// int ylimit = poly.getBounds().height;
			// int area2 = 0;
			// for (int xi = 0; xi <= xlimit; xi++) {
			// for (int yi = 0; yi <= ylimit; yi++) {
			// if (poly.contains(xi + x, yi + y)) {
			// area2++;
			// }
			// }
			// }
			// System.out.println("Fläche mit Pixeln: " + area2);
			// System.out.flush();

			return area / 2;
		} catch (Exception e) {
			return 0;
		}

	}

	/*
	 * public Polygon crossSection(Polygon poly1, Polygon poly2) { // always
	 * check two points. if both are within the first polygon, take // them into
	 * the new polygon. If one is inside, the other is // outside,take inside
	 * point+intersection point.
	 * 
	 * for (int i = 0; i < poly2.npoints - 1; i++) { Point pt1 = new
	 * Point(poly2.xpoints[i], poly2.ypoints[i]); Point pt2 = new
	 * Point(poly2.xpoints[i + 1], poly2.ypoints[i + 1]); ArrayList<Point>
	 * output = new ArrayList<Point>(); if (poly1.contains(pt1.x, pt1.y)) { if
	 * (poly1.contains(pt2.x, pt2.y)) { // both points in poly1:add
	 * output.add(pt1); output.add(pt2); } else { // pt1 is in polygon, pt2
	 * not.add pt1 and intersection with // poly1 // poly1. } } }
	 * 
	 * // check points on poly2 that are within poly1 ArrayList<Point>
	 * containingPTs = new ArrayList<Point>(poly2.npoints); for (int i = 0; i <
	 * poly2.npoints; i++) { if (poly1.contains(poly2.xpoints[i],
	 * poly2.ypoints[i])) { containingPTs .add(new Point(poly2.xpoints[i],
	 * poly2.ypoints[i])); } } return null; }
	 */

	public double intersectArea(Path2D poly1, Path2D poly2) {
		// check the bounding box!
		Rectangle2D box = getBox(poly1, poly2);
		if (box.isEmpty())
			return 0;
		if (box.getWidth() < 0.00001 || box.getHeight() < 0.00001) {
			return box.getHeight() * box.getWidth();
		}
		double area = 0;
		// double maxX = box.getMaxX();
		// double minX = box.getMinX();
		// double maxY = box.getMaxY();
		// double minY = box.getMinY();
		double stepX = (box.getMaxX() - box.getMinX()) / 42; // accuracy
		double stepY = (box.getMaxY() - box.getMinY()) / 42; // accuracy

		for (double x = box.getMinX(); x <= box.getMaxX(); x += stepX) {
			for (double y = box.getMinY(); y <= box.getMaxY(); y += stepY) {
				if (poly1.contains(x, y) && poly2.contains(x, y)) {
					area += stepX * stepY;
				}
			}
		}
		// for (int x = box.x; x <= box.width + box.x; x++) {
		// for (int y = box.y; y <= box.height + box.y; y++) {
		// if (poly1.contains(x, y) && poly2.contains(x, y))
		// area++;
		// hier darf doch kein int drüberlaufen.klar, dass die fläche
		// dann nicht gemessen werden kann!
		// System.
		// }
		// }
		return area;
	}

	public double drawIntersectArea(Path2D poly1, Path2D poly2, Graphics2D g) {
		// check the bounding box!
		Rectangle2D box = getBox(poly1, poly2);
		if (box.isEmpty())
			return 0;
		if (box.getWidth() < 0.00001 || box.getHeight() < 0.00001) {
			return box.getHeight() * box.getWidth();
		}
		double area = 0;
		// double maxX = box.getMaxX();
		// double minX = box.getMinX();
		// double maxY = box.getMaxY();
		// double minY = box.getMinY();
		double stepX = (box.getMaxX() - box.getMinX()) / 50;
		double stepY = (box.getMaxY() - box.getMinY()) / 50;
		stepX = 1;
		stepY = 1;
		Random r = new Random();
		g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
		for (double x = box.getMinX(); x <= box.getMaxX(); x += stepX) {
			for (double y = box.getMinY(); y <= box.getMaxY(); y += stepY) {
				if (poly1.contains(x, y) && poly2.contains(x, y)) {
					area += stepX * stepY;
					g.fillRect((int) x, (int) y, 1, 1);
				}
			}
		}
		// for (int x = box.x; x <= box.width + box.x; x++) {
		// for (int y = box.y; y <= box.height + box.y; y++) {
		// if (poly1.contains(x, y) && poly2.contains(x, y))
		// area++;
		// hier darf doch kein int drüberlaufen.klar, dass die fläche
		// dann nicht gemessen werden kann!
		// System.
		// }
		// }
		return area;
	}

	private Rectangle2D getBox(Path2D poly1, Path2D poly2) {

		// TODO Auto-generated method stub
		Rectangle2D rect1 = poly1.getBounds2D();
		Rectangle2D rect2 = poly2.getBounds2D();
		Rectangle2D box = new Rectangle2D.Double();
		Rectangle2D.intersect(rect1, rect2, box);
		// maximum x
		// int x = Math.max(rect1.x, rect2.x);
		// int y = Math.max(rect1.y, rect2.y);
		// int width = Math.min(rect1.width, rect2.width);
		// int height = Math.min(rect1.height, rect2.height);
		// Rectangle result = new Rectangle(x, y, width, height);
		// return result;
		return box;

	}

	/**
	 * Gets a list of lines. Sorts the lines so that a polygon can be formed out
	 * of it! list gets cleared during this process!!!
	 * 
	 * @param list
	 * @return
	 */
	private Path2D sortLineList(List<Line2D> list) {
		if (list.isEmpty()) {
			return null;
		}

		// during the voronoi process, lines may be created where start and
		// endpoint are the same. remove them! Careful, rounding errors appear!
		for (int i = 0; i < list.size(); i++) {
			Line2D check = list.get(i);
			if (Math.abs(check.getP1().getX() - check.getP2().getX()) < 0.0001d
					&& Math.abs(check.getP1().getY() - check.getP2().getY()) < 0.0001d) {
				list.remove(i);
				i--;
			}
		}

		// now, we traverse the list and sesarch the points
		// Polygon result = new Polygon();
		Path2D path = new Path2D.Double();
		/*
		 * Line2D current = list.remove(0); result.addPoint((int)
		 * current.getX1(), (int) current.getY1()); result.addPoint((int)
		 * current.getX2(), (int) current.getY2()); current.setLine(0, 0,
		 * current.getX2(), current.getY2());
		 * 
		 * 
		 * while (!list.isEmpty()) { // formerly: !list.isEmpty() for (int i =
		 * 0; i < list.size(); i++) { if (commonConnectionPT(current,
		 * list.get(i))) { Point now = getCommonPT(current, list.get(i));
		 * result.addPoint((int) now.x, (int) now.y); current = list.remove(i);
		 * break; } } }
		 */
		try {
			// initialize. Take first point
			Point currPT = new Point();
			currPT.x = list.get(0).getX1();
			currPT.y = list.get(0).getY1();
			// path.moveTo(currPT.x, currPT.y);
			boolean firstloop = true;
			while (!list.isEmpty()) {

				// traverse all points, look for a match
				for (int i = list.size() - 1; i >= 0; i--) {
					if (LineContainsPT(currPT, list.get(i))) {
						// a line containing the current point is found. Add
						// this
						// point to the polygon.
						// result.addPoint((int) currPT.x, (int) currPT.y);
						if (firstloop) {
							// cannot do this otherwise: Taking the first
							// element from list would result in wrong Paths,
							// because then also the next endpoint would be gone
							// away
							firstloop = false;
							path.moveTo(currPT.x, currPT.y);
						} else {
							path.lineTo(currPT.x, currPT.y);
						}
						// path.moveTo(currPT.x, currPT.y);
						// get the neighbor point of the current line
						// in next iteration, search a line that contains the
						// new point
						currPT = theOtherPoint(currPT, list.get(i));
						// remove the line that contained the point
						list.remove(i);
						break;
						// TODO: if list contains a line with negativ x or y
						// value, change it to zero maybe?
					}
					// HACK:if only one line is left, add the points
					// this part is only reached if all lines were traversed but
					// nothing was found!
					if (list.size() == 1) {
						// result.addPoint((int) list.get(0).getX1(), (int) list
						// .get(0).getY1());
						// result.addPoint((int) list.get(0).getX2(), (int) list
						// .get(0).getY2());
						list.remove(0);
					}
				}
			}
			// result.npoints = list.size();
			path.closePath();
			return path;
		} catch (Exception e) {
			// System.out.println("kann polygon nicht bauen");
			return null;
		}
	}

	private Point theOtherPoint(Point currPT, Line2D line2d) {
		Point result = new Point();
		// take in count rounding errors by doubles! Do Math.abs(x1-x2)<0.001
		if (Math.abs(currPT.x - line2d.getX1()) < 0.0001
				&& Math.abs(currPT.y - line2d.getY1()) < 0.0001) {
			result.x = line2d.getX2();
			result.y = line2d.getY2();
			return result;
		} else if (Math.abs(currPT.x - line2d.getX2()) < 0.0001
				&& Math.abs(currPT.y - line2d.getY2()) < 0.0001) {
			result.x = line2d.getX1();
			result.y = line2d.getY1();
			return result;
		}
		return null;
	}

	private boolean LineContainsPT(Point currPT, Line2D line2d) {
		// tolerance of 0.001
		if (Math.abs(currPT.x - line2d.getX1()) < 0.001
				&& Math.abs(currPT.y - line2d.getY1()) < 0.001) {
			return true;
		}
		if (Math.abs(currPT.x - line2d.getX2()) < 0.001
				&& Math.abs(currPT.y - line2d.getY2()) < 0.001) {
			return true;
		}
		return false;
	}

	/**
	 * Gets the point where both lines are connected to each other!
	 * 
	 * @param current
	 * @param line2d
	 * @return
	 */
	@SuppressWarnings("unused")
	private Point getCommonPT(Line2D line1, Line2D line2) {
		// TODO Auto-generated method stub
		Point point = new Point();
		if (line1.getX1() == line2.getX1() && line1.getY1() == line2.getY1()) {
			point.x = line1.getX1();
			point.y = line1.getY1();
		} else if (line1.getX2() == line2.getX2()
				&& line1.getY2() == line2.getY2()) {
			point.x = line1.getX1();
			point.y = line1.getY1();
		} else if (line1.getX2() == line2.getX1()
				&& line1.getY2() == line2.getY1()) {
			point.x = line1.getX1();
			point.y = line1.getY1();
		} else if (line1.getX1() == line2.getX2()
				&& line1.getY1() == line2.getY2()) {
			point.x = line1.getX1();
			point.y = line1.getY1();
		} else {
			return null;
		}
		return point;
	}

	/**
	 * Checks if the two lines have a common starting point! (Only check left
	 * part?!)
	 * 
	 * @param line1
	 * @param line2
	 * @return
	 */
	@SuppressWarnings("unused")
	private boolean commonConnectionPT(Line2D line1, Line2D line2) {
		if (line1.getX1() == line2.getX1() && line1.getY1() == line2.getY1()) {
			return true;
		}
		if (line1.getX2() == line2.getX2() && line1.getY2() == line2.getY2()) {
			return true;
		}
		if (line1.getX2() == line2.getX1() && line1.getY2() == line2.getY1()) {
			return true;
		}
		if (line1.getX1() == line2.getX2() && line1.getY1() == line2.getY2()) {
			return true;
		}
		return false;
	}

	/**
	 * Returns the polygon for the point in x,y. Point x,y must be a
	 * Measurement. The Point must exist.
	 * 
	 * @param x
	 * @param y
	 * @return
	 */
	public Path2D getPoly(int x, int y) {
		if (!isGenerated) {
			// todo:change the parameters according to GSMmap-size!
			// Edge.reg[] gibt an, zu welchen Punkten die Linie gehört!
			generateVoronoi(0, 2000, 0, 2000);
		}
		Edge currEdge;
		Site s = new Site();
		ArrayList<Line2D> lines = new ArrayList<Line2D>();
		s.coord = new Point(x, y);

		// TODO: check lines for null-values. if so, clip it to the max value of
		// the voronoi

		for (int i = 0; i < edges.size(); i++) {
			try {
				currEdge = edges.get(i);
				// see if x/y matches the lines in edges
				if (s.equals(currEdge.reg[0])) {

					double x1 = currEdge.ep[0].coord.x;
					double y1 = currEdge.ep[0].coord.y;
					double x2 = currEdge.ep[1].coord.x;
					double y2 = currEdge.ep[1].coord.y;
					lines.add(new Line2D.Double(x1, y1, x2, y2));

					// lines.add(makeToLine(currEdge));
				}
				// lines.add(new Line2D.Double(x1,y1,);
				// vielleicht gut, wenn man prüft, ob currEdge[0]oder[1] die
				// Kante
				// beinhaltet... eigentlich unnötig. kann in ein test eingebaut
				// werden
				else if (s.equals(currEdge.reg[1])) {

					double x1 = currEdge.ep[0].coord.x;
					double y1 = currEdge.ep[0].coord.y;
					double x2 = currEdge.ep[1].coord.x;
					double y2 = currEdge.ep[1].coord.y;
					lines.add(new Line2D.Double(x1, y1, x2, y2));

					// lines.add(makeToLine(currEdge));
				}

				// linien zu polygon verschmelzen
			} catch (Exception e) {
				// System.out.println("Mindetens eine Linie ist null");
				return null;
			}
		}
		// System.out.println("anzahl der Linien: " + lines.size());

		// Polygon result = new Polygon();
		Path2D result = sortLineList(lines);

		return result;
	}

	@SuppressWarnings("unused")
	private Line2D makeToLine(Edge currEdge) {
		// TODO Auto-generated method stub
		// check for null references. if so, set it to a max value;
		/*
		 * double middle_of_x = (xmax - xmin) / 2; double middle_of_y = (ymax -
		 * ymin) / 2; if (currEdge.ep[0] == null) { // first endpoint is null.
		 * Check where the other endpoint is[1]. // clip // accordingly
		 * currEdge.ep[0] = new Site(); currEdge.ep[0].coord = new Point(); //
		 * check for x if (currEdge.ep[1].coord.x < middle_of_x) {
		 * currEdge.ep[0].coord.x = 0; } else { currEdge.ep[0].coord.x = xmax; }
		 * // check for y if (currEdge.ep[1].coord.y < middle_of_y) {
		 * currEdge.ep[0].coord.y = 0; } else { currEdge.ep[0].coord.y = ymax; }
		 * 
		 * } else if (currEdge.ep[1] == null) { currEdge.ep[1] = new Site();
		 * currEdge.ep[1].coord = new Point(); if (currEdge.ep[0].coord.x <
		 * middle_of_x) { currEdge.ep[1].coord.x = 0; } else {
		 * currEdge.ep[1].coord.x = xmax; } // check for y if
		 * (currEdge.ep[0].coord.y < middle_of_y) { currEdge.ep[1].coord.y = 0;
		 * } else { currEdge.ep[1].coord.y = ymax; } }
		 */
		// now, build line
		double x1 = currEdge.ep[0].coord.x;
		double x2 = currEdge.ep[1].coord.x;
		double y1 = currEdge.ep[0].coord.y;
		double y2 = currEdge.ep[1].coord.y;
		Line2D result = new Line2D.Double();
		result.setLine(x1, y1, x2, y2);
		return result;

	}

	/**
	 * Get the neighbors of a point. Only these neighbors are in relationship
	 * with the given point. Lets say you insert a point for interpolation, than
	 * you would check for his neihgbors in order to see which polygons you need
	 * for calculation
	 * 
	 * @param x
	 * @param y
	 * @return
	 */
	public ArrayList<Point2D> getDirectNeighbors(double x, double y) {
		Site s = new Site();
		s.coord.x = x;
		s.coord.y = y;
		Edge currEdge;
		ArrayList<Point2D> result = new ArrayList<Point2D>();

		for (int i = edges.size() - 1; i >= 0; i--) {
			Point2D currPT = new Point2D.Double();
			currEdge = edges.get(i);
			// check if this edge has something to do with the coordinate x,y
			if (s.equals(currEdge.reg[0])) {
				currPT.setLocation(currEdge.reg[1].coord.x,
						currEdge.reg[1].coord.y);
				result.add(currPT);
			} else if (s.equals(currEdge.reg[1])) {
				currPT.setLocation(currEdge.reg[0].coord.x,
						currEdge.reg[0].coord.y);
				result.add(currPT);
			}
		}
		return result;
	}

	/**
	 * Returns the size of the area of the voronoi region at x,y
	 * 
	 * @param x
	 * @param y
	 * @return
	 */
	public double areaOf(int x, int y) {
		return area(getPoly(x, y));
	}

	public void VorSim(boolean vorsim, Graphics vorg, int vorw, int vorh,
			Canvas Can) {
		VorSim = vorsim;
		c = Can;
		g = vorg;
		w = vorw;
		h = vorh;
	}

	void cleanupSites() {
		if (sites != null)
			for (int i = 0; i < sites.length; i++)
				sites[i] = null;
		sites = null;
	}

	void cleanupEdges() {
		GraphEdge geCurrent = allEdges;

		while (geCurrent != null && geCurrent.next != null) {
			@SuppressWarnings("unused")
			GraphEdge freeCurrent = geCurrent;
			geCurrent = geCurrent.next;
			freeCurrent = null;
		}
		allEdges = null;
	}

	/**
	 * löscht wohl das diagram
	 */
	void dVoronoiDiagramGenerator() {
		cleanupSites();
		cleanupEdges();
		sfl = null;
		efl = null;
	}

	/**
	 * Site compare. Liefert -1,1 zurück, je nachdem welcher x oder y Wert
	 * größer ist
	 * 
	 * @param p1
	 * @param p2
	 * @return
	 */
	int scomp(Site p1, Site p2) {
		Point s1 = p1.coord, s2 = p2.coord;
		if (s1.y < s2.y)
			return (-1);
		if (s1.y > s2.y)
			return (1);
		if (s1.x < s2.x)
			return (-1);
		if (s1.x > s2.x)
			return (1);
		return (0);
	}

	/**
	 * sortiert von sites die ersten n einträge
	 * 
	 * @param sites
	 * @param n
	 */
	void qsort(Site[] sites, int n) {
		Site tmp;
		if (n == 1)
			return;
		for (int i = 0; i < n; i++) {
			for (int j = i + 1; j < n; j++) {
				if (scomp(sites[i], sites[j]) > 0) {
					tmp = sites[i];
					sites[i] = sites[j];
					sites[j] = tmp;
				}
			}
		}
	}

	/**
	 * fügt Punkte hinzu? Sortiert automatisch
	 * 
	 * @param xValues
	 * @param yValues
	 * @param numPoints
	 */
	public void sortNode(double xValues[], double yValues[], int numPoints) {
		// richy start
		dVoronoiDiagramGenerator();
		// richy end
		int i;
		nsites = numPoints;
		sites = new Site[nsites];
		// richy start
		double sn = (double) nsites + 4;
		sqrt_nsites = (int) Math.sqrt(sn);
		minDistanceBetweenSites = 3;
		nvertices = 0;
		sfl = new Sfreelist();
		nedges = 0;
		efl = new Efreelist();
		// richy end
		xmin = xValues[0];
		ymin = yValues[0];
		xmax = xValues[0];
		ymax = yValues[0];
		// for all x and y values given
		for (i = 0; i < nsites; i++) {
			// generate new site with x,y values
			sites[i] = new Site();
			sites[i].coord.setPoint(xValues[i], yValues[i]);
			sites[i].sitenumbr = i;

			// update min and max
			if (xValues[i] < xmin)
				xmin = xValues[i];
			else if (xValues[i] > xmax)
				xmax = xValues[i];

			if (yValues[i] < ymin)
				ymin = yValues[i];
			else if (yValues[i] > ymax)
				ymax = yValues[i];
		}
		// sort the recently added sites
		qsort(sites, nsites);
		deltay = ymax - ymin;
		deltax = xmax - xmin;
	}

	/**
	 * Inserts point into Voronoi. Uses a gsmmap and a ARFCN.
	 * 
	 * @param map
	 * @param resize
	 *            multiplied to each coordinate to have a larger distance
	 *            between points. 1 if not wanted!
	 */
	public void sortNode(GSMMap map, int arfcn, int resize) {
		double x = Double.NaN;
		double y = Double.NaN;
		sortNode(map, arfcn, resize, x, y);
	}

	/**
	 * Inserts point into Voronoi. Uses a gsmmap and a ARFCN. Adds one
	 * additional point: x,y
	 * 
	 * @param map
	 * @param resize
	 *            multiplied to each coordinate to have a larger distance
	 *            between points. 1 if not wanted!
	 */
	public void sortNode(GSMMap map, int arfcn, int resize, double xcoord,
			double ycoord) {
		ArrayList<Double> xVal = new ArrayList<Double>();
		ArrayList<Double> yVal = new ArrayList<Double>();
		if (!Double.isNaN(xcoord) && !Double.isNaN(ycoord)) {
			xVal.add(xcoord);
			yVal.add(ycoord);
		}
		edges = new ArrayList<Edge>(map.numberOfCoordinates);
		zoomfactor = resize;
		// get xValues
		for (int x = 0; x < map.Xcoords.length; x++) {
			for (int y = 0; y < map.Ycoords.length; y++) {
				if (ListBTS.contains(map.map[x][y], arfcn)) {
					xVal.add((double) x);
					yVal.add((double) y);
				}
			}
		}
		double xArray[] = new double[xVal.size()];
		for (int x = 0; x < xArray.length; x++) {
			xArray[x] = xVal.get(x) * resize;
		}

		double yArray[] = new double[yVal.size()];
		for (int y = 0; y < yArray.length; y++) {
			yArray[y] = yVal.get(y) * resize;
		}

		sortNode(xArray, yArray, xVal.size());
	}

	/**
	 * return a single in-storage site from array sites. A site is a point
	 * 
	 * @return the next site in the storage
	 */
	Site nextone() {
		Site s;
		if (siteidx < nsites) {
			s = sites[siteidx];
			siteidx += 1;
			return (s);
		} else
			return (null);
	}

	/**
	 * to bisect: halbieren.
	 * 
	 * @param s1
	 * @param s2
	 * @return
	 */
	Edge bisect(Site s1, Site s2) {
		double dx, dy, adx, ady;
		Edge newedge;

		newedge = new Edge();
		// RICHY: hier ist der ganze geile scheiß, den ich suche!!!!!!!!!!

		// store the sites that this edge is bisecting: reg
		// Jede edge speichern und dann anzeigen!
		newedge.reg[0] = s1;
		newedge.reg[1] = s2;

		// to begin with, there are no endpoints on the bisector - it goes to
		// infinity: ep: Endpoints of Line
		newedge.ep[0] = null;
		newedge.ep[1] = null;

		// get the difference in x dist between the sites
		dx = s2.coord.x - s1.coord.x;
		dy = s2.coord.y - s1.coord.y;
		// make sure that the difference in positive. Ternary operator
		adx = dx > 0 ? dx : -dx;
		ady = dy > 0 ? dy : -dy;
		newedge.c = (double) (s1.coord.x * dx + s1.coord.y * dy + (dx * dx + dy
				* dy) * 0.5);// get the slope of the line

		if (adx > ady) {
			newedge.a = 1.0f;
			newedge.b = dy / dx;
			newedge.c /= dx;// set formula of line, with x fixed to 1
		} else {
			newedge.b = 1.0f;
			newedge.a = dx / dy;
			newedge.c /= dy;// set formula of line, with y fixed to 1
		}

		// sets a number to that edge
		newedge.edgenumbr = nedges;

		// increses the edge counter for the next call
		nedges += 1;
		// RICHY: diese Änderung wars. Beim einfügen ist newedge noch quasi
		// leer, aber das ändert sich während voronoi_bd() läuft, weil ja nur
		// die Referenz in der ArrayList steht und nicht eine Kopie des Objektes
		// prüfe, ob newedge nicht schon drin ist!
		edges.add(newedge); // TODO: das wieder einkommentieren zur not
		return (newedge);
	}

	void resetIterator() {
		iteratorEdges = allEdges;
	}

	/**
	 * 
	 * @return
	 */
	GraphEdge getNext() {
		if (iteratorEdges != null) {
			GraphEdge temp = iteratorEdges;
			iteratorEdges = iteratorEdges.next;
			return temp;
		}
		return null;
	}

	/**
	 * Fügt Punkte zum Voronoi Diagram hinzu? Vertex: Knoten, Eckpunkt
	 * 
	 * @param list
	 */
	void Sort(cVertexList list) {
		double xValues[];
		double yValues[];
		int count;

		dVoronoiDiagramGenerator();

		nsites = list.n;
		minDistanceBetweenSites = 3;

		nvertices = 0;
		sfl = new Sfreelist();

		nedges = 0;
		efl = new Efreelist();

		double sn = (double) nsites + 4;
		sqrt_nsites = (int) Math.sqrt(sn);

		count = list.n;
		xValues = new double[count];
		yValues = new double[count];
		for (int i = 0; i < count; i++) {
			xValues[i] = list.GetElement(i).v.x;
			yValues[i] = list.GetElement(i).v.y;
		}
		sortNode(xValues, yValues, count);
	}

	/**
	 * gives the Site v a vertice number. Increses the number then for the next
	 * call
	 * 
	 * @param v
	 */
	void makevertex(Site v) {
		v.sitenumbr = nvertices;
		nvertices += 1;
	}

	boolean PQinitialize() {
		PQcount = 0;
		PQmin = 0;
		PQhashsize = 4 * sqrt_nsites;
		PQhash = new Halfedge[PQhashsize];

		for (int i = 0; i < PQhashsize; i += 1)
			PQhash[i] = new Halfedge();
		return true;
	}

	int PQbucket(Halfedge he) {
		int bucket;

		bucket = (int) ((he.ystar - ymin) / deltay * PQhashsize);
		if (bucket < 0)
			bucket = 0;
		if (bucket >= PQhashsize)
			bucket = PQhashsize - 1;
		if (bucket < PQmin)
			PQmin = bucket;
		return (bucket);
	}

	/**
	 * push the HalfEdge into the ordered linked list of vertices. vertices:
	 * Ecke
	 * 
	 * @param he
	 * @param v
	 * @param offset
	 */
	void PQinsert(Halfedge he, Site v, double offset, Site s) {
		// Site s hab ich eingefügt
		Halfedge last, next;

		he.vertex = v;
		he.ystar = (double) (v.coord.y + offset);
		last = PQhash[PQbucket(he)];
		while ((next = last.PQnext) != null
				&& (he.ystar > next.ystar || (he.ystar == next.ystar && v.coord.x > next.vertex.coord.x))) {
			last = next;
		}
		he.PQnext = last.PQnext;
		last.PQnext = he;
		PQcount += 1;
	}

	// remove the HalfEdge from the list of vertices
	void PQdelete(Halfedge he) {
		Halfedge last;

		if (he.vertex != null) {
			last = PQhash[PQbucket(he)];
			while (last.PQnext != he)
				last = last.PQnext;

			last.PQnext = he.PQnext;
			PQcount -= 1;
			he.vertex = null;
		}
	}

	boolean PQempty() {
		return (PQcount == 0);
	}

	Point PQ_min() {
		Point answer = new Point();

		while (PQhash[PQmin].PQnext == null) {
			PQmin += 1;
		}
		answer.x = PQhash[PQmin].PQnext.vertex.coord.x;
		answer.y = PQhash[PQmin].PQnext.ystar;
		return (answer);
	}

	Halfedge PQextractmin() {
		Halfedge curr;

		curr = PQhash[PQmin].PQnext;
		PQhash[PQmin].PQnext = curr.PQnext;
		PQcount -= 1;
		return (curr);
	}

	Halfedge HEcreate(Edge e, int pm) {
		Halfedge answer;
		answer = new Halfedge();
		answer.ELedge = e;
		answer.ELpm = pm;
		answer.PQnext = null;
		answer.vertex = null;
		return (answer);
	}

	boolean ELinitialize() {
		int i;
		ELhashsize = 2 * sqrt_nsites;
		ELhash = new Halfedge[ELhashsize];

		for (i = 0; i < ELhashsize; i += 1)
			ELhash[i] = null;
		ELleftend = HEcreate(null, 0);
		ELrightend = HEcreate(null, 0);
		ELleftend.ELleft = null;
		ELleftend.ELright = ELrightend;
		ELrightend.ELleft = ELleftend;
		ELrightend.ELright = null;
		ELhash[0] = ELleftend;
		ELhash[ELhashsize - 1] = ELrightend;

		return true;
	}

	/**
	 * returns right edge of halfedge???
	 * 
	 * @param he
	 * @return
	 */
	Halfedge ELright(Halfedge he) {
		return (he.ELright);
	}

	Halfedge ELleft(Halfedge he) {
		return (he.ELleft);
	}

	Site leftreg(Halfedge he) {
		if (he.ELedge == null)
			return (bottomsite);
		return (he.ELpm == le ? he.ELedge.reg[le] : he.ELedge.reg[re]);
	}

	/**
	 * makes both Halfedges to neighbors
	 * 
	 * @param lb
	 * @param newHe
	 */
	void ELinsert(Halfedge lb, Halfedge newHe) {
		newHe.ELleft = lb;
		newHe.ELright = lb.ELright;
		(lb.ELright).ELleft = newHe;
		lb.ELright = newHe;
	}

	/*
	 * This delete routine can't reclaim node, since pointers from hash table
	 * may be present.
	 */
	void ELdelete(Halfedge he) {
		(he.ELleft).ELright = he.ELright;
		(he.ELright).ELleft = he.ELleft;
		he.deleted = true;
	}

	/* Get entry from hash table, pruning any deleted nodes */
	Halfedge ELgethash(int b) {
		Halfedge he;

		if (b < 0 || b >= ELhashsize)
			return (null);
		he = ELhash[b];
		if (he == null || !he.deleted)
			return (he);

		/* Hash table points to deleted half edge. Patch as necessary. */
		ELhash[b] = null;
		return (null);
	}

	/**
	 * gets left Edge of Point p
	 * 
	 * @param p
	 * @return
	 */
	Halfedge ELleftbnd(Point p) {
		int i, bucket;
		Halfedge he;

		/* Use hash table to get close to desired halfedge */
		// use the hash function to find the place in the hash map that this
		// HalfEdge should be
		bucket = (int) ((p.x - xmin) / deltax * ELhashsize);

		// make sure that the bucket position in within the range of the hash
		// array
		if (bucket < 0)
			bucket = 0;
		if (bucket >= ELhashsize)
			bucket = ELhashsize - 1;

		he = ELgethash(bucket);
		if (he == null)
		// if the HE isn't found, search backwards and forwards in the hash map
		// for the first non-null entry
		{
			for (i = 1; i < ELhashsize; i += 1) {
				if ((he = ELgethash(bucket - i)) != null)
					break;
				if ((he = ELgethash(bucket + i)) != null)
					break;
			}
		}
		/* Now search linear list of halfedges for the correct one */
		if (he == ELleftend || (he != ELrightend && right_of(he, p))) {
			// keep going right on the list until either the end is reached, or
			// you find the 1st edge which the point isn't to the right of
			do {
				he = he.ELright;
			} while (he != ELrightend && right_of(he, p));
			he = he.ELleft;
		} else
			// if the point is to the left of the HalfEdge, then search left for
			// the HE just to the left of the point
			do {
				he = he.ELleft;
			} while (he != ELleftend && !right_of(he, p));

		/* Update hash table and reference counts */
		if (bucket > 0 && bucket < ELhashsize - 1) {
			ELhash[bucket] = he;
		}
		return (he);
	}

	GraphEdge pushGraphEdge(double x1, double y1, double x2, double y2, Site v) {
		GraphEdge newEdge = new GraphEdge();
		newEdge.next = allEdges;
		allEdges = newEdge;
		newEdge.x1 = x1;
		newEdge.y1 = y1;
		newEdge.x2 = x2;
		newEdge.y2 = y2;
		newEdge.correspondingPt = v;
		return newEdge;
	}

	GraphEdge line(double x1, double y1, double x2, double y2, Site v) {
		GraphEdge result = pushGraphEdge(x1, y1, x2, y2, v);
		if (VorSim) {
			g.setColor(Color.red);
			g.drawLine((int) x1, (int) y1, (int) x2, (int) y2);
			c.repaint();
			try {
				Thread.sleep(500);
			} catch (InterruptedException ee) {
				;
			}
		}
		return result;
	}

	GraphEdge clip_line(Edge e, Site v) {
		double pxmin, pxmax, pymin, pymax;
		Site s1, s2;
		double x1 = 0, x2 = 0, y1 = 0, y2 = 0;

		x1 = e.reg[0].coord.x;
		x2 = e.reg[1].coord.x;
		y1 = e.reg[0].coord.y;
		y2 = e.reg[1].coord.y;

		// if the distance between the two points this line was created from is
		// less than the square root of 2, then ignore it
		if (Math.sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1))) < minDistanceBetweenSites) {
			return null;
		}
		pxmin = borderMinX;
		pxmax = borderMaxX;
		pymin = borderMinY;
		pymax = borderMaxY;

		if (e.a == 1.0 && e.b >= 0.0) {
			s1 = e.ep[1];
			s2 = e.ep[0];
		} else {
			s1 = e.ep[0];
			s2 = e.ep[1];
		}

		if (e.a == 1.0) {
			y1 = pymin;
			if (s1 != null && s1.coord.y > pymin) {
				y1 = s1.coord.y;
			}
			if (y1 > pymax) {
				y1 = pymax;
			}
			x1 = e.c - e.b * y1;
			y2 = pymax;
			if (s2 != null && s2.coord.y < pymax)
				y2 = s2.coord.y;

			if (y2 < pymin) {
				y2 = pymin;
			}
			x2 = (e.c) - (e.b) * y2;
			if (((x1 > pxmax) & (x2 > pxmax)) | ((x1 < pxmin) & (x2 < pxmin))) {
				return null;
			}
			if (x1 > pxmax) {
				x1 = pxmax;
				y1 = (e.c - x1) / e.b;
			}
			if (x1 < pxmin) {
				x1 = pxmin;
				y1 = (e.c - x1) / e.b;
			}
			if (x2 > pxmax) {
				x2 = pxmax;
				y2 = (e.c - x2) / e.b;
			}
			if (x2 < pxmin) {
				x2 = pxmin;
				y2 = (e.c - x2) / e.b;
			}
		} else {
			x1 = pxmin;
			if (s1 != null && s1.coord.x > pxmin)
				x1 = s1.coord.x;
			if (x1 > pxmax) {
				x1 = pxmax;
			}
			y1 = e.c - e.a * x1;
			x2 = pxmax;
			if (s2 != null && s2.coord.x < pxmax)
				x2 = s2.coord.x;
			if (x2 < pxmin) {
				x2 = pxmin;
			}
			y2 = e.c - e.a * x2;
			if (((y1 > pymax) & (y2 > pymax)) | ((y1 < pymin) & (y2 < pymin))) {
				return null;
			}
			if (y1 > pymax) {
				y1 = pymax;
				x1 = (e.c - y1) / e.a;
			}
			if (y1 < pymin) {
				y1 = pymin;
				x1 = (e.c - y1) / e.a;
			}
			if (y2 > pymax) {
				y2 = pymax;
				x2 = (e.c - y2) / e.a;
			}
			if (y2 < pymin) {
				y2 = pymin;
				x2 = (e.c - y2) / e.a;
			}
		}

		return line(x1, y1, x2, y2, v);
	}

	GraphEdge endpoint(Edge e, int lr, Site s) {
		e.ep[lr] = s;
		if (e.ep[re - lr] == null)
			return null;
		return clip_line(e, s);
	}

	/* returns 1 if p is to right of halfedge e */
	boolean right_of(Halfedge el, Point p) {
		Edge e;
		Site topsite;
		boolean right_of_site;
		boolean above, fast;
		double dxp, dyp, dxs, t1, t2, t3, yl;

		e = el.ELedge;
		topsite = e.reg[1];
		if (p.x > topsite.coord.x)
			right_of_site = true;
		else
			right_of_site = false;
		if (right_of_site && el.ELpm == le)
			return (true);
		if (!right_of_site && el.ELpm == re)
			return (false);

		if (e.a == 1.0) {
			dyp = p.y - topsite.coord.y;
			dxp = p.x - topsite.coord.x;
			fast = false;
			if ((!right_of_site & (e.b < 0.0)) | (right_of_site & (e.b >= 0.0))) {
				above = dyp >= e.b * dxp;
				fast = above;
			} else {
				above = p.x + p.y * e.b > e.c;
				if (e.b < 0.0)
					above = !above;
				if (!above)
					fast = true;
			}
			if (!fast) {
				dxs = topsite.coord.x - (e.reg[0]).coord.x;
				above = e.b * (dxp * dxp - dyp * dyp) < dxs * dyp
						* (1.0 + 2.0 * dxp / dxs + e.b * e.b);
				if (e.b < 0.0)
					above = !above;
			}
		} else /* e.b==1.0 */
		{
			yl = e.c - e.a * p.x;
			t1 = p.y - yl;
			t2 = p.x - topsite.coord.x;
			t3 = yl - topsite.coord.y;
			above = t1 * t1 > t2 * t2 + t3 * t3;
		}
		return (el.ELpm == le ? above : !above);
	}

	/**
	 * Bringt rechte (oder Linke?) Site von he zurück. Falls es das nicht gibt,
	 * wird bottomsite zurückgeliefert
	 * 
	 * @param he
	 * @return
	 */
	Site rightreg(Halfedge he) {
		if (he.ELedge == (Edge) null)
			// if this halfedge has no edge, return the bottom site (whatever
			// that is)
			return (bottomsite);

		// if the ELpm field is zero, return the site 0 that this edge bisects,
		// otherwise return site number 1
		return (he.ELpm == le ? he.ELedge.reg[re] : he.ELedge.reg[le]);
	}

	/**
	 * Distance between two points
	 */
	double dist(Site s, Site t) {
		double dx, dy;
		dx = s.coord.x - t.coord.x;
		dy = s.coord.y - t.coord.y;
		return (double) (Math.sqrt(dx * dx + dy * dy));
	}

	/**
	 * create a new site where the HalfEdges el1 and el2 intersect - note that
	 * the Point in the argument list is not used, don't know why it's there
	 * 
	 * @param el1
	 * @param el2
	 * @return
	 */
	Site intersect(Halfedge el1, Halfedge el2) {
		Edge e1, e2, e;
		Halfedge el;
		double d, xint, yint;
		boolean right_of_site;
		Site v;

		e1 = el1.ELedge;
		e2 = el2.ELedge;
		if (e1 == null || e2 == null)
			return null;

		// if the two edges bisect the same parent, return null
		if (e1.reg[1] == e2.reg[1])
			return null;

		d = e1.a * e2.b - e1.b * e2.a;
		if (-1.0e-10 < d && d < 1.0e-10)
			return null;

		xint = (e1.c * e2.b - e2.c * e1.b) / d;
		yint = (e2.c * e1.a - e1.c * e2.a) / d;

		if ((e1.reg[1].coord.y < e2.reg[1].coord.y)
				|| (e1.reg[1].coord.y == e2.reg[1].coord.y && e1.reg[1].coord.x < e2.reg[1].coord.x)) {
			el = el1;
			e = e1;
		} else {
			el = el2;
			e = e2;
		}

		right_of_site = xint >= e.reg[1].coord.x;
		if ((right_of_site && el.ELpm == le)
				|| (!right_of_site && el.ELpm == re))
			return null;

		// create a new site at the point of intersection - this is a new vector
		// event waiting to happen
		v = new Site();
		v.coord.x = xint;
		v.coord.y = yint;
		return (v);
	}

	/**
	 * Zeigt sachen an, wenn VorSim = 1. Sonst tut es nichts
	 * 
	 * @param s1
	 * @param s2
	 * @param s3
	 */
	void out_triple(Site s1, Site s2, Site s3) {

		if (VorSim) {
			double P1X = s1.coord.x;
			double P1Y = s1.coord.y;
			double P2X = s2.coord.x;
			double P2Y = s2.coord.y;
			double P3X = s3.coord.x;
			double P3Y = s3.coord.y;

			double l1a = P2X - P1X;
			double l1b = P2Y - P1Y;
			double l1c = (P1Y * P1Y - P2Y * P2Y + P1X * P1X - P2X * P2X) * 0.5000;
			double l2a = P2X - P3X;
			double l2b = P2Y - P3Y;
			double l2c = (P3Y * P3Y - P2Y * P2Y + P3X * P3X - P2X * P2X) * 0.5000;

			if (l1a * l2b == l1b * l2a)
				return;
			double x = (l1c * l2b - l1b * l2c) / (l1b * l2a - l1a * l2b);
			double y = (l1a * l2c - l1c * l2a) / (l1b * l2a - l1a * l2b);

			double d = Math.sqrt(Math.pow(s1.coord.x - x, 2)
					+ Math.pow(s1.coord.y - y, 2));

			g.setColor(Color.red);
			g.fillOval((int) P1X - (int) (w / 2), (int) P1Y - (int) (h / 2), w,
					h);
			g.fillOval((int) P2X - (int) (w / 2), (int) P2Y - (int) (h / 2), w,
					h);
			g.fillOval((int) P3X - (int) (w / 2), (int) P3Y - (int) (h / 2), w,
					h);
			g.setColor(Color.blue);
			g.drawOval((int) (x - d), (int) (y - d), (int) (2 * d),
					(int) (2 * d));
			c.repaint();
			try {
				Thread.sleep(300);
			} catch (InterruptedException ee) {
				;
			}
			g.setColor(Color.lightGray);
			g.drawOval((int) (x - d), (int) (y - d), (int) (2 * d),
					(int) (2 * d));
			g.setColor(Color.black);
			g.fillOval((int) P1X - (int) (w / 2), (int) P1Y - (int) (h / 2), w,
					h);
			g.fillOval((int) P2X - (int) (w / 2), (int) P2Y - (int) (h / 2), w,
					h);
			g.fillOval((int) P3X - (int) (w / 2), (int) P3Y - (int) (h / 2), w,
					h);
			c.repaint();
		}
	}

	int beachline_bake[] = null;

	/**
	 * tut nichts wenn VorSim == false
	 */
	void out_beachline() {
		if (VorSim) {
			if (beachline_bake == null) {
				beachline_bake = new int[(int) (borderMaxX - borderMinX)];
				for (int x = (int) borderMinX; x < (int) borderMaxX; x++) {
					beachline_bake[x] = 0;
				}
			}
			for (int x = (int) borderMinX; x < (int) borderMaxX; x++) {
				g.setColor(Color.lightGray);
				g.fillOval(x, beachline_bake[x], 1, 1);

				Site s = sites[siteidx - 1];
				double y0 = s.coord.y;
				Halfedge he = ELleftend;
				int y = (int) 0;
				do {
					Site v = rightreg(he);
					double x1 = v.coord.x;
					double y1 = v.coord.y;
					int y2 = (int) ((y0 * y0 - (x1 - x) * (x1 - x) - y1 * y1) / (2 * y0 - 2 * y1));
					if (y2 > y)
						y = y2;
					he = he.ELright;
				} while (he.ELright != null);
				if (y != 0) {
					g.setColor(Color.red);
					g.fillOval(x, y, 1, 1);
					c.repaint();
					beachline_bake[x] = y;
				}
			}
			try {
				Thread.sleep(1000);
			} catch (InterruptedException ee) {
				;
			}
		}
	}

	/**
	 * Does nothing when VorSim isn't enabled
	 * 
	 * @param s1
	 */
	void out_site(Site s1) {

		if (VorSim) {
			if (s1 == null)
				return;
			double P1X = s1.coord.x;
			double P1Y = s1.coord.y;
			g.setColor(Color.lightGray);
			g.drawLine((int) borderMinX, (int) lasty, (int) borderMaxX,
					(int) lasty);
			g.setColor(Color.black);
			g.drawLine((int) borderMinX, (int) P1Y, (int) borderMaxX, (int) P1Y);
			g.setColor(Color.red);
			g.fillOval((int) P1X - (int) (w / 2), (int) P1Y - (int) (h / 2), w,
					h);
			c.repaint();
			try {
				Thread.sleep(1000);
			} catch (InterruptedException ee) {
				;
			}
			g.setColor(Color.black);
			g.drawLine((int) borderMinX, (int) P1Y, (int) borderMaxX, (int) P1Y);
			g.fillOval((int) P1X - (int) (w / 2), (int) P1Y - (int) (h / 2), w,
					h);
			c.repaint();
			lasty = P1Y;
		}
	}

	/*
	 * implicit parameters: nsites, sqrt_nsites, xmin, xmax, ymin, ymax, deltax,
	 * deltay (can all be estimates). Performance suffers if they are wrong;
	 * better to make nsites, deltax, and deltay too big than too small. (?)
	 */

	/**
	 * der eigentliche algorithmus
	 */
	boolean voronoi_bd() {
		isGenerated = true;
		// clear edges
		edges.clear();
		edges = new ArrayList<Edge>(200);
		// clear other lists
		// corresponding.clear();
		Site newsite, bottom, top, temp, p;
		Site v;
		Point newintstar = null;
		int pm;
		Halfedge lbnd, rbnd, llbnd, rrbnd, bisector;
		Edge e;

		PQinitialize();
		ELinitialize();

		bottomsite = nextone();
		out_site(bottomsite);
		newsite = nextone();
		while (true) {
			if (!PQempty())
				newintstar = PQ_min();
			// if the lowest site has a smaller y value than the lowest vector
			// intersection,
			// process the site otherwise process the vector intersection

			if (newsite != null
					&& (PQempty() || newsite.coord.y < newintstar.y || (newsite.coord.y == newintstar.y && newsite.coord.x < newintstar.x))) {
				out_site(newsite);
				/* new site is smallest -this is a site event */
				// get the first HalfEdge to the LEFT of the new site
				// richy: left bound
				lbnd = ELleftbnd((newsite.coord));
				// get the first HalfEdge to the RIGHT of the new site
				// richy: right bound
				rbnd = ELright(lbnd);
				// if this halfedge has no edge,bot =bottom site (whatever that
				// is)
				bottom = rightreg(lbnd);
				// create a new edge that bisects. Baue Halbierende
				e = bisect(bottom, newsite); // richy: e gehört zu bottom,
												// newsite

				// create a new HalfEdge, setting its ELpm field to 0
				bisector = HEcreate(e, le); // richy: e in HalfEdge wandeln?
				// insert this new bisector (Halbierende) edge between the left
				// and right
				// vectors in a linked list
				ELinsert(lbnd, bisector);

				// if the new bisector intersects with the left edge,
				// remove the left edge's vertex, and put in the new one
				// p wird hier zugewiesen. p scheint aber "nur" ein Schnittpunkt
				// zweier Kanten zu sein
				if ((p = intersect(lbnd, bisector)) != null) {
					PQdelete(lbnd);
					PQinsert(lbnd, p, dist(p, newsite), newsite);
				}
				lbnd = bisector;
				// create a new HalfEdge, setting its ELpm field to 1
				bisector = HEcreate(e, re);
				// insert the new HE to the right of the original bisector
				// earlier in the IF stmt
				ELinsert(lbnd, bisector);

				// if this new bisector intersects with the new HalfEdge
				if ((p = intersect(bisector, rbnd)) != null) {
					// push the HE into the ordered linked list of vertices
					PQinsert(bisector, p, dist(p, newsite), newsite); // PQinsert
																		// ändern
					// Richy:hier werden die Kanten tatsächlich eingefügt.
					// Vielleicht
					// das isert hier machen!!!!!!!
					// edges.add(e);
				}
				out_beachline();
				newsite = nextone(); // (nur) hier wird eine neue Site geholt!
			} else if (!PQempty())
			/* intersection is smallest - this is a vector event */
			// richy: vertices are created here
			{
				// pop the HalfEdge with the lowest vector off the ordered list
				// of vectors
				lbnd = PQextractmin(); // lbnd: left bound?
				// get the HalfEdge to the left of the above HE
				llbnd = ELleft(lbnd); // richy: left of left bound
				// get the HalfEdge to the right of the above HE
				rbnd = ELright(lbnd);
				// get the HalfEdge to the right of the HE to the right of the
				// lowest HE
				rrbnd = ELright(rbnd);
				// get the Site to the left of the left HE which it bisects
				bottom = leftreg(lbnd);
				// get the Site to the right of the right HE which it bisects
				top = rightreg(rbnd);

				// output the triple of sites, stating that a circle goes
				// through them
				out_triple(bottom, top, rightreg(lbnd));

				v = lbnd.vertex; // get the vertex that caused this event
				makevertex(v); // set the vertex number - couldn't do this
				// earlier since we didn't know when it would be processed
				// GraphEdge one = endpoint(lbnd.ELedge, lbnd.ELpm, v); //
				// richy:
				// bottom
				// and
				// top maybe?
				// diese zwei endpoints könnten eine zu bottom und top
				// zugehörige kante sein!

				// set the endpoint of
				// the left HalfEdge to be this vector
				// GraphEdge two = endpoint(rbnd.ELedge, rbnd.ELpm, v);
				// set the endpoint of the right HalfEdge to
				// be this vector
				// richy linien zeugs: die Kanten one und two gehören zu punkte
				// top&bottom
				/*
				 * PointAndLines tempP2D1 = new PointAndLines(); tempP2D1.coord
				 * = bottom.coord; Line2D line1 = new Line2D.Double(); if (one
				 * != null) line1.setLine(one.x1, one.y1, one.x2, one.y2);
				 * Line2D line2 = new Line2D.Double(); if (two != null)
				 * line2.setLine(two.x1, two.y1, two.x2, two.y2);
				 * tempP2D1.corrLines.add(line1); tempP2D1.corrLines.add(line2);
				 * corresponding.add(tempP2D1);
				 */
				PointAndLines tempP2D1 = new PointAndLines();
				tempP2D1.coord = bottom.coord;
				Line2D line1 = new Line2D.Double();
				line1.setLine(bottom.coord.x, bottom.coord.y, v.coord.x,
						v.coord.y);
				if (line1 != null) {
					tempP2D1.corrLines.add(line1);
				}
				// corresponding.add(tempP2D1);

				ELdelete(lbnd); // mark the lowest HE for
				// deletion - can't delete yet because there might be pointers
				// to it in Hash Map
				PQdelete(rbnd);
				// remove all vertex events to do with the right HE
				ELdelete(rbnd); // mark the right HE for
				// deletion - can't delete yet because there might be pointers
				// to it in Hash Map
				pm = le; // set the pm variable to zero

				// richy: v and newsite seem to be the points that are involved
				// in this line.
				// or bottom and top?

				if (bottom.coord.y > top.coord.y)
				// if the site to the left of the event is higher than the
				// Site
				{ // to the right of it, then swap them and set the 'pm'
					// variable to 1
					temp = bottom;
					bottom = top;
					top = temp;
					pm = re;
				}
				e = bisect(bottom, top); // create an Edge (or line)
				// that is between the two Sites. This creates the formula of
				// the line, and assigns a line number to it
				bisector = HEcreate(e, pm); // create a HE from the Edge 'e',
				// and make it point to that edge
				// with its ELedge field
				ELinsert(llbnd, bisector); // insert the new bisector to the
				// right of the left HE
				endpoint(e, re - pm, v); // set one endpoint to the new edge
				// to be the vector point 'v'.
				// If the site to the left of this bisector is higher than the
				// right Site, then this endpoint
				// is put in position 0; otherwise in pos 1

				// if left HE and the new bisector intersect, then delete
				// the left HE, and reinsert it
				if ((p = intersect(llbnd, bisector)) != null) {
					PQdelete(llbnd);
					PQinsert(llbnd, p, dist(p, bottom), p);
				}

				// if right HE and the new bisector intersect, then
				// reinsert it
				if ((p = intersect(bisector, rrbnd)) != null) {
					PQinsert(bisector, p, dist(p, bottom), p);
				}

				// hier startet eine neue Ieration
				// PointAndVertices verts = new PointAndVertices();
				// if (bottom != null) {
				// verts.Punkt.x = bottom.coord.x;
				// verts.Punkt.y = bottom.coord.y;
				// }
				// nun an den Punkt die äußeren Kanten anfügen
				Point point = null;
				if (lbnd != null && lbnd.vertex != null) {
					point = new Point();
					point.x = (int) lbnd.vertex.coord.x;
					point.y = (int) lbnd.vertex.coord.y;
					// verts.polygon.add(point);
				}
				if (llbnd != null && llbnd.vertex != null) {
					point = new Point();
					point.x = (int) llbnd.vertex.coord.x;
					point.y = (int) llbnd.vertex.coord.y;
					// verts.polygon.add(point);
				}
				if (rbnd != null && rbnd.vertex != null) {
					point = new Point();
					point.x = (int) rbnd.vertex.coord.x;
					point.y = (int) rbnd.vertex.coord.y;
					// verts.polygon.add(point);
				}

				// polygon.add(verts);

				// richy: schaue mal e, lbnd, rbdn und llbnd an

			} else
				break;
		} // hier ist die while (true) Schleife zu ende

		for (lbnd = ELright(ELleftend); lbnd != ELrightend; lbnd = ELright(lbnd)) {
			e = lbnd.ELedge;
			clip_line(e, null);
		}
		endsim();
		return true;
	}

	/**
	 * Generates Voronoi
	 * 
	 * @param minX
	 * @param maxX
	 *            maximum x size of diagram
	 * @param minY
	 * @param maxY
	 *            maximum y size of diagram
	 * @return
	 */
	public boolean generateVoronoi(double minX, double maxX, double minY,
			double maxY) {
		edges = new ArrayList<Edge>(1000);
		double temp = 0;
		if (minX > maxX) {
			temp = minX;
			minX = maxX;
			maxX = temp;
		}
		if (minY > maxY) {
			temp = minY;
			minY = maxY;
			maxY = temp;
		}
		borderMinX = minX;
		borderMinY = minY;
		borderMaxX = maxX;
		borderMaxY = maxY;

		siteidx = 0;
		voronoi_bd();
		isGenerated = true;

		// clean edges
		// cleanEdgeList();

		return true;
	}

	@SuppressWarnings("unused")
	private void cleanEdgeList() {
		ArrayList<Edge> result = new ArrayList<Edge>();
		Edge temp;
		for (int i = 0; i < edges.size(); i++) {
			temp = edges.get(i);
			// search if temp is only once in edges. if so, go on. else, delete
			// the others
			for (int j = i; j < edges.size(); j++) {
				// if (temp.ep[0].equals(edges.get(j).ep[0]))
			}
		}

	}

	void endsim() {
		if (VorSim) {
			g.setColor(Color.lightGray);
			g.drawLine((int) borderMinX, (int) lasty, (int) borderMaxX,
					(int) lasty);
			if (beachline_bake != null) {
				for (int x = (int) borderMinX; x < (int) borderMaxX; x++) {
					g.setColor(Color.lightGray);
					g.fillOval(x, beachline_bake[x], 1, 1);

				}
			}
			c.repaint();
		}

	}

	/**
	 * Zeichnet das Diagramm in Graphics g
	 * 
	 * @param g
	 */
	public void DrawVor(Graphics g) {
		double minX, maxX, minY, maxY;
		minX = 0;
		minY = 0;
		maxX = 1500;
		maxY = 1500;

		generateVoronoi(minX, maxX, minY, maxY);

		resetIterator();
		GraphEdge temp = null;
		g.setColor(Color.black);

		// vom Entwickler gemachte Schleife
		while ((temp = getNext()) != null) {
			g.drawLine((int) temp.x1, (int) temp.y1, (int) temp.x2,
					(int) temp.y2);
			if (temp.correspondingPt != null) {
				g.drawRect((int) temp.correspondingPt.coord.x - 1,
						(int) temp.correspondingPt.coord.y - 1, 2, 2);// Eckpunkte
			}

		}

		/*
		 * g.setColor(Color.black); Random r = new Random(); // corresponding
		 * malen g.setColor(Color.green); for (PointAndLines points :
		 * corresponding) { if (points == null || points.coord.y != 224 ||
		 * points.coord.x != 344) continue; // g.setColor(new
		 * Color(r.nextInt(256), r.nextInt(256), // r.nextInt(256)));
		 * g.setColor(Color.blue); g.drawRect((int) points.coord.x - 1, (int)
		 * points.coord.y - 1, 2, 2); for (Line2D line : points.corrLines) {
		 * g.drawLine((int) line.getX1(), (int) line.getY1(), (int)
		 * line.getX2(), (int) line.getY2()); } }
		 * 
		 * // Points and Vertices zeichnen! g.setColor(Color.red); for
		 * (PointAndVertices current : polygon) { if (current.Punkt.x != 344 ||
		 * current.Punkt.y != 224) continue; g.drawRect((int) current.Punkt.x,
		 * (int) current.Punkt.y, 2, 2); for (Point points : current.polygon) {
		 * if (points != null) g.drawRect((int) points.x, (int) points.y, 2, 2);
		 * 
		 * } } /*
		 * 
		 * // start richy // draw the points? // halfedges malen
		 * resetIterator(); Halfedge bla; g.setColor(Color.yellow); /* while
		 * ((bla = ELleftend.PQnext) != null) { g.drawRect((int)
		 * bla.vertex.coord.x, (int) bla.vertex.coord.y, 2, 2); if
		 * ((bla.ELedge.ep[0].coord.x == 344 || bla.ELedge.ep[1].coord.x == 344)
		 * && (bla.ELedge.ep[0].coord.y == 224 || bla.ELedge.ep[1].coord.y ==
		 * 224)) { g.drawRect((int) bla.ELedge.ep[0].coord.x, (int)
		 * bla.ELedge.ep[1].coord.y, 2, 2); g.drawLine((int)
		 * bla.ELedge.reg[0].coord.x, (int) bla.ELedge.reg[0].coord.y, (int)
		 * bla.ELedge.reg[1].coord.x, (int) bla.ELedge.reg[1].coord.y); } }
		 */

		// <so geht das!!!!!!!!!!!!!!!!!!!!!!!> Siehe edges!
		/*
		 * g.setColor(Color.yellow); for (Edge tempedge : edges) { if
		 * ((tempedge.reg[0].coord.x == 272 && tempedge.reg[0].coord.y == 136)
		 * || (tempedge.reg[1].coord.x == 272 && tempedge.reg[1].coord.y ==
		 * 136)) { try { g.drawRect((int) tempedge.reg[0].coord.x - 1, (int)
		 * tempedge.reg[0].coord.y - 1, 3, 3); g.drawLine((int)
		 * tempedge.ep[0].coord.x, (int) tempedge.ep[0].coord.y, (int)
		 * tempedge.ep[1].coord.x, (int) tempedge.ep[1].coord.y); } catch
		 * (Exception e) { System.out.println("Hat nicht geklappt"); } } }/*
		 * //</so geht das>
		 * 
		 * // end richy return; }
		 */
	}
	/*
	 * public Weight[] getWeights(int x, int y, voronoi original) { Polygon
	 * interpolator = getPoly(x, y); // get the neighbors of this x,y
	 * ArrayList<Point2D> neighbors = getDirectNeighbors(x, y); // get the
	 * polygons for each neighbor out of original
	 * 
	 * return null; }
	 */

}

class PointAndLines {
	Point coord;
	ArrayList<Line2D> corrLines = new ArrayList<Line2D>();

	// constructor not needed!

	public void removeDuplicates() {
		// ArrayList<Line2D> resultList = new ArrayList<Line2D>(10);
		// Line2D result;
		for (int i = 0; i < corrLines.size(); i++) {

		}
	}

	public boolean contains(Line2D line) {
		for (int i = 0; i < corrLines.size(); i++) {
			Line2D current = corrLines.get(i);
			if (current.getX1() == line.getX1()
					&& current.getX2() == line.getX2()
					&& current.getY1() == line.getY1()
					&& current.getY2() == line.getY2())
				return true;
		}
		return false;
	}
}

/**
 * Should store a Point and the corresponding intersections of the voronoi
 * 
 * @author richy
 * 
 */
class PointAndVertices {
	Point Punkt = new Point();
	ArrayList<Point> polygon = new ArrayList<Point>(10);
}