From 804f3bce8ad4dfd48186f0030252067f69b62e07 Mon Sep 17 00:00:00 2001 From: erdgeist Date: Sun, 9 Apr 2017 22:08:28 +0200 Subject: Add function to find a point for a certain offset --- geometry.c | 9 +++++++++ geometry.h | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/geometry.c b/geometry.c index f2961c7..7c29f85 100644 --- a/geometry.c +++ b/geometry.c @@ -29,3 +29,12 @@ int dist_pp(LPoint const * p0, LPoint const * p1) { return impl_dist_pp(p0->x,p0->y,p1->x,p1->y); } + +int +get_x_for_y(LPoint const * p0, LPoint const * p1, int y) { + if (p1->x!=p0->x && p1->y!=p0->y) { + double m = (double)(p1->y-p0->y) / (double)(p1->x-p0->x); + return (int)((((double)y) - (double)(p0->y) + m * (double)p0->x)/m); + } + return p1->x; +} diff --git a/geometry.h b/geometry.h index 509ec7e..049fba4 100644 --- a/geometry.h +++ b/geometry.h @@ -15,4 +15,4 @@ typedef struct { // range 0..65536 (but can extend, if normale hits line outside line segment) int dist_pl(LPoint const * p, LLine const * l, int * offs); int dist_pp(LPoint const * p0, LPoint const * p1); - +int get_x_for_y(LPoint const * p0, LPoint const * p1, int y); -- cgit v1.2.3