From 7b59d3a238a484e6f302043c87f77f1f268234ae Mon Sep 17 00:00:00 2001 From: Matt Hill Date: Sun, 20 Sep 2015 21:04:28 -0400 Subject: [PATCH] Added function to get Y value for a point on a line segment --- src/openalpr/utility.cpp | 6 ++++++ src/openalpr/utility.h | 1 + src/tests/test_utility.cpp | 5 +++++ 3 files changed, 12 insertions(+) diff --git a/src/openalpr/utility.cpp b/src/openalpr/utility.cpp index ac370cf..2e5f264 100644 --- a/src/openalpr/utility.cpp +++ b/src/openalpr/utility.cpp @@ -439,6 +439,12 @@ int levenshteinDistance (const std::string &s1, const std::string &s2, int max) return slope * (x - p2.x) + p2.y; } + float LineSegment::getXPointAt(float y) + { + float y_intercept = getPointAt(0); + return (y - y_intercept) / slope; + } + Point LineSegment::closestPointOnSegmentTo(Point p) { float top = (p.x - p1.x) * (p2.x - p1.x) + (p.y - p1.y)*(p2.y - p1.y); diff --git a/src/openalpr/utility.h b/src/openalpr/utility.h index b2743b7..a94a9ac 100644 --- a/src/openalpr/utility.h +++ b/src/openalpr/utility.h @@ -55,6 +55,7 @@ namespace alpr bool isPointBelowLine(cv::Point tp); float getPointAt(float x); + float getXPointAt(float y); cv::Point closestPointOnSegmentTo(cv::Point p); diff --git a/src/tests/test_utility.cpp b/src/tests/test_utility.cpp index 9e7977b..5b2a052 100644 --- a/src/tests/test_utility.cpp +++ b/src/tests/test_utility.cpp @@ -26,6 +26,11 @@ TEST_CASE( "LineSegment Test", "[2d primitives]" ) { REQUIRE( flat_horizontal.midpoint().x == 11 ); REQUIRE( flat_horizontal.getPointAt(11) == 1 ); + LineSegment rising_45(1,1, 5,5); + + REQUIRE( rising_45.getPointAt(3) == 3 ); + REQUIRE( rising_45.getXPointAt(3) == 3 ); + // Test distance between points calculation REQUIRE( distanceBetweenPoints(Point(10,10), Point(20,20)) == Approx(14.1421) ); REQUIRE( distanceBetweenPoints(Point(-5,10), Point(20,-12)) == Approx(33.3017) );