繁体   English   中英

Java Scanline多边形填充算法

[英]Java Scanline Polygon Fill Algorithm

我刚刚开始学习计算机图形学,对JAVA也是新手。 我的Java小程序执行扫描线算法,在某些情况下效果很好。 谁能帮我知道我到底在哪里错?

您可以在此处查看完整的代码= http://codeshare.io/9uVUf预先感谢。

我在C ++和OpenGL中做了类似的事情。 使用凹面和凸面,不确定是否所有多边形都适用。

我不知道这是否是最佳解决方案,但对我有用。

bool GraphicObject::IsMouseInside(int x, int y)
{
    if (Points.size() < 2)
    {
        return false;
    }

    int count = 0;
    float ti;
    float yint = y;
    float xint = 0;

    for (size_t i = 0; i < Points.size() - 1; i++)
    {
        auto p1 = Points[i + 1];
        auto p2 = Points[i];

        if (p1.x == x && p1.y == y)
        {
            return true;
        }

        ti = 0;

        if ((p2.y - p1.y) != 0)
        {
            ti = (yint - p1.y) / (p2.y - p1.y);
        }

        if (ti > 0 && ti < 1)
        {
            xint = p1.x + (p2.x - p1.x) * ti;

            // HACK: To point (line) are impossible to select.
            // Add an error margin.
            if (Points.size() == 2)
            {
                // TODO: Use euclidean distance for this. Is better?
                if ((xint + 8) > x || (xint - 8) > x)
                {
                    count++;
                }
            }
            else
            {
                if (xint > x)
                {
                    count++;
                }
            }
        }
    }

    // last point with first.
    auto p1 = Points[Points.size() - 1];
    auto p2 = Points[0];

    ti = (yint - p1.y) / (p2.y - p1.y);

    if (ti > 0 && ti < 1)
    {
        xint = p1.x + (p2.x - p1.x) * ti;

        if (xint > x)
        {
            count++;
        }
    }

    return count % 2 != 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM