diff --git a/dcel.o b/dcel.o index bda734f..88feeaf 100644 Binary files a/dcel.o and b/dcel.o differ diff --git a/geometry.c b/geometry.c index 8dfe276..ea75e12 100644 --- a/geometry.c +++ b/geometry.c @@ -17,7 +17,40 @@ #include "common.h" #endif -char *getBisectorEquation(vertex_t *vertexA, vertex_t *vertexB) { - printf("x: %lf y: %lf\n", vertexA->x, vertexB->y); - return "hi"; +bisector_t getBisector(vertex_t *pointA, vertex_t *pointB) { + bisector_t newBisector; + double midpointX = (pointA->x + pointB->x) / 2; + double midpointY = (pointA->y + pointB->y) / 2; + + newBisector.x = midpointX; + newBisector.y = midpointY; + + /* Calculating bisector slope according to slope of AB */ + if (pointA->x == pointB->x) { + /* The line segment AB has an infinite gradient, + * so the orthogonal line will have zero slope. + */ + newBisector.slope = 0; + newBisector.isSlopeInfinite = 0; + } else if (pointA->y == pointB->y) { + /* The line segment AB has gradient of zero, so + * the orthogonal line will have an infinite slope. + */ + newBisector.isSlopeInfinite = 1; + + /* Not actually zero, just a placeholder to prevent + * accidental errors + */ + newBisector.slope = 0; + } else { + /* Slope of the line segment AB */ + double segmentSlope = \ + (pointB->y - pointA->y) / (pointB->x - pointA->x); + + /* Calculate orthogonal slope */ + newBisector.isSlopeInfinite = 0; + newBisector.slope = -1 / segmentSlope; + } + + return newBisector; } diff --git a/geometry.h b/geometry.h index ddd4b3d..f199c17 100644 --- a/geometry.h +++ b/geometry.h @@ -6,4 +6,14 @@ typedef struct vertex { double y; } vertex_t; +typedef struct bisector { + int isSlopeInfinite; + double slope; + double x; + double y; +} bisector_t; + +/* Calculates and returns the equation of a bisector of two points */ +bisector_t getBisector(vertex_t *vertexA, vertex_t *vertexB); + #endif \ No newline at end of file diff --git a/geometry.o b/geometry.o index 46d9a01..5e75cfe 100644 Binary files a/geometry.o and b/geometry.o differ diff --git a/output.txt b/output.txt index e69de29..7c243ae 100644 --- a/output.txt +++ b/output.txt @@ -0,0 +1,6 @@ +y = 0.000000 * (x - 145.600000) + -34.700000 +y = 0.000000 * (x - 145.600000) + -35.200000 +x = 147.100000 +x = 147.100000 +x = 147.600000 +y = 1.000000 * (x - 147.600000) + -33.200000 diff --git a/voronoi.c b/voronoi.c index 816641e..c012bb0 100644 --- a/voronoi.c +++ b/voronoi.c @@ -22,15 +22,6 @@ #define MAX_FIELD_LEN 128 #define NUM_CSV_FIELDS 6 -struct tower { - char *id; - char *postcode; - char *manager; - int population; - double x; - double y; -}; - tower_t **readTowers(tower_t **towers, FILE *datasetFile, int *numTowers) { /* Maximum length of a single CSV line */ size_t lineBufferSize = MAX_CSV_ENTRY_LEN + 1; @@ -120,7 +111,7 @@ void freeTowers(tower_t **towers, int numTowers) { vertex_t **readPoints(vertex_t **points, FILE *pointsFile, int *numPoints) { /* Current length of a single points line */ - size_t lineBufferSize = 1; + size_t lineBufferSize = 50; /* Stores the current line from the points file */ char *lineBuffer = malloc(lineBufferSize * sizeof(*lineBuffer)); @@ -169,10 +160,24 @@ void stage1(char *pointsFileName, char *outputFileName) { pointsFile = safeFileOpen(&pointsFile, pointsFileName, "r"); outputFile = safeFileOpen(&outputFile, outputFileName, "w"); - vertex_t **points = malloc(sizeof(*points)); + /* Points are given as pairs, so initialise 2 points */ + vertex_t **points = malloc(sizeof(*points) * 2); checkNullPointer(points); int numPoints = 0; - // points = readPoints(points, pointsFile, &numPoints); + points = readPoints(points, pointsFile, &numPoints); + + /* For each pair, calculate and print the bisector to outputFile */ + for (int i = 0; i < numPoints; i += 2) { + bisector_t currBisector = getBisector(points[i], points[i + 1]); + + /* Cannot print infinite slope, so print only x-intercept */ + if (currBisector.isSlopeInfinite) { + fprintf(outputFile, "x = %lf\n", currBisector.x); + } else { + fprintf(outputFile, "y = %lf * (x - %lf) + %lf\n", \ + currBisector.slope, currBisector.x, currBisector.y); + } + } freePoints(points, numPoints); fclose(pointsFile); diff --git a/voronoi.h b/voronoi.h index a5e8d0c..905c1b8 100644 --- a/voronoi.h +++ b/voronoi.h @@ -5,20 +5,30 @@ #ifndef VORONOI_HEADER #define VORONOI_HEADER -typedef struct tower tower_t; +typedef struct tower { + char *id; + char *postcode; + char *manager; + int population; + double x; + double y; +} tower_t; /* Reads the CSV file and stores the information in the towers array */ tower_t **readTowers(tower_t **towers, FILE *datasetFile, int *numTowers); -/* Reads the current row from the CSV into a new tower */ +/* Reads the current row from the CSV and converts it into a new tower */ void readCurrentTower(char *lineBuffer, tower_t *tower); -/* Takes a line from the CSV and converts the data into a tower_t format */ +/* Frees all towers in a towers array */ void freeTowers(tower_t **towers, int numTowers); /* Reads a points file and stores the information in the points array */ vertex_t **readPoints(vertex_t **points, FILE *pointsFile, int *numPoints); +/* Frees all points from a points array */ +void freePoints(vertex_t **points, int numPoints); + /* Outputs the bisector equations from pointsFile into outputFile */ void stage1(char *pointsFileName, char *outputFileName); diff --git a/voronoi.o b/voronoi.o index 7a5cc09..707d2cc 100644 Binary files a/voronoi.o and b/voronoi.o differ diff --git a/voronoi2 b/voronoi2 index 8f53c7b..9096ef9 100755 Binary files a/voronoi2 and b/voronoi2 differ