forked from lizboese/CU-CSCI3308-CIPractice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geometry.c
90 lines (67 loc) · 1.73 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* 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 );
}
/*
*/
double coord_2d_area_triangle(const coord_2d_t* a, const
coord_2d_t* b, const coord_2d_t* c){
if((a->x < 0)||(a->y<0)||(b->x<0)||(b->y<0)||(c->x<0)||(b->y<0)){
return -1;
}
double val = ((a->x)*(b->y - c->y) + (b->x)*(c->y- a->y)+(c->x)*(a->y -b->y))/2;
val = fabs(val);
return val;
}