| 1 |
#include <stdbool.h> |
| 2 |
#include <math.h> |
| 3 |
|
| 4 |
#include "epsilon.h" |
| 5 |
|
| 6 |
#include "point.h" |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
/* Operations on points with real coordinates. It is not orthogonal, |
| 11 |
but more convenient, to have the subtraction operator return a |
| 12 |
vector, and the addition operator return a point. |
| 13 |
*/ |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
Point |
| 18 |
point_make(float const x, |
| 19 |
float const y, |
| 20 |
float const z) { |
| 21 |
|
| 22 |
Point retval; |
| 23 |
|
| 24 |
retval.x = x; |
| 25 |
retval.y = y; |
| 26 |
retval.z = z; |
| 27 |
|
| 28 |
return retval; |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
bool |
| 34 |
point_equal(Point const comparand, |
| 35 |
Point const comparator) { |
| 36 |
|
| 37 |
return |
| 38 |
epsilon_equal(comparand.x, comparator.x) |
| 39 |
&& |
| 40 |
epsilon_equal(comparand.y, comparator.y) |
| 41 |
&& |
| 42 |
epsilon_equal(comparand.z, comparator.z) |
| 43 |
; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
Point |
| 49 |
point_sum(Point const coord1, |
| 50 |
Point const coord2) { |
| 51 |
|
| 52 |
Point retval; |
| 53 |
|
| 54 |
retval.x = coord1.x + coord2.x; |
| 55 |
retval.y = coord1.y + coord2.y; |
| 56 |
retval.z = coord1.z + coord2.z; |
| 57 |
|
| 58 |
return retval; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
Point |
| 64 |
point_scaled(Point const coord, |
| 65 |
float const r) { |
| 66 |
|
| 67 |
Point retval; |
| 68 |
|
| 69 |
retval.x = coord.x * r; |
| 70 |
retval.y = coord.y * r; |
| 71 |
retval.z = coord.z * r; |
| 72 |
|
| 73 |
return retval; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
float |
| 79 |
point_distance(Point const p1, |
| 80 |
Point const p2) { |
| 81 |
/*---------------------------------------------------------------------------- |
| 82 |
Return the Euclidean distance between 'p1' and 'p2'. |
| 83 |
-----------------------------------------------------------------------------*/ |
| 84 |
float const x = p1.x - p2.x, y = p1.y - p2.y, z = p1.z - p2.z; |
| 85 |
|
| 86 |
return (float) sqrt(SQR(x) + SQR(y) + SQR(z)); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|