Hi Vishun, definitely I would choose the approach of the slope. If they are collinear, the slope between any of the coords will result in the same value.
>From a quick look at your code I see that you round the values. Why are you doing this? You definitely will loose precision and come up with different slope values. Cheers, Andrea On Wed, Jun 5, 2013 at 10:38 AM, vishnu soman <vishn...@gmail.com> wrote: > Hi All, > > I am sure that my questions is a very generic one , probably a pure java > problem. However i have been trying to find a way to identify whether three > coordinates are collinear using the same logic of finding it doesnt seem to > work on examples which take 'Points' as inputs. > Two approaches can be > 1. Find the area of the triangle forming the three coordinates/points . If > they are on the same line; the value of area must be zero. > 2. Split the line joining these coordinates into two and find the individual > slopes for the same. If they are on the same line, the slopes will be same. > > The following is the method i am trying. > > > private > > boolean collinearCheck( Coordinate endPointOne , Coordinate > intersection,Coordinate endPointTwo ){ > boolean isCollenear = false; > > //Area of triangle approach > > double area = (Math.round(endPointOne.x )* (Math.round(intersection.y) - > Math.round(endPointTwo.y)) + Math.round(intersection.x )* > (Math.round(endPointTwo.y) - Math.round(endPointOne.y)) + > > Math.round(endPointTwo.x) * (Math.round(endPointOne.y) - > Math.round(intersection.y))); > > if((endPointOne.x * (intersection.y - endPointTwo.y) + intersection.x * > (endPointTwo.y - endPointOne.y) + > > endPointTwo.x * (endPointOne.y - intersection.y))<= 0) > > if(Math.round(area) <= 0) > > { > > isCollenear = true; > > } > > > > // Slope Approach > > double numeratorOne = Math.round(intersection.y) - > Math.round(endPointOne.y); > > double denominatorOne = Math.round(intersection.x) - > Math.round(endPointOne.x); > > double numeratorTwo = Math.round(endPointTwo.y) - > Math.round(intersection.y); > > double denominatorTwo = Math.round(endPointTwo.x) - > Math.round(intersection.x); > > double result1 = Math.round(numeratorOne/denominatorOne); > > double result2 = Math.round(numeratorTwo/denominatorTwo); > > if(result1== 0 && result2==0){ > > isCollenear = true; > > } > > return isCollenear; > > } > > In both these cases while using coordinates as inputs ; even for simingly > collinear situations i end up getting values like 4 etc for the area. And > for situations that are clearly non collinear; i end up getting the same > value for slope. > > Is there a way I can obtain the clear notifier of collinearity using any > udig construct? Is my approach correct? > The sample value for coordinates i pass to the method are > > Coordinate endPointOne = -26.666666666666686, 32.38095238095238 .... etc > > > Looking forward for your inputs. > > > Thanks & Regards > VS > > _______________________________________________ > User-friendly Desktop Internet GIS (uDig) > http://udig.refractions.net > http://lists.refractions.net/mailman/listinfo/udig-devel > _______________________________________________ User-friendly Desktop Internet GIS (uDig) http://udig.refractions.net http://lists.refractions.net/mailman/listinfo/udig-devel