comp20003-project02/geometry.c
2021-09-09 21:23:53 +10:00

56 lines
1.5 KiB
C

/* geometry.c
*
* Created by Rory Healy (healyr@student.unimelb.edu.au)
* Created on 9th September 2021
* Last modified 9th September 2021
*
* A small library of functions used in the geometric construction of a
* Voronoi diagram.
*
*/
#ifndef GEOMETRY_HEADER
#include "geometry.h"
#endif
#ifndef COMMON_HEADER
#include "common.h"
#endif
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;
}