gpt4 book ai didi

java - 使用我的多边形填充算法填充多边形会出现越界错误

转载 作者:太空宇宙 更新时间:2023-11-04 06:13:59 25 4
gpt4 key购买 nike

我正在创建一个程序,它绘制一个多边形,然后用绿色填充该多边形。目前,我编写的算法存在问题,因为它仅填充多边形的上半部分,然后出现 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

我不知道为什么会收到错误或如何停止它。错误发生在我的行上,显示“drawMyHorizo​​ntalLine(g, (int)Math.round(xcoord[i]), scan, (int)Math.round(xcoord[i + 1]));”

我非常感谢您的帮助。谢谢。

最佳答案

这个循环是你的问题。您一次将数组遍历两个位置 (i+= 2),但直到 i < xcoord.length 为止。

因此,当 i == (xcoord.length - 1) 时,即 xcoord[i] 是数组的最后一个元素,在调用 drawMyHorizo​​ntalLine 的方法中,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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com