summaryrefslogtreecommitdiff
path: root/geometry.c
diff options
context:
space:
mode:
authorerdgeist <erdgeist@bauklotz.fritz.box>2017-04-08 14:21:36 +0200
committererdgeist <erdgeist@bauklotz.fritz.box>2017-04-08 14:21:36 +0200
commitf2683a4b707cd714b7f540ebf6482563df83d51e (patch)
treede4941add99f0eb1642aa57c6af180b4ee70119a /geometry.c
parent78d309a97b782bd6ab2716fa7595bb3f409479e3 (diff)
Near complete rewrite.
Diffstat (limited to 'geometry.c')
-rw-r--r--geometry.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/geometry.c b/geometry.c
new file mode 100644
index 0000000..f2961c7
--- /dev/null
+++ b/geometry.c
@@ -0,0 +1,31 @@
1#include "geometry.h"
2
3// dist is a fixed point with precission of 8 bits
4// offs is where on the line segment xy0-xy1 the point's normale hits,
5// range 0..65536 (but can extend, if normale hits line outside line segment)
6static inline int
7impl_dist_pl(int xp, int yp, int x0, int y0, int x1, int y1, int *offs)
8{
9 double r = (y1 - y0) * (y1 - y0) + (x1 - x0) * (x1 - x0);
10 double q1 = (xp - x0) * (y1 - y0) - (yp - y0) * (x1 - x0);
11 double q2 = (x1 - x0) * (xp - x0) + (y1 - y0) * (yp - y0);
12
13 *offs = (int)((q2 *65336.0f) / r);
14 return (int)( q1 * q1 * ((double)(y0 - y1) * (double)(y0 - y1) + (double)(x1 - x0) * (double)(x1 - x0)) * 256.0f / (r * r));
15}
16
17int
18dist_pl(LPoint const * p, LLine const * l, int *offs)
19{
20 return impl_dist_pl(p->x, p->y, l->p0.x, l->p0.y, l->p1.x, l->p1.y, offs);
21}
22
23static inline int
24impl_dist_pp(int x0, int y0, int x1, int y1 ) {
25 return (y0-y1)*(y0-y1)+(x0-x1)*(x0-x1);
26}
27
28int
29dist_pp(LPoint const * p0, LPoint const * p1) {
30 return impl_dist_pp(p0->x,p0->y,p1->x,p1->y);
31}