00001 package delaunaypackage;
00002 import java.awt.*;
00003
00004 import debuggingCode.Debug;
00005
00006
00007
00008 public class QuadraticAlgorithm extends TriangulationAlgorithm {
00009 int s, t, u, bP;
00010 Circle bC = new Circle();
00011 final static String algName = "O(n^2)";
00012
00013 public QuadraticAlgorithm(){
00014 super();
00015 }
00016
00017 public QuadraticAlgorithm(Triangulation t, RealWindow w, int nPoints) {
00018 super(t, w, algName, nPoints);
00019 }
00020
00021 public void reset() {
00022 triCanvas.needToClear = true;
00023 super.reset();
00024 }
00025
00026 public void draw(RealWindowGraphics rWG, Triangulation tri) {
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 tri.draw(rWG, Color.black, Color.black);
00051
00052 }
00053
00054 public synchronized void triangulate(Triangulation tri) {
00055 if(Debug.DEBUG_DELAUNAY_TRIANGULATION)Debug.print("start triangulating");
00056 int seedEdge, currentEdge;
00057 int nFaces;
00058 Int s, t;
00059
00060
00061 nFaces = 0;
00062 s = new Int();
00063 t = new Int();
00064
00065
00066 findClosestNeighbours(tri.point, tri.nPoints, s, t);
00067
00068
00069 if(Debug.DEBUG_DELAUNAY_TRIANGULATION)Debug.print("begin edge van de triangulatie, de edge het dichts bij de oorsprong.");
00070 seedEdge = tri.addEdge(s.getValue(), t.getValue(),
00071 Triangulation.Undefined,
00072 Triangulation.Undefined);
00073
00074 currentEdge = 0;
00075 while (currentEdge < tri.nEdges) {
00076 if (tri.edge[currentEdge].leftTriangle == Triangulation.Undefined) {
00077 nFaces= completeFacet(currentEdge, tri, nFaces);
00078
00079 }
00080 if (tri.edge[currentEdge].rightTriangle == Triangulation.Undefined) {
00081 nFaces= completeFacet(currentEdge, tri, nFaces);
00082
00083 }
00084 currentEdge++;
00085 }
00086 tri.setnFaces(nFaces);
00087 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) tri.print(System.out);
00088 }
00089
00090
00091 public void findClosestNeighbours(RealPoint p[], int nPoints,
00092 Int u, Int v) {
00093 int i, j;
00094 float d, min;
00095 int s, t;
00096
00097 s = t = 0;
00098 min = Float.MAX_VALUE;
00099 for (i = 0; i < nPoints-1; i++)
00100 for (j = i+1; j < nPoints; j++)
00101 {
00102 d = p[i].distanceSq(p[j]);
00103 if (d < min)
00104 {
00105 s = i;
00106 t = j;
00107 min = d;
00108 }
00109 }
00110 u.setValue(s);
00111 v.setValue(t);
00112 }
00113
00114
00115
00116
00117
00118
00119
00120 public int completeFacet(int eI, Triangulation tri, int nFaces) {
00121
00122 float cP;
00123 int i;
00124 Edge e[] = tri.edge;
00125 RealPoint p[] = tri.point;
00126
00127
00128 if (e[eI].leftTriangle == Triangulation.Undefined)
00129 {
00130 s = e[eI].beginPunt;
00131 t = e[eI].eindPunt;
00132 }
00133 else if (e[eI].rightTriangle == Triangulation.Undefined)
00134 {
00135 s = e[eI].eindPunt;
00136 t = e[eI].beginPunt;
00137 }
00138 else
00139
00140 return nFaces;
00141
00142
00143
00144 for (u = 0; u < tri.nPoints; u++)
00145 {
00146 if (u == s || u == t)
00147 continue;
00148 if (Vector.crossProduct(p[s], p[t], p[u]) > 0.0){
00149 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) Debug.print("eerste punt aan de linkerkant van de edge is gevonden");
00150 break; }
00151 }
00152
00153
00154 bP = u;
00155 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) Debug.print("onderzoek nu nog de overblijvende punten (kan ook zijn dat er geen gevonden zijn)");
00156 if (bP < tri.nPoints)
00157 {
00158 bC.circumCircle(p[s], p[t], p[bP]);
00159
00160
00161
00162 for (u = bP+1; u < tri.nPoints; u++)
00163 {
00164 if (u == s || u == t)
00165 continue;
00166
00167
00168
00169 cP = Vector.crossProduct(p[s], p[t], p[u]);
00170
00171 if (cP > 0.0)
00172 if (bC.inside(p[u]))
00173 {
00174
00175 bP = u;
00176 bC.circumCircle(p[s], p[t], p[u]);
00177
00178 }
00179 }
00180 }
00181
00182
00183 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) Debug.print("voeg een nieuwe driehoek toe of, als de edge op de hull ligt, update de informatie");
00184 if (bP < tri.nPoints)
00185 {
00186
00187
00188
00189 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) Debug.print("zorg ervoor dat de linker driehoek van (s,t) naar de nieuwe driehoek wijst");
00190 tri.updateLeftFace(eI, s, t, nFaces);
00191
00192
00193
00194 eI = tri.findEdge(bP, s);
00195 if (eI == Triangulation.Undefined){
00196
00197 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) Debug.print(" er bestaat nog geen edge tussen het beste punt bP en s");
00198 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) Debug.print(" de linkerdriehoek van de edge (bP,s) is de nieuwe driehoek");
00199 eI = tri.addEdge(bP, s, nFaces, Triangulation.Undefined);
00200 tri.face[nFaces].addEdge(eI);
00201 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) Debug.print(" tri.face["+nFaces+"].addEdge("+(eI)+"); // me ");
00202 }
00203 else{
00204
00205 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) Debug.print(" edge (bP,s) bestaat al: update alle betrokken info");
00206 tri.updateLeftFace(eI, bP, s, nFaces);
00207 }
00208
00209
00210
00211 eI = tri.findEdge(t, bP);
00212 if (eI == Triangulation.Undefined){
00213
00214 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) Debug.print(" er bestaat nog geen edge tussen het beste punt bP en t");
00215 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) Debug.print(" de linkerdriehoek van de edge (t,bP) is de nieuwe driehoek");
00216 eI = tri.addEdge(t, bP, nFaces, Triangulation.Undefined);
00217 tri.face[nFaces].addEdge(eI);
00218 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) Debug.print(" tri.face["+nFaces+"].addEdge("+(eI)+"); // me ");
00219 }
00220 else{
00221
00222 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) Debug.print(" edge (t,bP) bestaat al: update alle betrokken info");
00223 tri.updateLeftFace(eI, t, bP, nFaces);}
00224
00225 if(Debug.DEBUG_DELAUNAY_TRIANGULATION)Debug.print("maak een nieuwe driehoek (bP,s) (s,t) (t,bP)");
00226 if(Debug.DEBUG_DELAUNAY_TRIANGULATION)Debug.print("verhoog het aantal driehoeken");
00227 nFaces++;
00228 }
00229 else{
00230 if(Debug.DEBUG_DELAUNAY_TRIANGULATION) Debug.print("geen nieuwe driehoek, edge (s,t) op de hull");
00231 tri.updateLeftFace(eI, s, t, Triangulation.Universe);
00232 }
00233 return nFaces;
00234 }
00235
00236 }