Added function to get Y value for a point on a line segment

This commit is contained in:
Matt Hill
2015-09-20 21:04:28 -04:00
parent f5ad99b16b
commit 7b59d3a238
3 changed files with 12 additions and 0 deletions

View File

@@ -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);

View File

@@ -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);

View File

@@ -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) );