Skip to content
Snippets Groups Projects
Commit 1cd7e48e authored by mika's avatar mika
Browse files

add/mod dwyer, rem unnec functs, changed output

parent 2dba2226
Branches
No related tags found
No related merge requests found
......@@ -6,3 +6,5 @@
/perf.data
/perf.data.old
/.cproject
/Release/
/test.ps
/language.settings.xml
/org.eclipse.cdt.managedbuilder.core.prefs
eclipse.preferences.version=1
environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.913981392/CPATH/delimiter=;
environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.913981392/CPATH/operation=remove
environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.913981392/C_INCLUDE_PATH/delimiter=;
environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.913981392/C_INCLUDE_PATH/operation=remove
environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.913981392/append=true
environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.913981392/appendContributed=true
environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.mingw.exe.debug.913981392/LIBRARY_PATH/delimiter=;
environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.mingw.exe.debug.913981392/LIBRARY_PATH/operation=remove
environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.mingw.exe.debug.913981392/append=true
environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.mingw.exe.debug.913981392/appendContributed=true
/*
* fortuneVoronoi.c
*
* Created on: Dec 1, 2018
* Author: mika
*/
......@@ -24,22 +24,6 @@ int comparePointsByX (const void * a, const void * b) {
return 0;
}
/**
* draws a line from 1 Point to the other
* @param p Array with 2 Points
* @return
*/
int drawLine(vertex* p) {
qsort (p, 2, sizeof(*p), comparePointsByX);
double m = (p[1].y - p[0].y)/(p[1].x - p[0].x);
double n = p[0].y*H - m * p[0].x*W;
for(int x = p[0].x*W; x < p[1].x*W; x++) {
int y = (m*x + n);
r[y][x] = 255, g[y][x] = 255, b[y][x] = 255;
}
return 1;
}
......@@ -52,105 +36,28 @@ int drawLine(vertex* p) {
return 1;
}*/
/* Write the ppm-formatted file */
int reportTriangle(vertex* p, int size, FILE *out) {
for(int i = 0; i < 3; i++) {
printVertex(p[i]);
printf("; ");
}
printf("\n");
qsort (p, size, sizeof(*p), comparePointsByX);
//qsort (p, size, sizeof(*p), comparePointsByX);
char *color = "255,0,0";
//char *color = "255,0,0";
line l = {p[0], p[1]};
svgDrawTriangle(*(p), *(p+1), *(p+2), out);
/*edge l = {p[0], p[1]};
svgDrawLine(l, out, color);
l.b = p[2];
svgDrawLine(l, out, color);
l.a = p[1];
svgDrawLine(l, out, color);
svgDrawLine(l, out, color);*/
return 1;
}
void write_ppm ( const char *filename )
{
FILE *out = fopen ( filename, "w" );
/* Header:
* Magic number, width, height, maxvalue
*/
fprintf ( out, "P6 %d %d %d\n", W, H, M );
/* Rows. 2-byte values if max > 255, 1-byte otherwise.
* These loops explicitly show where every single byte
* goes; in practice, it would be faster and shorter to
* interleave the arrays and write bigger blocks of
* contiguous data.
*/
for ( size_t i=0; i<H; i++ )
for ( size_t j=0; j<W; j++ )
{
fwrite ( &r[i][j], sizeof(uint8_t), 1, out ); /* Red */
fwrite ( &g[i][j], sizeof(uint8_t), 1, out ); /* Green */
fwrite ( &b[i][j], sizeof(uint8_t), 1, out ); /* Blue */
}
fclose ( out );
}
void drawPoints(vertex* points, int size) {
for(int i = 0; i < size; i++) {
int x = points[i].x*W;
int y = points[i].y*H;
r[y][x] = 255, g[y][x] = 255, b[y][x] = 255;
y++;
r[y][x] = 255, g[y][x] = 255, b[y][x] = 255;
y = y - 2;
r[y][x] = 255, g[y][x] = 255, b[y][x] = 255;
x++;
r[y][x] = 255, g[y][x] = 255, b[y][x] = 255;
x = x - 2;
r[y][x] = 255, g[y][x] = 255, b[y][x] = 255;
y++;
r[y][x] = 255, g[y][x] = 255, b[y][x] = 255;
x = x + 2;
r[y][x] = 255, g[y][x] = 255, b[y][x] = 255;
y++;
r[y][x] = 255, g[y][x] = 255, b[y][x] = 255;
x = x - 2;
r[y][x] = 255, g[y][x] = 255, b[y][x] = 255;
}
}
//TODO ueberarbeiten, bisher nur copy paste von https://mathematica.stackexchange.com/questions/36995/code-to-give-center-of-circle-given-three-points-in-2d
//TODO eventuell hiermit http://paulbourke.net/geometry/circlesphere/
void circleThrough3Points(vertex p1, vertex p2, vertex p3, double* circle) {
double ax, ay, bx, by, cx, cy, a, b, c, d, e, f, g, centerx, centery, r;
ax = p1.x;
ay = p1.y;
bx = p2.x;
by = p2.y;
cx = p3.x;
cy = p3.y;
a = bx - ax;
b = by - ay;
c = cx - ax;
d = cy - ay;
e = a*(ax + bx) + b*(ay + by);
f = c*(ax + cx) + d*(ay + cy);
g = 2*(a*(cy - by) - b*(cx - bx));
if(g != 0) {
centerx = (d*e - b*f)/g;
centery = (a*f - c*e)/g;
r = sqrt(pow((ax - centerx),2) + pow((ay - centery),2));
}
circle[1] = centerx;
circle[2] = centery;
circle[3] = r;
}
//http://mathforum.org/library/drmath/view/54323.html
circle circle_vvv(vertex v1, vertex v2, vertex v3)
......@@ -180,6 +87,11 @@ double det2(double a, double b, double c, double d) {
return a*d-b*c;
}
/*
* https://stackoverflow.com/questions/22791951/algorithm-to-find-an-arc-its-center-radius-and-angles-given-3-points
* https://de.wikipedia.org/wiki/Cramersche_Regel
* https://de.wikipedia.org/wiki/Determinante
*/
circle circleFrom3Points(vertex v1, vertex v2, vertex v3) {
circle c;
double ax = 2*(v1.x-v2.x);
......@@ -202,7 +114,7 @@ void printPoint(vertex point) {
//printf("%f, %f\n", point.x, point.y);
}
void generatePoints(vertex points[], int sizeOfPoints) {
void generatePoints(vertex* points, int sizeOfPoints) {
double x, y;
int seed = time(NULL);
srand(seed);
......@@ -211,7 +123,6 @@ void generatePoints(vertex points[], int sizeOfPoints) {
y = (double) rand()/RAND_MAX;
points[i] = (vertex) {x, y};
}
return;
}
/**
......@@ -344,7 +255,7 @@ void clockwiseNextDelaunayEdge(int *p, int *q, int *r, vertex *S, int *size, int
*r = i;
c = circleFrom3Points(S[*p], S[*q], S[*r]);
} else {
//if inside circle: calc new circle, else: do nothing
//if inside circle: calculate new circle, else: do nothing
if(distance(c.center, S[i]) < c.radius) {
*r = i;
c = circleFrom3Points(S[*p], S[*q], S[*r]);
......@@ -360,19 +271,12 @@ void clockwiseNextDelaunayEdge(int *p, int *q, int *r, vertex *S, int *size, int
maxAngle = angle;
*r = i;
} else if(angle > maxAngle){
//printf("%f > %f\n", angle, maxAngle);
//printPoint(S[r]);
maxAngle = angle;
*r = i;
}
}
}
printf("right: %d\n", *right);
if(*right == 1) {
printf("maxAngle: %f\n", maxAngle);
} else if(*right == 2) {
printf("circle x: %f, y: %f, radius: %f\n", c.center.x, c.center.y, c.radius);
}
//if you want to draw the circle comment this line out
//svgDrawCircle(c, out);
}
......@@ -385,23 +289,12 @@ void delaunayCws(vertex* S, int size, FILE *out, int draw) {
//TODO eventuell nur j zurueckgeben (i haben wir ja schon)
int j = firstDelaunayEdgeIncidentTo(i, S, size);
// Find the point p[j] e S that is nearest to p[i]
//j0 stores all values j had already have TODO better solution
int jAlreadyUsed[size];
int numberOfJ = 0;
int j0 = j;
//int j1 = j;
do {
//TODO eventuell nur k zurueckgeben (i haben wir ja schon)
int k;
int isTriangle;
jAlreadyUsed[numberOfJ++] = j;
clockwiseNextDelaunayEdge(&i, &j, &k, S, &size, &isTriangle, out);
//bricht ab, wenn sich k und j abwechseln
//TODO die abwechslungszyklen koennen auch groesser sein
/*if(k == j1) {
break;
}*/
//printf("%d, %d, %d, %d\n", i, j, k, isTriangle);
if(i < j && i < k) {
if(isTriangle == 2){
vertex tri[3] = {S[i], S[j], S[k]};
......@@ -411,26 +304,17 @@ void delaunayCws(vertex* S, int size, FILE *out, int draw) {
}
}
}
//printf("j = %d, k = %d\n", j, k);
//printf("jAlreadyUsed = ");
/*for(int a = 0; a < numberOfJ; a++) {
printf("%d, ", jAlreadyUsed[a]);
}*/
//printf("\n");
//j1 = j;
j = k;
if(arrayContains(jAlreadyUsed, numberOfJ, j) && j != j0) {
printf("ERROR\n");
return;
}
} while(/*!arrayContains(jAlreadyUsed, numberOfJ, j)*/ j != j0);
} while(j != j0);
}
//return delaunayTriangles;
}
void printVertex(vertex v) {
printf("%f, %f", v.x, v.y);
fflush( stdout );
//https://stackoverflow.com/questions/29762048/c-structure-to-string
char* vertexToString(vertex v) {
int len = 0;
len = snprintf(NULL, len, "%f,%f", v.x*W, v.y*H);
char* str = malloc(sizeof(char) * len + 1);
snprintf(str, sizeof(str), "%f,%f", v.x*W, v.y*H);
return str;
}
......@@ -16,9 +16,6 @@
#include "helper.h"
/* 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*);
......@@ -31,6 +28,7 @@ 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);
char* vertexToString(vertex);
circle circleFrom3Points(vertex, vertex, vertex);
#endif /* SRC_DELAUNAY_H_ */
src/dwyer.c 0 → 100644
This diff is collapsed.
/*
* dwyer.h
*
* Created on: Dec 2, 2018
* Author: mika
*/
#ifndef SRC_DWYER_H_
#define SRC_DWYER_H_
#include "delaunay.h"
dwyer(char, vertex*, int);
#endif /* SRC_DWYER_H_ */
/*
* fortuneVoronoi.c
*
* Created on: Dec 1, 2018
* Author: mika
*/
#include "helper.h"
#include "delaunay.h"
......@@ -7,7 +7,7 @@
#include "helper.h"
void svgDrawLine(line l, FILE *out, char *color) {
void svgDrawLine(edge 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);
}
......@@ -15,6 +15,10 @@ void svgDrawCircle(circle c, FILE *out) {
fprintf(out, "<circle cx=\"%f\" cy=\"%f\" r=\"%f\" stroke=\"green\" stroke-width=\"1\" fill=\"none\" />", c.center.x*W, c.center.y*H, c.radius*W);
}
void svgDrawTriangle(vertex a, vertex b, vertex c, FILE *out) {
fprintf(out, "<polygon points=\"%f,%f %f,%f %f,%f\" style=\"fill:none;stroke:purple;stroke-width:1\" />", a.x*W, a.y*H, b.x*W, b.y*H, c.x*W, c.y*H);
}
void writeSvgStart (FILE *out) {
fprintf (out, "<!DOCTYPE html>\n<html>\n<body>\n<svg width=\"%d\" height=\"%d\">\n", W, H);
}
......
......@@ -18,6 +18,12 @@ typedef struct {
double x, y;
} vertex;
typedef struct {
vertex a;
vertex b;
vertex c;
};
typedef struct {
vertex center;
double radius;
......@@ -26,11 +32,12 @@ typedef struct {
typedef struct {
vertex a;
vertex b;
} line;
} edge;
void writeSvgStart (FILE*);
void writeSvgEnd (FILE*);
void svgDrawLine(line, FILE *, char *);
void svgDrawLine(edge, FILE *, char *);
void svgDrawCircle(circle, FILE *);
void svgDrawTriangle(vertex, vertex, vertex, FILE*);
#endif /* HELPER_H_ */
......@@ -12,7 +12,7 @@ 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 = 15000;
int numberOfPoints = 10000;
int function = 0;
int draw = 1;
int executions = 1;
......@@ -65,13 +65,18 @@ int main(int argc, char *argv[]) {
}
}
break;
case 2:
dwyer('m', points, numberOfPoints);
default:
break;
}
clock_t end = clock();
double number_of_clocks = (double) (end-begin);
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Number of clocks: %f\n", number_of_clocks);
printf("Time spent: %f\n", time_spent);
//readFile("test.txt", "r");
return EXIT_SUCCESS;
......
......@@ -7,35 +7,13 @@
#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);
circle c = circleFrom3Points(vc.a, vc.b, vc.c);
return c;
}
int reportEdge(line l, FILE *out) {
int reportEdge(edge l, FILE *out) {
//svgDrawCircle(a,out);
//svgDrawCircle(b,out);
char *color = "0,0,255";
......@@ -113,13 +91,13 @@ void voronoiCws(vertex* S, int size, FILE *out, int draw) {
} else {
if(!outOfBorder(currentV)) {
if(draw) {
reportEdge((line){currentV, lastV}, out);
reportEdge((edge){currentV, lastV}, out);
}
}
if(k == j0) {
if(draw) {
reportEdge((line){currentV, firstV}, out);
reportEdge((edge){currentV, firstV}, out);
}
}
......@@ -134,7 +112,7 @@ void voronoiCws(vertex* S, int size, FILE *out, int draw) {
} else {
if(!outOfBorder(lastV)) {
if(draw) {
reportEdge((line){currentV, lastV}, out);
reportEdge((edge){currentV, lastV}, out);
}
}
......@@ -142,7 +120,7 @@ void voronoiCws(vertex* S, int size, FILE *out, int draw) {
currentV = findBorderVertex(S[i], S[k]);
if(k == j0) {
if(!outOfBorder(firstV)) {
reportEdge((line){currentV, firstV}, out);
reportEdge((edge){currentV, firstV}, out);
}
}
}
......
......@@ -50,7 +50,7 @@ edgeCircle clockwiseNextVoronoiEdge(edgeCircle e);
*/
edgeCircle nextVoronoiEdgeOnBoudary(edgeCircle e);
int reportEdge(line l, FILE *out);
int reportEdge(edge l, FILE *out);
void voronoiCws(vertex* S, int size, FILE *out, int draw);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment