Completed stage 1
This commit is contained in:
parent
fe1f457038
commit
aa2690dbd3
9 changed files with 82 additions and 18 deletions
BIN
dcel.o
BIN
dcel.o
Binary file not shown.
39
geometry.c
39
geometry.c
|
@ -17,7 +17,40 @@
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
char *getBisectorEquation(vertex_t *vertexA, vertex_t *vertexB) {
|
bisector_t getBisector(vertex_t *pointA, vertex_t *pointB) {
|
||||||
printf("x: %lf y: %lf\n", vertexA->x, vertexB->y);
|
bisector_t newBisector;
|
||||||
return "hi";
|
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;
|
||||||
}
|
}
|
||||||
|
|
10
geometry.h
10
geometry.h
|
@ -6,4 +6,14 @@ typedef struct vertex {
|
||||||
double y;
|
double y;
|
||||||
} vertex_t;
|
} 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
|
#endif
|
BIN
geometry.o
BIN
geometry.o
Binary file not shown.
|
@ -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
|
29
voronoi.c
29
voronoi.c
|
@ -22,15 +22,6 @@
|
||||||
#define MAX_FIELD_LEN 128
|
#define MAX_FIELD_LEN 128
|
||||||
#define NUM_CSV_FIELDS 6
|
#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) {
|
tower_t **readTowers(tower_t **towers, FILE *datasetFile, int *numTowers) {
|
||||||
/* Maximum length of a single CSV line */
|
/* Maximum length of a single CSV line */
|
||||||
size_t lineBufferSize = MAX_CSV_ENTRY_LEN + 1;
|
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) {
|
vertex_t **readPoints(vertex_t **points, FILE *pointsFile, int *numPoints) {
|
||||||
/* Current length of a single points line */
|
/* Current length of a single points line */
|
||||||
size_t lineBufferSize = 1;
|
size_t lineBufferSize = 50;
|
||||||
|
|
||||||
/* Stores the current line from the points file */
|
/* Stores the current line from the points file */
|
||||||
char *lineBuffer = malloc(lineBufferSize * sizeof(*lineBuffer));
|
char *lineBuffer = malloc(lineBufferSize * sizeof(*lineBuffer));
|
||||||
|
@ -169,10 +160,24 @@ void stage1(char *pointsFileName, char *outputFileName) {
|
||||||
pointsFile = safeFileOpen(&pointsFile, pointsFileName, "r");
|
pointsFile = safeFileOpen(&pointsFile, pointsFileName, "r");
|
||||||
outputFile = safeFileOpen(&outputFile, outputFileName, "w");
|
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);
|
checkNullPointer(points);
|
||||||
int numPoints = 0;
|
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);
|
freePoints(points, numPoints);
|
||||||
fclose(pointsFile);
|
fclose(pointsFile);
|
||||||
|
|
16
voronoi.h
16
voronoi.h
|
@ -5,20 +5,30 @@
|
||||||
#ifndef VORONOI_HEADER
|
#ifndef VORONOI_HEADER
|
||||||
#define 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 */
|
/* Reads the CSV file and stores the information in the towers array */
|
||||||
tower_t **readTowers(tower_t **towers, FILE *datasetFile, int *numTowers);
|
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);
|
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);
|
void freeTowers(tower_t **towers, int numTowers);
|
||||||
|
|
||||||
/* Reads a points file and stores the information in the points array */
|
/* Reads a points file and stores the information in the points array */
|
||||||
vertex_t **readPoints(vertex_t **points, FILE *pointsFile, int *numPoints);
|
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 */
|
/* Outputs the bisector equations from pointsFile into outputFile */
|
||||||
void stage1(char *pointsFileName, char *outputFileName);
|
void stage1(char *pointsFileName, char *outputFileName);
|
||||||
|
|
||||||
|
|
BIN
voronoi.o
BIN
voronoi.o
Binary file not shown.
BIN
voronoi2
BIN
voronoi2
Binary file not shown.
Loading…
Reference in a new issue