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

weiteres Code-Clean-up und verbesserte Implementierung

parent cd8f9e97
No related branches found
No related tags found
No related merge requests found
......@@ -13,78 +13,23 @@
int comparePointsByX (const void * a, const void * b) {
vertex* pointA = (vertex*) a;
vertex* pointB = (vertex*) b;
if(pointA->x > pointB->x)
return 1;
if(pointA->x < pointB->x)
return -1;
return 0;
double distanceSquared(vertex p1, vertex p2) {
double x = p1.x - p2.x;
double y = p1.y-p2.y;
return (x*x+y*y);
}
/*int reportLine(vertex* p, int size, FILE *out) {
line l = {p[0], p[1]};
svgDrawLine(l, out);
l.a = p[0];
l.b = p[2];
svgDrawLine(l, out);
return 1;
}*/
void reportTriangle(vertex a, vertex b, vertex c) {
// qsort (p, size, sizeof(*p), comparePointsByX);
//
// char *color = "255,0,0";
#ifdef OUTPUTFILE
svgDrawTriangle(a, b, c);
#endif
// 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);
}
//http://mathforum.org/library/drmath/view/54323.html
circle circle_vvv(vertex v1, vertex v2, vertex v3)
{
circle c;
double bx = v1.x; double by = v1.y;
double cx = v2.x; double cy = v2.y;
double dx = v3.x; double dy = v3.y;
double temp = cx*cx+cy*cy;
double bc = (bx*bx + by*by - temp)/2.0;
double cd = (temp - dx*dx - dy*dy)/2.0;
double det = (bx-cx)*(cy-dy)-(cx-dx)*(by-cy);
if (fabs(det) < 1.0e-6) {
c.center.x = c.center.y = 1.0;
return c;
}
det = 1/det;
c.center.x = (bc*(cy-dy)-cd*(by-cy))*det;
c.center.y = ((bx-cx)*cd-(cx-dx)*bc)*det;
cx = c.center.x; cy = c.center.y;
c.radius = distance(v1, c.center);
return c;
}
double det2(double a, double b, double c, double d) {
return a*d-b*c;
}
......@@ -102,34 +47,14 @@ void circleCenter(const vertex* v1, const vertex* v2, const vertex* v3,
center->y = det2(ax, cx, ay, cy) / det;
}
/*
* 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
*/
//TODO seperate radius from this function
circle circleFrom3Points(vertex v1, vertex v2, vertex v3) {
circle c;
circleCenter(&v1, &v2, &v3, &c.center);
c.radius = sqrt((v1.x-c.center.x)*(v1.x-c.center.x)+(v1.y-c.center.y)*(v1.y-c.center.y));
c.radius = distanceSquared((v1),(c.center));
return c;
}
/*void reportTriangle(struct Triangle triangle) {
printf("%f, %f - %f, %f - %f, %f\n", triangle.a.x, triangle.a.y, triangle.b.x, triangle.b.y, triangle.c.x, triangle.c.y);
}*/
void printPoint(vertex point) {
//printf("%f, %f\n", point.x, point.y);
}
double calcArea(vertex p, vertex q, vertex r) {
//return 0.5*(-q.x*p.y+r.x*p.y+p.x*q.y-r.x*q.y-p.x*r.y+q.x*r.y);
return 0.5*(p.x*(q.y-r.y)-p.y*(q.x-r.x)+(q.x*r.y-q.y*r.x));
}
/**
*
......@@ -141,8 +66,8 @@ double distance(vertex p1, vertex p2) {
return hypot(p1.x - p2.x, p1.y-p2.y);
}
//https://math.stackexchange.com/questions/1232773/is-the-point-on-the-left-or-the-right-of-the-vector-in-2d-space
//https://betterexplained.com/articles/cross-product/
/**
*
* @param p
......@@ -172,8 +97,6 @@ vertex vector(vertex a, vertex b) {
}
/**
* law of cosines https://www.mathsisfun.com/algebra/trig-cosine-law.html
* other solutions https://stackoverflow.com/questions/1211212/how-to-calculate-an-angle-from-three-points
* @param q
* @param p
* @param r
......@@ -191,15 +114,15 @@ int firstDelaunayEdgeIncidentTo(int j, vertex* S, int size) {
double minDist, dist;
if(j != 0) {
k = 0;
minDist = distance(S[j], S[0]);
minDist = distanceSquared(S[j], S[0]);
} else /*if(j != 1)*/ {
k = 1;
minDist = distance(S[j], S[1]);
minDist = distanceSquared(S[j], S[1]);
}
//TODO erst bei j anfangen? problem: es ist nicht unbedingt die kleinste seite, wenn wir spaeter anfangen
for(int i = 0; i < size; i++) {
if(j != i) {
dist = distance(S[j], S[i]);
dist = distanceSquared(S[j], S[i]);
if(dist < minDist) {
k = i;
minDist = dist;
......@@ -209,14 +132,6 @@ int firstDelaunayEdgeIncidentTo(int j, vertex* S, int size) {
return k;
}
int arrayContains(int *jAlreadyUsed, int numberOfJ, int j){
for(int i = 0; i < numberOfJ; i++) {
if(jAlreadyUsed[i] == j) {
return 1;
}
}
return 0;
}
void clockwiseNextDelaunayEdge(const int *p, const int *q, int *r, const vertex *S, const int *size, int *right) {
//0: r not initialized, 1: r is left, 2: r is right
......@@ -238,7 +153,7 @@ void clockwiseNextDelaunayEdge(const int *p, const int *q, int *r, const vertex
c = circleFrom3Points(S[*p], S[*q], S[*r]);
} else {
//if inside circle: calculate new circle, else: do nothing
if(distance(c.center, S[i]) < c.radius) {
if(distanceSquared(c.center, S[i]) < c.radius) {
*r = i;
c = circleFrom3Points(S[*p], S[*q], S[*r]);
}
......@@ -289,11 +204,3 @@ void delaunayCws(vertex* S, int size) {
}
}
//https://stackoverflow.com/questions/29762048/c-structure-to-string
char* vertexToString(vertex v) {
int len = 0;
len = snprintf(NULL, len, "%f,%f", v.x, v.y);
char* str = malloc(sizeof(char) * len + 1);
snprintf(str, sizeof(str), "%f,%f", v.x, v.y);
return str;
}
......@@ -18,7 +18,6 @@
void reportTriangle(vertex, vertex, vertex);
circle circle_vvv(vertex, vertex, vertex);
vertex vector(vertex, vertex);
double distance(vertex, vertex);
void printPoint(vertex);
......
......@@ -23,7 +23,7 @@ void svgDrawCircle(circle c) {
void svgDrawTriangle(vertex a, vertex b, vertex c) {
#ifdef OUTPUTFILE
fprintf(out, "<polygon vector-effect=\"non-scaling-stroke\" points=\"%f,%f %f,%f %f,%f\" style=\"fill:none;stroke:black;stroke-width:1\" />\n", a.x, a.y, b.x, b.y, c.x, c.y);
fprintf(out, "<polygon vector-effect=\"non-scaling-stroke\" points=\"%f,%f %f,%f %f,%f\" style=\"fill:none;stroke:red;stroke-width:1\" />\n", a.x, a.y, b.x, b.y, c.x, c.y);
#endif
}
......
......@@ -11,7 +11,7 @@
/**
* use if you want to have an output
*/
#define OUTPUTFILE "output.html"
//#define OUTPUTFILE "output.html"
#include <stdio.h>
......@@ -22,8 +22,8 @@ double viewboxMinX;
double viewboxMinY;
double viewboxMaxX;
double viewboxMaxY;
#define W 400 //size of the svg-File
#define R 1.0 //Point size
#define W 1000 //size of the svg-File
#define R .2 //Point size
#endif
......
......@@ -2,12 +2,13 @@
/**
* use if you want to use command line input
*/
#define INPUT
//#define INPUT
/**
* used for doing the Runtimetest
*/
//#define RUNTIMETEST
#define RUNTIMETEST
/*
* main.c
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment