Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if a point is inside a triangle formed by 3 points with given latitude/longitude

I have 3 points ( lat , lon ) that form a triangle.How can i find if a point is inside this triangle?

like image 533
thikonom Avatar asked Mar 17 '10 18:03

thikonom


1 Answers

Java Code for just triangle , that is 3 points.

    public static boolean pntInTriangle(double px, double py, double x1, double y1, double x2, double y2, double x3, double y3) {

    double o1 = getOrientationResult(x1, y1, x2, y2, px, py);
    double o2 = getOrientationResult(x2, y2, x3, y3, px, py);
    double o3 = getOrientationResult(x3, y3, x1, y1, px, py);

    return (o1 == o2) && (o2 == o3);
}

private static int getOrientationResult(double x1, double y1, double x2, double y2, double px, double py) {
    double orientation = ((x2 - x1) * (py - y1)) - ((px - x1) * (y2 - y1));
    if (orientation > 0) {
        return 1;
    }
    else if (orientation < 0) {
        return -1;
    }
    else {
        return 0;
    }
}
like image 110
Eric Avatar answered Sep 24 '22 19:09

Eric