// The method iIsInside has four parameters of type Position. // The first three are those for the vertices of the triangle. // The last parameter is for the point. // It returns true if and only if the point is inside the triangle. private static boolean iIsInside(final Position pPosition1, final Position pPosition2, final Position pPosition3, final Position pPoint) { final double x1 = pPosition1.getLongitude().getDistance(); final double y1 = pPosition1.getLatitude().getDistance(); final double x2 = pPosition2.getLongitude().getDistance(); final double y2 = pPosition2.getLatitude().getDistance(); final double x3 = pPosition3.getLongitude().getDistance(); final double y3 = pPosition3.getLatitude().getDistance(); final double x0 = pPoint.getLongitude().getDistance(); final double y0 = pPoint.getLatitude().getDistance(); // thanks to Horst Kraemer for the clever stuff from here on final double d = (x2-x1)*(y3-y1) - (y2-y1)*(x3-x1); final double a = (x0-x1)*(y3-y1) - (y0-y1)*(x3-x1); final double b = (x2-x1)*(y0-y1) - (y2-y1)*(x0-x1); if (d==0) { return true; } return 0<=a/d && 0<=b/d && a/d+b/d<=1; }