Reputation: 1660
i read various related answers. In particular here (How do you detect where two line segments intersect?) it is greatly explained how to find the intersection between two segments and to check for parallelism and if they belong to the same line. I wrote a fortran program following that great idea. The problem now consists of finding the union segment when the two segments belong to the same line. Here I found a C++ code (Detecting coincident subset of two coincident line segments) but it is not explained and I cant read C++ but only Fortran (here is a useful image depiction the problem, posted in another question but with no useful answer http://judark.myweb.hinet.net/parallel.JPG ). What is the best language-agnostic algorithm to find the locus of common points (i.e. the union segment, i.e. the two points defining this union) of two segments belonging to the same line? I have managed to do it with planty of "if" computing all the manhattan distances between the points (http://en.wikipedia.org/wiki/Taxicab_geometry) but I was wondering if there is a better way to do it. Thanks A.
Upvotes: 1
Views: 641
Reputation: 60988
If the two segments are on the same line and do overlap, then the union is simply the segment between those two of the four end points which are farthest apart from one another. So simply compute all squared distances (no need to compute square roots) and identify the pair with maximal distance. This approach handles many degenerate cases nicely, including the case where all 4 points coincide and the union of two equal points is simply that point.
Upvotes: 0