diff --git a/.settings/language.settings.xml b/.settings/language.settings.xml index c990946f19752387b15cd1dcd505a5b723fa8e2e..7677341ca165db237b6de394ee0288d87d618808 100644 --- a/.settings/language.settings.xml +++ b/.settings/language.settings.xml @@ -11,7 +11,7 @@ <provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/> - <provider class="org.eclipse.cdt.managedbuilder.internal.language.settings.providers.GCCBuiltinSpecsDetectorMinGW" console="false" env-hash="616115681376380407" id="org.eclipse.cdt.managedbuilder.core.GCCBuiltinSpecsDetectorMinGW" keep-relative-paths="false" name="CDT GCC Built-in Compiler Settings MinGW" parameter="${COMMAND} ${FLAGS} -E -P -v -dD "${INPUTS}"" prefer-non-shared="true"> + <provider class="org.eclipse.cdt.managedbuilder.internal.language.settings.providers.GCCBuiltinSpecsDetectorMinGW" console="false" env-hash="558743779821345239" id="org.eclipse.cdt.managedbuilder.core.GCCBuiltinSpecsDetectorMinGW" keep-relative-paths="false" name="CDT GCC Built-in Compiler Settings MinGW" parameter="${COMMAND} ${FLAGS} -E -P -v -dD "${INPUTS}"" prefer-non-shared="true"> <language-scope id="org.eclipse.cdt.core.gcc"/> @@ -33,7 +33,7 @@ <provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/> - <provider class="org.eclipse.cdt.managedbuilder.internal.language.settings.providers.GCCBuiltinSpecsDetectorMinGW" console="false" env-hash="616115681376380407" id="org.eclipse.cdt.managedbuilder.core.GCCBuiltinSpecsDetectorMinGW" keep-relative-paths="false" name="CDT GCC Built-in Compiler Settings MinGW" parameter="${COMMAND} ${FLAGS} -E -P -v -dD "${INPUTS}"" prefer-non-shared="true"> + <provider class="org.eclipse.cdt.managedbuilder.internal.language.settings.providers.GCCBuiltinSpecsDetectorMinGW" console="false" env-hash="558743779821345239" id="org.eclipse.cdt.managedbuilder.core.GCCBuiltinSpecsDetectorMinGW" keep-relative-paths="false" name="CDT GCC Built-in Compiler Settings MinGW" parameter="${COMMAND} ${FLAGS} -E -P -v -dD "${INPUTS}"" prefer-non-shared="true"> <language-scope id="org.eclipse.cdt.core.gcc"/> diff --git a/src/delaunay.c b/src/delaunay.c index b26021c05d63fc049982e3fbbef631b88cd1f938..444d850ce5b793ab743d6a3848fff80b25b70ac0 100644 --- a/src/delaunay.c +++ b/src/delaunay.c @@ -12,6 +12,7 @@ + /* Draw a full 2*pi period of the sine */ void create_image ( void ) { @@ -65,14 +66,21 @@ int drawLine(vertex* p) { /* Write the ppm-formatted file */ int reportTriangle(vertex* p, int size, FILE *out) { + for(int i = 0; i < size; i++) { + printVertex(p[i]); + printf("; "); + } + printf("\n"); qsort (p, size, sizeof(*p), comparePointsByX); + char *color = "255,0,0"; + line l = {p[0], p[1]}; - svgDrawLine(l, out); + svgDrawLine(l, out, color); l.b = p[2]; - svgDrawLine(l, out); + svgDrawLine(l, out, color); l.a = p[1]; - svgDrawLine(l, out); + svgDrawLine(l, out, color); return 1; } @@ -399,3 +407,9 @@ void delaunayCws(vertex* S, int size, FILE *out, int draw) { } //return delaunayTriangles; } + + +void printVertex(vertex v) { + printf("%f, %f", v.x, v.y); + fflush( stdout ); +} diff --git a/src/delaunay.h b/src/delaunay.h index d41d677365e01f44132a46f93a4748250d73d24e..6573ca29ce8668fce4c6af7636b31416b889790a 100644 --- a/src/delaunay.h +++ b/src/delaunay.h @@ -19,7 +19,7 @@ /* This is where we'll store the pixels */ uint8_t r[H][W], g[H][W], b[H][W]; - +int reportTriangle(vertex* p, int size, FILE *out); circle circle_vvv(vertex, vertex, vertex); void readFile(char*, char*); vertex vector(vertex, vertex); @@ -29,5 +29,8 @@ void printPoint(vertex); void delaunayCws(vertex*, int, FILE*, int); void drawPoints(vertex*, int); void write_ppm(const char*); +void clockwiseNextDelaunayEdge(int *p, int *q, int *r, vertex *S, int *size, int *right, FILE *out); +int firstDelaunayEdgeIncidentTo(int j, vertex* S, int size); +void printVertex(vertex); #endif /* SRC_DELAUNAY_H_ */ diff --git a/src/helper.c b/src/helper.c index f21b71dd9e154ab8bf585f1f11e4fe11ca9476c1..6c9db68ea40e2d6da899f9e14c918cf9c658e0f6 100644 --- a/src/helper.c +++ b/src/helper.c @@ -7,8 +7,8 @@ #include "helper.h" -void svgDrawLine(line l, FILE *out) { - fprintf(out, "<line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\" style=\"stroke:rgb(255,0,0);stroke-width:1\" />\n", l.a.x*W, l.a.y*H, l.b.x*W, l.b.y*H); +void svgDrawLine(line l, FILE *out, char *color) { + fprintf(out, "<line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\" style=\"stroke:rgb(%s);stroke-width:1\" />\n", l.a.x*W, l.a.y*H, l.b.x*W, l.b.y*H, color); } void svgDrawCircle(circle c, FILE *out) { diff --git a/src/helper.h b/src/helper.h index 8d94e13c9ed5781379b8234b710707a1481f1ea2..a550a0d3c2cc390dcc31b9ee3bb4ffc10a9df198 100644 --- a/src/helper.h +++ b/src/helper.h @@ -30,7 +30,7 @@ typedef struct { void writeSvgStart (FILE*); void writeSvgEnd (FILE*); -void svgDrawLine(line, FILE *); +void svgDrawLine(line, FILE *, char *); void svgDrawCircle(circle, FILE *); #endif /* HELPER_H_ */ diff --git a/src/main.c b/src/main.c index 16c6bcc7cf9fbd3f13525b80cf9c2d6bcedb1966..eec7e85dfd7770068567e10b7cdee9773168046e 100644 --- a/src/main.c +++ b/src/main.c @@ -6,14 +6,15 @@ */ #include "delaunay.h" +#include "voronoi.h" int main(int argc, char *argv[]) { //input: 1. function 0=cws_delaunay 2. # of points, 3. draw? 4. # of executions //create_image(); //write_ppm ( "sine.ppm" ); int numberOfPoints = 10; - int function = 0; - int draw = 0; + int function = 1; + int draw = 1; int executions = 1; if(argc > 0) { //printf("%s\n", argv[0]); @@ -21,7 +22,7 @@ int main(int argc, char *argv[]) { //printf("%d\n", argc); if(argc == 2) { //printf("%s\n", argv[1]); - numberOfPoints = atoi(argv[1]); + //numberOfPoints = atoi(argv[1]); } @@ -45,6 +46,24 @@ int main(int argc, char *argv[]) { fclose(out); } } + break; + case 1: + for(int i = 0; i < executions; i++) { + FILE *out = NULL; + if(draw) { + + out = fopen ( "voronoi.html", "w" ); + writeSvgStart ( out ); + } + voronoiCws(points, numberOfPoints, out, draw); + if(draw){ + writeSvgEnd ( out ); + fclose(out); + } + } + break; + default: + break; } //readFile("test.txt", "r"); diff --git a/src/voronoi.c b/src/voronoi.c index 16988eaff82084b84f04e366b0dbfa8490dce64f..bd5f902091f7365a25e5c8fde3e1eac9ca895202 100644 --- a/src/voronoi.c +++ b/src/voronoi.c @@ -7,4 +7,176 @@ #include "voronoi.h" +void printVertexCircle(vertexCircle vc) { + printf("("); + fflush( stdout ); + printVertex(vc.a); + printf(", "); + fflush( stdout ); + printVertex(vc.b); + printf(", "); + fflush( stdout ); + printVertex(vc.c); + printf(")"); + fflush( stdout ); +} + +void printEdgeCircle(edgeCircle ec) { + printVertexCircle(ec.a); + printf(", "); + fflush(stdout); + printVertexCircle(ec.b); + printf("\n"); + fflush(stdout); +} + +circle center(vertexCircle vc) { + + circle c = circle_vvv(vc.a, vc.b, vc.c); + return c; +} +int reportEdge(line l, FILE *out) { + //svgDrawCircle(a,out); + //svgDrawCircle(b,out); + char *color = "0,0,255"; + svgDrawLine(l, out, color); + return 0; +} + +vertex vertexCircle2vertex(vertexCircle vc) { + vertex v = center(vc).center; + return v; +} + +/** + * returns the vertex, that you get, when you go right from the center of a and b until you hit the border of the room, where the points are + * @param a + * @param b + * @return + */ +vertex findBorderVertex(vertex a, vertex b) { + vertex ret = (vertex){-1,-1}; + vertex center = {(a.x+b.x)/2, (a.y+b.y)/2}; + //der Vektor, den man erhaelt, wenn man den Vektor ab nach rechts kippt + vertex dir = {(b.y-a.y), (a.x-b.x)}; + double m = (0 - center.x)/dir.x; + double y = center.y + m * dir.y; + if(m > 0 && (y > 0 && y < 1)) { + ret = (vertex){0, y}; + printf("%f = (0 - %f)/%f\n", m, center.x, dir.x); + printf("y = %f = %f + %f * %f\n", y, center.y, m, dir.y); + printVertex(a); + printf("\n"); + printVertex(b); + printf("\n"); + printVertex(ret); + printf("\n"); + return ret; + } + m = (1 - center.x)/dir.x; + y = center.y + m * dir.y; + if(m > 0 && (y > 0 && y < 1)) { + ret = (vertex){1, y}; + printf("%f = (1 - %f)/%f\n", m, center.x, dir.x); + printf("y = %f = %f + %f * %f\n", y, center.y, m, dir.y); + printVertex(a); + printf("\n"); + printVertex(b); + printf("\n"); + printVertex(ret); + printf("\n"); + return ret; + } + m = (0 - center.y)/dir.y; + double x = center.x + m * dir.x; + if(m > 0 && (x > 0 && x < 1)) { + ret = (vertex){x, 0}; + printVertex(a); + printf("\n"); + printVertex(b); + printf("\n"); + printVertex(ret); + printf("\n"); + return ret; + } + m = (1 - center.y)/dir.y; + x = center.x + m * dir.x; + if(m > 0 && (x > 0 && x < 1)) { + ret = (vertex){x, 1}; + printVertex(a); + printf("\n"); + printVertex(b); + printf("\n"); + printVertex(ret); + printf("\n"); + return ret; + } + return ret; +} + +int outOfBorder(vertex v) { + if(v.y > 1 || v.y < 0 || v.x > 1 || v.x < 0) { + return 1; + } + return 0; +} + +void voronoiCws(vertex* S, int size, FILE *out, int draw) { + for(int i = 0; i < size; i++){ + int j = firstDelaunayEdgeIncidentTo(i, S, size); + int j0 = j; + vertex lastV; + vertex currentV; + vertex firstV; + int first = 1; + do { + int k; + int isTriangle; + clockwiseNextDelaunayEdge(&i, &j, &k, S, &size, &isTriangle, out); + if(isTriangle == 2) { + currentV = vertexCircle2vertex((vertexCircle){S[i], S[j], S[k]}); + if(first) { + first = 0; + firstV = currentV; + } else { + if(!outOfBorder(currentV)) { + reportEdge((line){currentV, lastV}, out); + } + if(k == j0) { + reportEdge((line){currentV, firstV}, out); + } + } + } else if(isTriangle == 1) { + //hier ist ein aeusserer Rand der Triangulierung erreicht, also muss der naechste Punkt des Diagramms per Hand ausgerechnet werden + //lastV = vertexCircle2vertex((vertexCircle){S[i], S[j], S[k]}); + currentV = findBorderVertex(S[j], S[i]); + if(first) { + first = 0; + firstV = currentV; + } else { + if(!outOfBorder(lastV)) { + reportEdge((line){currentV, lastV}, out); + } + } + currentV = findBorderVertex(S[i], S[k]); + if(k == j0) { + if(!outOfBorder(firstV)) { + reportEdge((line){currentV, firstV}, out); + } + } + } + lastV = currentV; + if(i < j && i < k) { + if(isTriangle == 2){ + vertex tri[3] = {S[i], S[j], S[k]}; + if(draw){ + reportTriangle(tri, 3, out); + } + } + } + j = k; + } while(j != j0); + } +} + diff --git a/src/voronoi.h b/src/voronoi.h index 07b7f414d1f5cf9a9aead66298c134ec5fe877d0..6faf18015142c2098e1b77aa8a2bb166ca5e29f9 100644 --- a/src/voronoi.h +++ b/src/voronoi.h @@ -10,6 +10,7 @@ #include "delaunay.h" + /** * a vertex represented by a circle, where it is the center */ @@ -49,5 +50,9 @@ edgeCircle clockwiseNextVoronoiEdge(edgeCircle e); */ edgeCircle nextVoronoiEdgeOnBoudary(edgeCircle e); +int reportEdge(line l, FILE *out); + +void voronoiCws(vertex* S, int size, FILE *out, int draw); + #endif /* SRC_VORONOI_H_ */