I read how to keep points that are between two points (ie. : that are part of a segment, with some imprecision) here : How can I tell if a point is nearby a certain line?
Thus, I implemented this little algorithm in Java, and my code is (note that the variables' name should be clear for you ! :) ) :
List<Cupple> returned = new ArrayList<>(points_to_test);
for(Cupple c : points_to_test) {
/*if(c == segment_first_point || c == segment_last_point) {
continue;
}*/
if(Math.abs(Math.abs(
(segment_last_point.getNumber(0) - segment_first_point.getNumber(0))
*
(segment_first_point.getNumber(1) - c.getNumber(1))
-
(segment_first_point.getNumber(0) - c.getNumber(0))
*
(segment_last_point.getNumber(1) - segment_first_point.getNumber(1))
)
/
Math.sqrt(
Math.pow((segment_last_point.getNumber(0) - segment_first_point.getNumber(0)), 2)
+
Math.pow((segment_last_point.getNumber(1) - segment_first_point.getNumber(1)), 2)
)
) > maximal_allowed_distance) {
returned.remove(c);
}
}
return returned;
To be sure you understand :
returned
is the list with points that are on the segment, or near the segment (and the "imprecision" / maximal distance that determine if a point is out of the segment is the variable :maximal_allowed_distance
)points_to_test
are ALL the points that are present in my graph : the both of my segment + the points that are really on the segment + the points that are almost on the segment (<=maximal_allowed_distance
) + the points that are far from the segment (>maximal_allowed_distance
). The idea of my little algorithm is that I remove all the latter.segment_[first|last]_point
are the two segment's extremitiesc
is the current point ofpoints_to_test
and I want to know if it is far from the segment or in (according to themaximal_allowed_distance
)getNumber(0)
returns the X coordinate of the point,getNumber(1)
returns the Y one.
However, it does not work. It doesn't return the good points (ie. : the points that are in the segment, taking account of maximal_allowed_distance
).
Do you know if I misunderstood the answer I gave you in the first line of this question ? Do you see any mistake in my own implementation of this algorithm ?