comp20003-project02/voronoi.c
2021-09-13 12:27:17 +10:00

128 lines
4.4 KiB
C

/* voronoi.c
*
* Created by Rory Healy (healyr@student.unimelb.edu.au)
* Created on 12th August 2021
* Last modified 13th September 2021
*
* Contains functions involving the generation of the Voronoi diagram, and
* running each stage of the assignment.
*
*/
#ifndef VORONOI_HEADER
#include "voronoi.h"
#endif
void stage1PrintBisector(bisector_t *bisector, FILE *outputFile) {
if (bisector->isSlopeInfinite) {
/* Cannot print infinite slope, so print only x-intercept */
fprintf(outputFile, "x = %lf\n", bisector->mid->x);
} else {
fprintf(outputFile, "y = %lf * (x - %lf) + %lf\n", \
bisector->slope, bisector->mid->x, bisector->mid->y);
}
}
void stage2PrintIntersection(intersection_t *intersection, FILE *outputFile) {
fprintf(outputFile, "From Edge %d (%lf, %lf) to Edge %d (%lf, %lf)\n", \
intersection->fromEdge, intersection->fromPoint->x, \
intersection->fromPoint->y, intersection->toEdge, \
intersection->toPoint->x, intersection->toPoint->y);
}
void stage1(char *pointsFileName, char *outputFileName) {
FILE *pointsFile = NULL, *outputFile = NULL;
pointsFile = safeFileOpen(&pointsFile, pointsFileName, "r");
outputFile = safeFileOpen(&outputFile, outputFileName, "w");
/* 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);
/* There are half as many bisectors as points */
int numBisectors = numPoints / 2;
bisector_t **bisectors = malloc(sizeof(*bisectors) * numBisectors);
checkNullPointer(bisectors);
bisectors = getBisectors(bisectors, points, numBisectors);
/* For each pair, calculate and print the bisector to outputFile */
for (int i = 0; i < numBisectors; i++) {
stage1PrintBisector(bisectors[i], outputFile);
}
/* Clean up */
freePoints(points, numPoints);
freeBisectors(bisectors, numBisectors);
fclose(pointsFile);
fclose(outputFile);
}
void stage2(char *pointsFileName, char *polygonFileName, char *outputFileName) {
FILE *pointsFile = NULL, *outputFile = NULL;
pointsFile = safeFileOpen(&pointsFile, pointsFileName, "r");
outputFile = safeFileOpen(&outputFile, outputFileName, "w");
/* 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);
/* There are half as many bisectors as points */
int numBisectors = numPoints / 2;
bisector_t **bisectors = malloc(sizeof(*bisectors) * numBisectors);
checkNullPointer(bisectors);
bisectors = getBisectors(bisectors, points, numBisectors);
/* Construct the DCEL from the polygon file */
DCEL_t *dcel = readPolygonFile(polygonFileName);
/* Calculate and print intersections to the output file */
intersection_t **intersections = malloc(sizeof(*intersections));
checkNullPointer(intersections);
int numIntersections = 0;
intersections = getIntersections(intersections, &numIntersections, bisectors, numBisectors, dcel, DEFAULT_FACE, \
DEFAULTMINLENGTH);
for (int i = 0; i < numIntersections; i++) {
stage2PrintIntersection(intersections[i], outputFile);
}
/* Clean up */
freePoints(points, numPoints);
freeBisectors(bisectors, numBisectors);
freeIntersections(intersections, numIntersections);
freeDCEL(dcel);
fclose(pointsFile);
fclose(outputFile);
}
void stage3(char *dataFileName, char *polygonFileName, char *outputFileName) {
FILE *dataFile = NULL, *polygonFile = NULL, *outputFile = NULL;
dataFile = safeFileOpen(&dataFile, dataFileName, "r");
polygonFile = safeFileOpen(&polygonFile, polygonFileName, "r");
outputFile = safeFileOpen(&outputFile, outputFileName, "w");
/* Clean up */
fclose(dataFile);
fclose(polygonFile);
fclose(outputFile);
}
void stage4(char *dataFileName, char *polygonFileName, char *outputFileName) {
FILE *dataFile = NULL, *polygonFile = NULL, *outputFile = NULL;
dataFile = safeFileOpen(&dataFile, dataFileName, "r");
polygonFile = safeFileOpen(&polygonFile, polygonFileName, "r");
outputFile = safeFileOpen(&outputFile, outputFileName, "w");
/* Clean up */
fclose(dataFile);
fclose(polygonFile);
fclose(outputFile);
}