diff options
author | erdgeist <erdgeist@bauklotz.fritz.box> | 2017-04-08 14:21:36 +0200 |
---|---|---|
committer | erdgeist <erdgeist@bauklotz.fritz.box> | 2017-04-08 14:21:36 +0200 |
commit | f2683a4b707cd714b7f540ebf6482563df83d51e (patch) | |
tree | de4941add99f0eb1642aa57c6af180b4ee70119a /geometry.c | |
parent | 78d309a97b782bd6ab2716fa7595bb3f409479e3 (diff) |
Near complete rewrite.
Diffstat (limited to 'geometry.c')
-rw-r--r-- | geometry.c | 31 |
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) | ||
6 | static inline int | ||
7 | impl_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 | |||
17 | int | ||
18 | dist_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 | |||
23 | static inline int | ||
24 | impl_dist_pp(int x0, int y0, int x1, int y1 ) { | ||
25 | return (y0-y1)*(y0-y1)+(x0-x1)*(x0-x1); | ||
26 | } | ||
27 | |||
28 | int | ||
29 | dist_pp(LPoint const * p0, LPoint const * p1) { | ||
30 | return impl_dist_pp(p0->x,p0->y,p1->x,p1->y); | ||
31 | } | ||