Completed stage 1

This commit is contained in:
Rory Healy 2021-09-09 21:23:53 +10:00
parent fe1f457038
commit aa2690dbd3
9 changed files with 82 additions and 18 deletions

BIN
dcel.o

Binary file not shown.

View file

@ -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;
}

View file

@ -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

Binary file not shown.

View file

@ -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

View file

@ -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);

View file

@ -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);

BIN
voronoi.o

Binary file not shown.

BIN
voronoi2

Binary file not shown.