forked from asayler/CU-CSCI3308-CIPractice
-
Notifications
You must be signed in to change notification settings - Fork 709
/
geometry.c
71 lines (56 loc) · 1.42 KB
/
geometry.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* geometry.c
* Andy Sayler
* CSCI 3308
* Summer 2014
*
* This file contains a simple geomtery functions.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <errno.h>
#include "geometry.h"
#define FUZZY_EQ 0.01
#define DEBUG(file, line, func, msg) fprintf(stderr, "DEBUG - %s_%d_%s: %s", file, line, func, msg);
double coord_2d_dist(const coord_2d_t* a, const coord_2d_t* b){
/* Input Checks */
if(!a){
DEBUG(__FILE__, __LINE__, __func__, "'a' must not be NULL");
return NAN;
}
if(!b){
DEBUG(__FILE__, __LINE__, __func__, "'b' must not be NULL");
return NAN;
}
/* Maths */
return sqrt(pow((a->x - b->x), 2) + pow((a->y - b->y), 2));
}
bool coord_2d_eq(const coord_2d_t* a, const coord_2d_t* b){
/* Equal if dist <= FUZZY_EQ */
if(coord_2d_dist(a, b) <= FUZZY_EQ){
return true;
}
else{
return false;
}
}
void coord_2d_midpoint(coord_2d_t* mid, const coord_2d_t* a, const coord_2d_t* b){
/* Input Checks */
if(!mid){
DEBUG(__FILE__, __LINE__, __func__, "'mid' must not be NULL");
return;
}
if(!a){
DEBUG(__FILE__, __LINE__, __func__, "'a' must not be NULL");
return;
}
if(!b){
DEBUG(__FILE__, __LINE__, __func__, "'b' must not be NULL");
return;
}
/* Maths */
mid->x = ((a->x + b->x) / 2.0 );
mid->y = ((a->y + b->y) / 2.0 );
}