comp20003-project01/dcel.c

69 lines
1.6 KiB
C
Raw Normal View History

/* dcel.c
*
* Created by Rory Healy (healyr@student.unimelb.edu.au)
* Created on 25th August 2021
* Last modified 25th August 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
struct halfEdge {
halfEdge_t *previous;
halfEdge_t *next;
halfEdge_t *twin;
int face;
int edge;
};
struct vertex {
double x;
double y;
};
struct edge {
halfEdge_t halfEdge;
};
struct face {
halfEdge_t start;
};
/* 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);
}