簡體   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