comp20003-project02/dcel.c

49 lines
1.4 KiB
C

/* dcel.c
*
* Created by Rory Healy (healyr@student.unimelb.edu.au)
* Created on 25th August 2021
* Last modified 9th September 2021
*
* Contains functions for the DCEL data structure, including creating,
* splitting an edge, and identifying towers in faces.
*
*/
#ifndef DCEL_HEADER
#include "dcel.h"
#endif
#ifndef COMMON_HEADER
#include "common.h"
#endif
/* Reads the polygon file and stores the information in the vertices array. */
vertex_t **readPolygon(vertex_t **vertices, FILE *polygonFile, \
int *numVertices) {
double currentX, currentY;
int maxSizeVertices = 1;
while ((fscanf(polygonFile, "%lf %lf", &currentX, &currentY)) != EOF) {
/* Check if there enough space in the towers array */
if (*numVertices == maxSizeVertices) {
maxSizeVertices *= 2;
vertex_t **temp = realloc(vertices, \
maxSizeVertices * sizeof(*vertices));
checkNullPointer(temp);
vertices = temp;
}
/* The current vertex being filled in with information */
vertices[*numVertices] = malloc(sizeof(*vertices[*numVertices]));
vertices[*numVertices]->x = currentX;
vertices[*numVertices]->y = currentY;
*numVertices += 1;
}
return vertices;
}
void freeVertices(vertex_t **vertices, int numVertices) {
for (int i = 0; i < numVertices; i++) {
free(vertices[i]);
}
free(vertices);
}