A repository of code for the book Computational Geometry in C, 2nd edition.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

146 lines
4.3 KiB

#ifndef CGC_MATH_H
#define CGC_MATH_H
#include "vertex.h"
#define X 0
#define Y 1
#define Z 2
/**
* Given 3 vertices, calculates the two dimensional area of the triangle
* represented by those shapes.
*
* Integers are used here for simplicity. Doubles would be nice too but
* for now the lack of rounding error in integers is sufficient for the
* purpose.
*
* @param a The first vertex.
* @param b The second vertex.
* @param c The third vertex.
* @return The integer area of the polygon.
*/
int area2(const struct Vertex *a, const struct Vertex *b,
const struct Vertex *c);
/**
* Given the head of a counter-clockwise indexed polygon area_poly_2
* calculates the area of the polygon given.
*
* @param head The first vertex.
* @return The area.
*/
int area_poly_2(const struct Vertex *head);
/**
* Determines if vertex c is to the left of the line segment ab. Note the
* area of the shape made by the 3 vertices must have c to the left to have
* positive area.
* @param a Vertex a.
* @param b Vertex b.
* @param c Vertex c.
* @return True if it is to the left, false otherwise.
*/
bool left(const struct Vertex *a, const struct Vertex *b,
const struct Vertex *c);
/**
* Determines if vertex c is to the left or collinear with the line segment ab.
* @param a Vertex a.
* @param b Vertex b.
* @param c Vertex c.
* @return True if it is to the left or collinear, false otherwise.
*/
bool left_on(const struct Vertex *a, const struct Vertex *b,
const struct Vertex *c);
/**
* Determines if the vertex c is collinear with line segment ab.
* @param a Vertex a.
* @param b Vertex b.
* @param c Vertex c.
* @return True if it is collinear with ab, false otherwise.
*/
bool collinear(const struct Vertex *a, const struct Vertex *b,
const struct Vertex *c);
/**
* Determines if the line segments ab and cd are intersecting properly.
* @param a Vertex a.
* @param b Vertex b.
* @param c Vertex c.
* @param d Vertex d.
* @return True if they intersect properly, false otherwise.
*/
bool intersect_prop(const struct Vertex *a, const struct Vertex *b,
const struct Vertex *c, const struct Vertex *d);
/**
* Determines the betweenness of c.
* @param a Vertex a.
* @param b Vertex b.
* @param c Vertex c.
* @return True if c is between ab.
*/
bool between(const struct Vertex *a, const struct Vertex *b,
const struct Vertex *c);
/**
* Determines if two line segments, ab and cd, intersect.
* @param a Vertex a.
* @param b Vertex b.
* @param c Vertex c.
* @param d Vertex d.
* @return True if they intersect, false otherwise.
*/
bool intersect(const struct Vertex *a, const struct Vertex *b,
const struct Vertex *c, const struct Vertex *d);
/**
* Finds a diagonal of line segment ab for the polygon represented by the start
* vertex `head`.
*
* @param head The first vertex of a counter-clockwise labeled polygon.
* @param a Vertex a.
* @param b Vertex b.
* @return True if ab is a valid diagonal, false otherwise.
*/
bool diagonalie(const struct Vertex *head, const struct Vertex *a,
const struct Vertex *b);
/**
* Determines if one vector b lines inside the cone created by vertices a and
* b.
* @param a Vertex a.
* @param b Vertex b.
* @return True if it lies in the cone, false otherwise.
*/
bool in_cone(const struct Vertex *a, const struct Vertex *b);
/**
* Determines if ab is a diagonal in the polygon represented by the
* counter-clockwise ordered vertex list head.
*
* @param head The head of the polygon's vertex list.
* @param a Vertex a.
* @param b Vertex b.
* @return True if ab is a diagonal, false otherwise.
*/
bool diagonal(const struct Vertex *head, const struct Vertex *a,
const struct Vertex *b);
/**
* Triangulates a given polygon and prints the results to screen.
*
* Note this currently destructively modifies the polygon. Each ear
* removed is freed as a result of the algorithm pointing around it in
* the circularly linked list. The book does not talk about memory management
* and on large polygons this is a massive memory leak.
*
* Future versions of the function should operate on copies and/or non
* destructively produce results.
*
* @param head The start vertex of a polygon.
*/
void triangulate(const struct Vertex *head);
#endif // CGC_MATH_H