- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在创建一个程序,它绘制一个多边形,然后用绿色填充该多边形。目前,我编写的算法存在问题,因为它仅填充多边形的上半部分,然后出现 IndexOutOfBounds 错误。
这是我的算法的代码:
public class FillPolygon
{
int left_most_edge, right_most_edge, scan = 0;
double[] xcoord;
double[][] table = new double[4][200]; //2d array containing:
//[0][-] -> ymax, [1][-] -> ymin, [2][-] -> dx, [3][-] -> x
public void initializeTable()
{
int i, j;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 200; j++)
{
table[i][j] = 0;
}//end for
}//end for
}//end initializeTable
public void help(int i)
{
double helpX, helpDX, helpYMax, helpYMin;
for (int j = i - 1; j >= 0; j--)
{
if ((table[1][j] == table[1][j + 1] && table[3][j] > table[3][j + 1]) || table[1][j] > table[1][j + 1])
{
helpYMax = table[0][j];
table[0][j] = table[0][j + 1];
table[0][j + 1] = helpYMax;
helpYMin = table[1][j];
table[1][j] = table[1][j + 1];
table[1][j + 1] = helpYMin;
helpDX = table[2][j];
table[2][j] = table[2][j + 1];
table[2][j + 1] = helpDX;
helpX = table[3][j];
table[3][j] = table[3][j + 1];
table[3][j + 1] = helpX;
}//end if
}//end for
}//end help
public double max (double x, double y)
{ //determines the greater of two values
double max;
if (x > y)
max = x;
else
max = y;
return max;
}//end max
public void edgeInsert(double xStart, double yStart, double xEnd, double yEnd, int number_entered_edges)
{
int j = number_entered_edges - 1; //removing the - 1 removes line on left side
double x;
if (yStart > yEnd)
{
table[0][j] = yStart;
table[1][j] = yEnd;
}//end if
else
{
table[0][j] = yEnd;
table[1][j] = yStart;
}//end else
if (table[1][j] == xStart)
x = xStart;
else
x = xEnd;
if (table[0][j] == yStart)
table[2][j] = -(-(xEnd - xStart) / (yEnd - yStart));
else
table[2][j] = -(xEnd - xStart) / (yEnd - yStart);
table[3][j] = x + table[2][j] / 2;
help(j);
}//end edgeInsert
public void loadTable(int number_vertices, int number_entered_edges,
double[] px, double[] py)
{ //take the x and y coordinats and build an edge table based off of them
int k;
double xStart, yStart, xEnd, yEnd;
xStart = px[number_vertices - 1];
yStart = trunc(py[number_vertices - 1]) + 0.5;
//start off with no edges in edge table
number_entered_edges = 0;
for (k = 0; k < number_vertices; k++)
{
xEnd = px[k];
yEnd = trunc(py[k]) + 0.5;
System.out.println("x: " + xEnd + " y: " + yEnd);
if (yStart == yEnd)
{
xStart = xEnd;
}//end if
else
{
//add edge to edge table
number_entered_edges++;
edgeInsert(xStart, yStart, xEnd, yEnd, number_entered_edges);
yStart = yEnd;
xStart = xEnd;
}//end else
}//end for
scan = (int)trunc(table[1][0]); //start at the top of the polygon
}//end loadTable
public void include(int number_entered_edges)
{ //pushing the right most edge
while ((right_most_edge + 1 < number_entered_edges) && (table[1][right_most_edge + 1] < scan))
{
right_most_edge++;
}//end while
}//end include
public void exclude()
{ //excluding edges that we no longer care about
for (int i = left_most_edge; i <= right_most_edge; i++)
{
if (table[0][i] < scan)
{
left_most_edge++;
for (int j = i; j >= left_most_edge; j--)
{
table[0][j] = table[0][j - 1];
table[2][j] = table[2][j - 1];
table[3][j] = table[3][j - 1];
}//end for
}//end if
}//end for
}//end exclude
public void updateX()
{ //increment x based on dx
for (int i = left_most_edge; i <= right_most_edge; i++)
{
table[3][i] += table[2][i];
}//end for
}//end updateX
public void sortOnX()
{ //sorting x values from least to greatest in edge table
int l = 0;
double t;
xcoord = new double[right_most_edge - left_most_edge + 1];
for (int i = left_most_edge; i <= right_most_edge; i++)
{
xcoord[l] = table[3][i];
for(int j = l - 1; j >= 0; j--)
{
if (xcoord[j] > xcoord[j + 1])
{
t = xcoord[j];
xcoord[j] = xcoord[j + 1];
xcoord[j + 1] = t;
}//end if
}//end for
l++;
}//end for
}//end sortOnX
public void fillScan(Graphics g)
{ //determines the line to be drawn for filling
for (int i = 0; i < xcoord.length; i += 2)
{
drawMyHorizontalLine(g, (int)Math.round(xcoord[i]), scan, (int)Math.round(xcoord[i + 1]));
}//end for
}//end fillScan
public double trunc(double num)
{ //trucates the number passed in to remove any decimal
double rem;
if ((num % 2) == 0)
return num;
else
{
rem = num % 2;
return num - rem;
}//end else
}//end trunc
public void drawMyPolygon(Graphics g)
{ //draws the polygon
g.setColor(Color.RED);
g.drawLine(100, 125, 150, 100); //from (100, 125) to (150, 100)
g.drawLine(150, 100, 250, 200); //from (150, 100) to (250, 200)
g.drawLine(250, 200, 300, 150); //from (250, 200) to (300, 150)
g.drawLine(300, 150, 250, 100); //from (300, 150) to (250, 100)
g.drawLine(250, 100, 150, 200); //from (250, 100) to (150, 200)
g.drawLine(150, 200, 100, 200); //from (150, 200) to (100, 200)
g.drawLine(100, 200, 100, 125); //from (100, 125) to (100, 125)
}//end drawMyPolygon
public void drawMyHorizontalLine(Graphics g, int x1, int y, int x2)
{ //draws the line for filling
g.setColor(Color.GREEN);
g.drawLine(x1, y, x2, y);
}//end drawMyHorizontalLine
public void fillMyPolygon(Graphics g, int number_vertices, int number_entered_edges, double[] px, double[] py)
{ //called methods to deal with edge table and fill the polygon
if (number_entered_edges < 3 || number_entered_edges > 200)
{
System.out.println("Polygon size error");
}//end if
else
{
loadTable(number_vertices, number_entered_edges, px, py);
while (left_most_edge < number_entered_edges)
{
scan++;
exclude();
updateX();
include(number_entered_edges);
sortOnX();
fillScan(g);
}//end while
}//end else
}//end fillMyPolygon
}//end FillPolygon
@Override
public void paint(Graphics g)
{
FillPolygon f = new FillPolygon();
double[] px = new double[7]; //contains all x coord.
double[] py = new double[7]; //contains all y coord.
//populate x coord.
px[0] = 100;
px[1] = 150;
px[2] = 250;
px[3] = 300;
px[4] = 250;
px[5] = 150;
px[6] = 100;
//populate y coord.
py[0] = 125;
py[1] = 100;
py[2] = 200;
py[3] = 150;
py[4] = 100;
py[5] = 200;
py[6] = 200;
f.initializeTable();
f.fillMyPolygon(g, 7, 7, px, py); //begin filling the polygon
f.drawMyPolygon(g); //draw polygon with red outline
}//end paint
我不知道为什么会收到错误或如何停止它。错误发生在我的行上,显示“drawMyHorizontalLine(g, (int)Math.round(xcoord[i]), scan, (int)Math.round(xcoord[i + 1]));”
我非常感谢您的帮助。谢谢。
最佳答案
这个循环是你的问题。您一次将数组遍历两个位置 (i+= 2),但直到 i < xcoord.length 为止。
因此,当 i == (xcoord.length - 1) 时,即 xcoord[i] 是数组的最后一个元素,在调用 drawMyHorizontalLine 的方法中,xcoord[i + 1] 将超出数组的末尾。
public void fillScan(Graphics g)
{ //determines the line to be drawn for filling
for (int i = 0; i < xcoord.length; i += 2)
{
drawMyHorizontalLine(g, (int)Math.round(xcoord[i]), scan, (int)Math.round(xcoord[i + 1]));
}//end for
}//end fillScan
要修复此循环,只需更改为 for(...; i < xcoord.length-1 ;i += 2)。只要数组中还剩下两个元素,就会一次移动两个元素。
这应该可以解决您的越界错误,不确定您的多边形是否只填充了一半。我不太明白你的多边形表示。
关于java - 使用我的多边形填充算法填充多边形会出现越界错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28337647/
我已经实现了 Bentley-Ottmann-algorithm检测多边形-多边形交叉点。这通常非常有效:由于多边形不自相交,因此两个多边形的线段并集中的任何线段相交表明两个多边形相交。 但是如果我看
我在 Silverlight 中有一个多边形(棋盘游戏的十六进制),例如; public class GridHex : IGridShape { ..... public IList
我创建了一个简单的 DirectX 应用程序来渲染一个顶点场。顶点呈现如下(如果从顶部查看): |\|\|\|\| |\|\|\|\| 每个三角形都是这样渲染的: 1 |\ 2 3 这应该意味着多边形
我的代码的某些部分here : var stage = new Kinetic.Stage({ container: "canvas", width: 300,
我正在尝试从 map 数据构造导航网格物体。步骤之一涉及将二进制图像(其中0表示占用空间,1表示自由空间)转换成平面直线图。 我正在尝试找到一种可靠的方法。我目前的想法是使用Canny边缘检测器,然后
我的任务是编写 MATLAB 代码来生成一个由 4 部分组成的 Logo ,如屏幕截图所示。左上角应为黑色,右下角应为白色。另一个程序应随机选择两种颜色。 我采取了以下方法: clear all cl
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
如何向 google.maps.Rectangle 和 google.maps.Polygon 添加标题? title 属性在 RectangleOptions 中不可用.我试过了,但没用(对于 go
我有一个表,用于将表上的段存储为多边形。然后我想获取所有被另一个多边形接触的线段,例如正方形或圆形。图片上:http://img.acianetmedia.com/GJ3 我将灰色小框表示为段和 bi
我正在我的网站上使用 CSS 来制作形状。它在 chrome 中运行良好,但在 mozilla、internet explorer 中打开时,它无法运行。 有人知道怎么解决吗? 这是 fiddle h
我使用 DrawingManager 在 Google map 上绘制圆形和多边形。我尝试使用下面的代码删除圆形/多边形。 selectedShape.setMap(null); 这里selected
我看到了很多如何检测碰撞的教程,但没有看到如何解决它。我正在制作一个自上而下的游戏,玩家具有圆形碰撞形状,而墙壁是各种多边形。 我正在使用 slick2d。我应该做的是,如果玩家与角落碰撞,我会按法线
我对 tkinter 比较陌生,我正在制作一个只使用正方形的游戏。我正在抄写的书只显示三角形。这是代码: # The tkinter launcher (Already complete) from
我在 OpenCV/Python 中工作,我遇到了这个问题。我已经使用 cv2.minAreaRect() 来获取围绕一组点的边界框。是否有任何其他函数/通用算法可以给我内接多边形(点集)的最大矩形?
如果给定一组线段 S ,我们能否设计一种算法来测试集合 S 中的线段是否可以形成多边形,我对它们是否相交多边形不感兴趣,我只想知道我可以测试什么标准, 任何建议 最佳答案 构建一个图形数据结构,其中节
如何从多个地理位置(经度、纬度值)创建多边形地理围栏。此外,如何在 Android 上跟踪用户进入此地理围栏区域或从该区域退出。 最佳答案 地理围栏只是一组形成多边形的纬度/经度点。获得纬度/经度点列
我想要一个 complex image like this在我的申请中。我想让用户点击复杂的多边形(在这种情况下是有边界的国家/地区)并突出显示他们点击的多边形。我有我需要用于该状态的图像。 我怎样才
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
我想在 python tkinter 中移动对象,特别是多边形。问题出在 is_click 函数中。我似乎无法弄清楚如何确定我是否单击了该对象。代码还没有 100% 完成,移动仍然需要完成,但我现在需
我有一个大多边形,我想找到与该多边形相交的要素,但由于多边形太大,我遇到超时异常。 我试图研究 JTS 方法,但不知道如何使用它。 final List coordinates = List.of(n
我是一名优秀的程序员,十分优秀!