Я пытаюсь сделать модернистское искусство рекурсией. Я написал код, но каждый раз, когда он запускается, он показывает ошибку StackOverflow. Я проверил так много раз, чтобы найти, где я не прав. У меня также есть базовый случай, чтобы остановить рекурсию, но все равно он показывает ошибку. пожалуйста, помогите мне с этим.
это мой код.
Трассировка стека
Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at java.base/java.util.Random.nextInt(Random.java:390)
at Rectangle.recursionRect(Rectangle.java:138)
at Rectangle.split_Vertically(Rectangle.java:101)
at Rectangle.recursionRect(Rectangle.java:149)
/**
*
*/
public int getRandomPoint(Random R)
{
return (R.nextInt((MAX_RANGE_POINT - MIN_RANGE_POINT) + 1) + MIN_RANGE_POINT)/100.0;
}
/**
* this method will split rectangle horizentally
*/
public void split_Horizentally(Graphics g,int start,int end,int width,int height)
{
Random R = new Random();
int Height_Split_Point = getRandomPoint(R); // this will get random split point on height
int left_width = Height_Split_Point * width;
int right_width = width - left_width;
g.setColor(c);
g.fillRect(start,end,left_width,height);
g.fillRect(start + left_width,end,right_width,height);
recursionRect(g,start,end,left_width,height);
recursionRect(g,start + left_width,end,right_width,height);
}
/**
* this method will split rectangle vertically
*/
public void split_Vertically(Graphics g,int start,int end,int width,int height)
{
Random R = new Random();
int Width_Split_Point = getRandomPoint(R); // this will get random point on width
int top_height = Width_Split_Point * height;
int bottom_height = height - top_height;
g.setColor(c);
g.fillRect(start,end,width,top_height);
g.fillRect(start,end + top_height,width,bottom_height);
recursionRect(g,start,end,width,top_height);
recursionRect(g,start,end + top_height,width,bottom_height);
}
/**
* this method will draw rectangles recursively
*/
public void recursionRect(Graphics g,int start,int end,int width,int height)
{
Random R = new Random();
int Height_Split_Point = R.nextInt((MINIMUM_REGION_LENGTH - ((3/2)*width)) + (int)((3/2)*width));
int Width_Split_Point = R.nextInt((MINIMUM_REGION_LENGTH - ((3/2)*height)) + (int)((3/2)*height));
if(width>WIDTH/2 && height>HEIGHT/2)
{
splitBoth(g,start,end,width,height);
}
else if(width>WIDTH/2)
{
split_Vertically(g,start,end,width,height);
// it shows error ^ here.
}
else if(height>HEIGHT/2)
{
split_Horizentally(g,start,end,width,height);
// ^ error here
}
else if(Height_Split_Point <width )
{
split_Vertically(g,start,end,width,height);
// ^ error here.
}
else if(Width_Split_Point<height)
{
split_Horizentally(g,start,end,width,height);
}
else
{
g.fillRect(start,end,width,height);
}
}