我刚开始学习计算机图形学,也是JAVA的新手。这是我的java applet,它执行扫描线算法,适用于某些情况。任何人都可以帮助我知道我在哪里错了吗?
您可以在此处查看整个代码= http://codeshare.io/9uVUf 提前谢谢。
答案 0 :(得分:0)
我在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;
}