开发者

creating many triangles with different points

开发者_JS百科I have a question that in this link

you will see pointInTriangle Method with 4 parameters .I want to know that how can I send those 3 last parameters to this method when we have n points? Is there any way to do this at O(n^3)

please help me thanks


Can you use Polygon.contains(Point) instead?


Your question is not completely clear, but assuming you just want to extend this solution to check for n points, I guess you can do something like this:

private static float sign(fPoint p1, fPoint p2, fPoint p3)
{
    return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y);
}

public static boolean[] pointsInTriangle(fPoint[] pt, fPoint v1, fPoint v2, fPoint v3)
{
    boolean b1, b2, b3;

    boolean[] ret = new boolean[pt.length];
    for (int i = 0; i < pt.length; i++)
    {
        b1 = sign(pt[i], v1, v2) < 0.0f;
        b2 = sign(pt[i], v2, v3) < 0.0f;
        b3 = sign(pt[i], v3, v1) < 0.0f;
        ret[i] = ((b1 == b2) && (b2 == b3)); 
    }
    return ret;
}

By the way, this is O(n).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜