Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I am getting an arraylist out of bounds exception: Index 4999, Size 4999 in this method:
for (int y = 0; y < trees.getHeight() - 1; ++y) {
for (int x = 0; x < trees.getWidth() - 1; ++x) {
int c = trees.getRGB(x, y);
Color color = new Color(c);
if (color.getRed() == 0 && color.getGreen() == 255 && color.getBlue() == 0) {
treePosB.add(new Vector2f(x, y));
}
}
}
int randX = 0;
int randZ = 0;
boolean canPlace = false;
for (int i = 0; i < treesAr.size() - 1; ++i) {
randX = randInt(-terrain.getTerrainSize(), terrain.getTerrainSize());
randZ = randInt(-terrain.getTerrainSize(), terrain.getTerrainSize());
treesAr.get(i).setLocalTranslation(randX, terrain.getHeight(new Vector2f(randX, randZ)) - (40 + 1.0f), randZ);
for (int x = 0; x < treePosB.size() - 1; ++i) {
//FOLLOWING LINE CAUSES ERROR
if (treesAr.get(i).getLocalTranslation().x == treePosB.get(x).x && treesAr.get(i).getLocalTranslation().y == treePosB.get(x).y) {
canPlace = true;
continue;
}
}
if (canPlace) {
treeNode.attachChild(treesAr.get(i));
} else {
}
}
Im not sure whats causing this, i have changed the for loops to include a ArrayList.size - 1 to adjust for this, because i knew it would happen, but it still happens. The trees ArrayLIst is created like so:
treesAr = treeGen.generate(am);
Texture tex1 = Main.assetManager.loadTexture("Textures/Terrain/Trees/Trees1.png");
BufferedImage trees = ImageToAwt.convert(tex1.getImage(), false, true, 0);
public ArrayList<Geometry> generate(int amount) {
ArrayList trees = new ArrayList();
Geometry tree = (Geometry) Main.assetManager.loadModel(
"Models and Textures/Landscape/Trees/Pine.obj");
for (int i = 0; i < amount - 1; ++i) {
Geometry treeI = tree.clone();
trees.add(treeI);
}
return trees;
}
What this is doing, is load an image, and check the image for green pixels and creates an arraylist with vecotrs that represent the position of each pixel.
It then loads the trees, and goes through a for loop and checks the trees randomized position. If the tree is within the pixel area, they are added to the scene, otherwise nothing happens.
This allows me to draw an image and draw out where the trees are allowed to spawn.
for (int x = 0; x < treePosB.size() - 1; ++i) {
You mistyped this loop (x -> i). It increments i infinitely until it exceeds the size of the list.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Hi I was wondering if I could get help creating a method to shift a java 2D array to the right
public void filter(PixelImage pi) {
Pixel[][] data = pi.getData(); // get image data
for (int row = 0; row < pi.getHeight(); row++) {
for (int col = 0; col < pi.getWidth(); col++) {
if(col == pi.getWidth()-1){
Pixel lastCol = data[row][pi.getWidth()-1];
data[row][0] = lastCol;
}
else{
Pixel temp1 = data[row][col];
data[row][col+1] = temp1;
}
}
}
// reset data into the PixelImage object pi
pi.setData(data);
}
Here is my code sample that is not working properly, could someone please point me in the right direction?
this might be work.
public void filter(PixelImage pi) {
Pixel[][] data = pi.getData(); // get image data
for (int row = 0; row < pi.getHeight(); row++) {
Pixel lastCol = data[row][pi.getWidth()-1];
for (int col = pi.getWidth() - 1; col > 0; col--) {
data[row][col] = data[row][col-1];
}
data[row][0] = lastCol;
}
// reset data into the PixelImage object pi
pi.setData(data);
}
To shift Right you should start shifting from right side.
Now what you are doing is like:
A B C D E
A->A C D E
A A->A D E
A A A->A E
A A A A->A
But you should do it like:
A B C D E
A B C D E->temp
A B C D->D
A B C->C D
A B->B C D
A->A B C D
temp->E A B C D
For that you need to start you loop from length-1 to 0
This will work kindly check
call method shiftByOnePlace number of times you need to shift
/**
*
* #param matrix
* #param noOfShift
*/
public static void shiftByOnePlace(int[][] matrix) {
// shifting all rows to right by 1 place
for (int i = 0; i < matrix.length; i++) {
int[] js = matrix[i];
int temp = js[js.length - 1];
shift(js);
js[0] = temp;
}
}
public static void shift(int[] a) {
for(int i=a.length-2;i>=0;i--) {
a[i+1]= a[i];
}
}
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
I currently have a problem with creating objects using two-dimensional arrays in Java.
I'ld like to display bricks on a playing board (like in the classic "BreakOut games), therefore I created a class "Stone" for the stone-objects, the constructor of the class is:
public Stone (Position pos, int value){
this.pos = pos;
this.type = type;
}
I also created a 2D-array (int) called "stoneTypes" for the pattern of the Stones, in which I saved a matrix from a JSON file.
I ld' now like to create Stone-objects in my class "Level" by using the values of the stoneTypes-matrix, which currently looks like this (I included an if-condition, so Stone-objects are only created for stoneTypes value >= 1):
private Stone[][] stones = new Stone[25][20];
private int[][] stoneTypes;
JSONReader reader = new JSONReader("res/Level" + levelnr +".json");
stoneTypes = reader.getStones2DArray();
for (int w = 0; w < stoneTypes.length; w++) {
for (int h = 0; h < stoneTypes[w].length; h++) {
if (stoneTypes [w][h] >= 1) {
Position pos = new Position(width * w, height * h);
this.stones[w][h] = new Stone(pos, stoneTypes[w][h]);
}
}
}
I also included a get-Method for the Stone-array, so I could use it to draw the Stones in my "Field" class:
public Stone[][] getStones(){
return this.stones;
}
The method for drawing the Stones in my "Field" class currently looks like this:
private void drawStones(Graphics2D g2) {
stones = view.getGame().getLevel().getStones();
for (int i = 0; i < stones.length; i++) {
for (int j = 0; j < stones[i].length; j++) {
*** int x_position = (int) stones[i][j].getPosition().getX(); ***
int y_position = (int) stones[i][j].getPosition().getY();
g2.fillRoundRect(x_position, y_position,
(int) ((double)Constants.SCREEN_WIDTH/Constants.SQUARES_X)-2,
(int) ((double)Constants.SCREEN_HEIGHT/Constants.SQUARES_Y)-2 ,1,1);
System.out.println(x_position);
}
}
}
Eclipse doesn't show any syntax errors but I do receive a NullPointerException at the spot I marked with the ***, as soon as I start the programm. I am not sure if my get-Method isn't implemented correctly or if the process of creating new Stone-objects is simply wrong. I tried hundreds of things but couldn't find a solution, I hope you guys can help me.
Thx in advance, Scoopa!
Here:
if (stoneTypes [w][h] >= 1) {
Position pos = new Position(width * w, height * h);
this.stones[w][h] = new Stone(pos, stoneTypes[w][h]);
}
You are only creating new stones when that condition is met. All other fields will stay with their default initial value. And that would be: null.
When you do that, you can't just go in (unconditionally) and do:
int x_position = (int) stones[i][j].getPosition().getX()
You would need a
if (stones[i][j] != null)
to guard such accesses!
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
So I tried this program which was supposed to find out what position these doors are in. They start out closed, and then you toggle every door. The next time you toggle every second, then every third, fourth, so on. I got the coding right but it seems really inefficient. If I tried this with larger numbers, then the program would take too long. Is there any way to do this more efficiently? Here is my coding:
public class doors
{
public static void main(String [] args)
{
int x, y = 1;
boolean [] doors = new boolean[100];
for (int a = 0; a < doors.length; a++)
doors[a] = false; //false means closed
do
{
for (x = 0; x < doors.length - 1; x++)
{
if ((x+1) % y == 0)
{
if (doors[x] == true)
doors[x] = false;
else if (doors[x] == false)
doors[x] = true;
}
}
y++;
}while (y <= doors.length);
for (boolean b : doors)
System.out.println(b);
}
}
// Thanks!!!
Let's simplify the code one step at a time.
1) When an array is allocated, all elements are initialized to their default value, which means 0, false, or null. In your case that means false, so no need to loop through and do that.
// before
boolean [] doors = new boolean[100];
for (int a = 0; a < doors.length; a++)
doors[a] = false; //false means closed
// after
boolean[] doors = new boolean[100];
2a) Since the array is not a zero-length array, the do-while loop might as well be a normal while loop (it will execute at least once).
2b) As a normal while loop, with an initializer right before the loop, and the increment at the end of the loop, it is the same as a for loop.
2c) Since y is not used after the loop, it can be declared in the loop.
// before
int x, y = 1;
do
{
//code
y++;
}while (y <= doors.length);
// after
int x;
for (int y = 1; y <= doors.length; y++)
{
//code
}
3a) doors[x] == true is the same as doors[x]. doors[x] == false is the same as ! doors[x].
3b) if (x) {} else if (! x) {} is the same as if (x) {} else {}. The test after else is redundant.
3c) if (x) x = false; else x = true; is the same as x = ! x;.
3d) You can skip the double evaluation by doing ^= true (XOR).
// before
if (doors[x] == true)
doors[x] = false;
else if (doors[x] == false)
doors[x] = true;
// after
doors[x] = ! doors[x];
// skip double evaluation of index lookup
doors[x] ^= true;
4a) (x+1) % y == 0 will only be true for x values of y-1, 2*y-1, 3*y-1, and so on, so make loop start at y-1 and increment by y, eliminating the need for the if statement.
4c) Since x is not used after the loop, it can be declared in the loop.
// before
int x;
for (x = 0; x < doors.length - 1; x++)
{
if ((x+1) % y == 0)
{
//code
}
}
// after
for (int x = y - 1; x < doors.length - 1; x += y)
{
//code
}
Result so far (after also removing unnecessary braces)
boolean[] doors = new boolean[100];
for (int y = 1; y <= doors.length; y++)
for (int x = y - 1; x < doors.length - 1; x += y)
doors[x] ^= true;
for (boolean b : doors)
System.out.println(b);
5a) Limiting x to < doors.length - 1 means that last value of the array will never be used/updated. Drop the - 1.
// before
for (int x = y - 1; x < doors.length - 1; x += y)
// after
for (int x = y - 1; x < doors.length; x += y)
6a) Printing the array with one boolean true/false value per lines is not very human readable. Print is as a binary number on one line.
// before
for (boolean b : doors)
System.out.println(b);
// after
for (boolean b : doors)
System.out.print(b ? '1' : '0');
System.out.println();
7a) Move the printing inside the outer loop, and you'll see an interesting pattern.
boolean[] doors = new boolean[100];
for (int y = 1; y <= doors.length; y++) {
for (int x = y - 1; x < doors.length; x += y)
doors[x] ^= true;
for (boolean b : doors)
System.out.print(b ? '1' : '0');
System.out.println();
}
1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
1010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010
1000111000111000111000111000111000111000111000111000111000111000111000111000111000111000111000111000
1001111100101001111100101001111100101001111100101001111100101001111100101001111100101001111100101001
1001011101101011111000100001101100001000111110101101110100111001011101101011111000100001101100001000
1001001101111011101000110001111100011000101110111101100100101001001101111011101000110001111100011000
1001000101111111101010110000111100111000111110110101100000101011001100111011001000100001110100011100
1001000001111110101010100000111000111001111110100101100100101010001100101011001100100000110100001100
1001000011111110111010100010111000101001111100100101110100101000001100111011001110100000100100001110
1001000010111110111110100010101000101000111100100001110100111000001101111011001010100000110100001111
1001000010011110111111100010101010101000111000100001111100111000011101111011101010100001110100001101
1001000010001110111111110010101010111000111000110001111100101000011101101011101010110001110100011101
1001000010000110111111110110101010111010111000110000111100101000111101101011111010110001111100011101
1001000010000010111111110111101010111010101000110000111000101000111100101011111010100001111100011001
1001000010000000111111110111111010111010101010110000111000111000111100101001111010100001101100011001
1001000010000001111111110111111110111010101010100000111000111001111100101001111110100001101100001001
1001000010000001011111110111111111111010101010100010111000111001111000101001111110101001101100001001
1001000010000001001111110111111111101010101010100010101000111001111000111001111110101001111100001001
1001000010000001000111110111111111101110101010100010101010111001111000111000111110101001111100101001
1001000010000001000011110111111111101111101010100010101010101001111000111000111010101001111100101000
1001000010000001000001110111111111101111111010100010101010101011111000111000111010111001111100101000
1001000010000001000000110111111111101111111110100010101010101011101000111000111010111000111100101000
1001000010000001000000010111111111101111111111100010101010101011101010111000111010111000111000101000
1001000010000001000000000111111111101111111111110010101010101011101010101000111010111000111000111000
1001000010000001000000001111111111101111111111110110101010101011101010101010111010111000111000111001
1001000010000001000000001011111111101111111111110111101010101011101010101010101010111000111000111001
1001000010000001000000001001111111101111111111110111111010101011101010101010101000111000111000111001
1001000010000001000000001000111111101111111111110111111110101011101010101010101000101000111000111001
1001000010000001000000001000011111101111111111110111111111101011101010101010101000101010111000111001
1001000010000001000000001000001111101111111111110111111111111011101010101010101000101010101000111001
1001000010000001000000001000000111101111111111110111111111111111101010101010101000101010101010111001
1001000010000001000000001000000011101111111111110111111111111110101010101010101000101010101010101001
1001000010000001000000001000000001101111111111110111111111111110111010101010101000101010101010101011
1001000010000001000000001000000000101111111111110111111111111110111110101010101000101010101010101011
1001000010000001000000001000000000001111111111110111111111111110111111101010101000101010101010101011
1001000010000001000000001000000000011111111111110111111111111110111111111010101000101010101010101011
1001000010000001000000001000000000010111111111110111111111111110111111111110101000101010101010101011
1001000010000001000000001000000000010011111111110111111111111110111111111111101000101010101010101011
1001000010000001000000001000000000010001111111110111111111111110111111111111111000101010101010101011
1001000010000001000000001000000000010000111111110111111111111110111111111111111100101010101010101011
1001000010000001000000001000000000010000011111110111111111111110111111111111111101101010101010101011
1001000010000001000000001000000000010000001111110111111111111110111111111111111101111010101010101011
1001000010000001000000001000000000010000000111110111111111111110111111111111111101111110101010101011
1001000010000001000000001000000000010000000011110111111111111110111111111111111101111111101010101011
1001000010000001000000001000000000010000000001110111111111111110111111111111111101111111111010101011
1001000010000001000000001000000000010000000000110111111111111110111111111111111101111111111110101011
1001000010000001000000001000000000010000000000010111111111111110111111111111111101111111111111101011
1001000010000001000000001000000000010000000000000111111111111110111111111111111101111111111111111011
1001000010000001000000001000000000010000000000001111111111111110111111111111111101111111111111111111
1001000010000001000000001000000000010000000000001011111111111110111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001001111111111110111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000111111111110111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000011111111110111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000001111111110111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000111111110111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000011111110111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000001111110111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000111110111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000011110111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000001110111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000110111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000010111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000000111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001011111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001001111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000011111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000001111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000011111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000001111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000011111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000001111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000011101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000001101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000001111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000011111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010011111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010001111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000011111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000001111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000011111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000001111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000000111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000000011111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000000001111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000000000111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000000000011110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000000000001110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000000000000110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000000000000010
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000000000000000
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000000000000001
Now, when you look at the pattern of the result (last line), you'll see that your code could be reduced to (very fast):
boolean[] doors = new boolean[100];
for (int idx = 0, incr = 1; idx < doors.length; idx += (incr += 2))
doors[idx] = true;
//print here
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000000000000001
First, the default value in your doors array is false (but you could use Arrays.fill to fill it if you needed to). Next you only need to increment your loop by y. Then you can toggle with doors[x] = !doors[x];. Something like,
int y = 1;
boolean[] doors = new boolean[100];
// Arrays.fill(doors, false);
do {
for (int x = y - 1; x < doors.length; x += y) {
doors[x] = !doors[x];
}
y++;
} while (y <= doors.length);
System.out.println(Arrays.toString(doors));
or using nested for loops like
boolean[] doors = new boolean[100];
// Arrays.fill(doors, false);
for (int y = 1; y < doors.length; y++) {
for (int x = y - 1; x <= doors.length; x += y) {
doors[x] = !doors[x];
}
}
System.out.println(Arrays.toString(doors));
As the guys have mentioned above there is a bunch of things you could do to simplify the code. By doing that you will start to better see the algorithms being used and therefore see where they can be improved.
With code like this, simplifying things like if statements is usually more about clarifying code than improving performance. The reason I say that is compilers these days and very good as optimising code and whilst a few instructions may be dropped the performance difference is likely to be minimal.
Where you are more likely to gain noticeable improvement is where there are loops. Reducing the number of iterations in a loop will often have a dramatic effect on overall time. To that end I can see two possible places you can improve things.
Firstly the initial setup loop may not be required. I haven't checked, but the array of doors may be false by default, so looping through to initialise could possibly be dropped.
Secondly, the inner loop increments by 1 and tests if that door matches y. You can short circuit a lot of this by simply incrementing the inner loop by y. That way you will always be on a door you want to flip. Which means you can drop the test for the correct door, and just flip it. As y increases, the number of iterations of the x loop would decrease. Meaning that it would speed up each time y increased.
Finally when I see code like this I always recommend one thing. Always put brackets on your if statements. Subtle bugs are easy to introduce when brackets are not used.
instead of using for statement , use foreach and switch instead
example
for(boolean i:doors){
//do something
}
I got a project in my Java class which I'm having trouble with.
The project is basically marking coordinates on the screen, making a (complex) polynomial out of them, then solving the polynomial with Newton's method using random guesses and drawing the path of the guesses on the screen.
I don't have a problem with any of the drawing, marking, etc.
But for some reason, my Newton's method algorithm randomly misses roots. Sometimes it hits none of them, sometimes it misses one or two. I've been changing stuff up for hours now but I couldn't really come up with a solution.
When a root is missed, usually the value I get in the array is either converging to infinity or negative infinity (very high numbers)
Any help would be really appreciated.
> // Polynomial evaluation method.
public Complex evalPoly(Complex complexArray[], Complex guess) {
Complex result = new Complex(0, 0);
for (int i = 0; i < complexArray.length; i++) {
result = result.gaussMult(guess).addComplex(complexArray[complexArray.length - i - 1]);
}
return result;
}
> // Polynomial differentation method.
public Complex[] diff(Complex[] comp) {
Complex[] result = new Complex[comp.length - 1];
for (int j = 0; j < result.length; j++) {
result[j] = new Complex(0, 0);
}
for (int i = 0; i < result.length - 1; i++) {
result[i].real = comp[i + 1].real * (i + 1);
result[i].imaginary = comp[i + 1].imaginary * (i + 1);
}
return result;
}
> // Method which eliminates some of the things that I don't want to go into the array
public boolean rootCheck2(Complex[] comps, Complex comp) {
double accLim = 0.01;
if (comp.real == Double.NaN)
return false;
if (comp.real == Double.NEGATIVE_INFINITY || comp.real == Double.POSITIVE_INFINITY)
return false;
if (comp.imaginary == Double.NaN)
return false;
if (comp.imaginary == Double.NEGATIVE_INFINITY || comp.imaginary == Double.POSITIVE_INFINITY)
return false;
for (int i = 0; i < comps.length; i++) {
if (Math.abs(comp.real - comps[i].real) < accLim && Math.abs(comp.imaginary - comps[i].imaginary) < accLim)
return false;
}
return true;
}
> // Method which finds (or attempts) to find all of the roots
public Complex[] addUnique2(Complex[] poly, Bitmap bitmapx, Paint paint, Canvas canvasx) {
Complex[] rootsC = new Complex[poly.length - 1];
int iterCount = 0;
int iteLim = 20000;
for (int i = 0; i < rootsC.length; i++) {
rootsC[i] = new Complex(0, 0);
}
while (iterCount < iteLim && MainActivity.a < rootsC.length) {
double guess = -492 + 984 * rand.nextDouble();
double guess2 = -718 + 1436 * rand.nextDouble();
if (rootCheck2(rootsC, findRoot2(poly, new Complex(guess, guess2), bitmapx, paint, canvasx))) {
rootsC[MainActivity.a] = findRoot2(poly, new Complex(guess, guess2), bitmapx, paint, canvasx);
MainActivity.a = MainActivity.a + 1;
}
iterCount = iterCount + 1;
}
return rootsC;
}
> // Method which finds a single root of the complex polynomial.
public Complex findRoot2(Complex[] comp, Complex guess, Bitmap bitmapx, Paint paint, Canvas canvasx) {
int iterCount = 0;
double accLim = 0.001;
int itLim = 20000;
Complex[] diffedComplex = diff(comp);
while (Math.abs(evalPoly(comp, guess).real) >= accLim && Math.abs(evalPoly(comp, guess).imaginary) >= accLim) {
if (iterCount >= itLim) {
return new Complex(Double.NaN, Double.NaN);
}
if (evalPoly(diffedComplex, guess).real == 0 || evalPoly(diffedComplex, guess).imaginary == 0) {
return new Complex(Double.NaN, Double.NaN);
}
iterCount = iterCount + 1;
guess.real = guess.subtractComplex(evalPoly(comp, guess).divideComplex(evalPoly(diffedComplex, guess))).real;
guess.imaginary = guess.subtractComplex(evalPoly(comp, guess).divideComplex(evalPoly(diffedComplex, guess))).imaginary;
drawCircles((float) guess.real, (float) guess.imaginary, paint, canvasx, bitmapx);
}
return guess;
}
> // Drawing method
void drawCircles(float x, float y, Paint paint, Canvas canvasx, Bitmap bitmapx) {
canvasx.drawCircle(x + 492, shiftBackY(y), 5, paint);
coordPlane.setAdjustViewBounds(false);
coordPlane.setImageBitmap(bitmapx);
}
}
Error 1
The lines
guess.real = guess.subtractComplex(evalPoly(comp, guess).divideComplex(evalPoly(diffedComplex, guess))).real;
guess.imaginary = guess.subtractComplex(evalPoly(comp, guess).divideComplex(evalPoly(diffedComplex, guess))).imaginary;
first introduce a needless complication and second introduce an error that makes it deviate from Newton's method. The guess used in the second line is different from the guess used in the first line since the real part has changed.
Why do you not use, like in the evaluation procedure, the complex assignment in
guess = guess.subtractComplex(evalPoly(comp, guess).divideComplex(evalPoly(diffedComplex, guess)));
Error 2 (Update)
In the computation of the differentiated polynomial, you are missing the highest degree term in
for (int i = 0; i < result.length - 1; i++) {
result[i].real = comp[i + 1].real * (i + 1);
result[i].imaginary = comp[i + 1].imaginary * (i + 1);
It should be either i < result.length or i < comp.length - 1. Using the wrong derivative will of course lead to unpredictable results in the iteration.
On root bounds and initial values
To each polynomial you can assign an outer root bound such as
R = 1+max(abs(c[0:N-1]))/abs(c[N])
Using 3*N points, random or equidistant, on or close to this circle should increase the probability to reach each of the roots.
But the usual way to find all of the roots is to use polynomial deflation, that is, splitting off the linear factors corresponding to the root approximations already found. Then a couple of additional Newton steps using the full polynomial restores maximal accuracy.
Newton fractals
Each root has a basin or domain of attraction with fractal boundaries between the domains. In rebuilding a similar situation to the one used in
I computed a Newton fractal showing that the attraction to two of the roots and ignorance of the other two is a feature of the mathematics behind it, not an error in implementing the Newton method.
Different shades of the same color belong to the domain of the same root where brightness corresponds to the number of steps used to reach the white areas around the roots.
This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 7 years ago.
I am developing a Java game, and am currently writing a map maker. I can make the map and draw tiles, but i need to be able to change the position of those tiles so the character can see different locations of the map. When I try to change it, in the moveMap() method, It gives me this error:
Exception in thread "Thread-2" java.lang.ArrayIndexOutOfBoundsException: 570
at Base.moveMap(Base.java:88)
at Base.run(Base.java:55)
at java.lang.Thread.run(Unknown Source)
I have no idea why this is happening - could someone please help me understand the problem. Is there any alternate way to move the tiles?
Here is my code...
public class Base extends JPanel implements Runnable {
private static String[] line = {
"wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww",
"wwwwwwwwwwwwwwwwfffffffffffwwwwwwwwwww",
"wwwwwwffwwwwwwwwfwwwwwwwwwwwwwwwwwwwww",
"wwwwwwfffffffwwwfwwwwwwwwwwwwwwwwwwwww",
"wwwwwwffwwwffffffwwwwwwwwwwwwwwwwwwwww",
"wwwwwwffwwwffffffwwwwwwwwwwwwwwwwwwwww",
"wwwwwwfffffffwwwwwwwwwwwwwwwwwwwwwwwww",
"wwwwwwffwwwwwwwwwwwwwwwwwwwwwwwwwwwwww",
"wwwwwwffwwwwwwwwwwwwwwffffffffwwwwwwww",
"wwwwwwffwwwwwwwwwwwwwwwwwwwwffwwwwwwww",
"wwwwffffffwwwwwwwwwwwwwwwwwwffwwwwwwww",
"wwwwffffffffffffffffffffffffffwwwwwwww",
"wwwwffffffwwwwwwwwwwwwwwwwwwwwwwwwwwww",
"wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww",
"wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww",};
private Rectangle[] colRect;
private int tileWidth = 30;
private int tileHeight = 30;
public Base() {
colRect = new Rectangle[line.length * line[0].length()];
for (int i = 0; i < line.length; i++) {
for (int f = 0; f < line[i].length(); f++) {
colRect[counter] = new Rectangle(f * tileWidth, i * tileHeight,tileWidth, tileHeight);
if (counter != colRect.length) {
counter += 1;
}
}
}
}
public void moveMap(){
for(int i = 0; i <= colRect.length; i++){
colRect[i].setLocation(colRect[i].x+1, colRect[i].y+1);
}
}
}
You almost certainly mean this:
for (int i = 0; i < colRect.length; i++) {
Instead of this:
for(int i = 0; i <= colRect.length; i++){
Remember that if an array has length n, the indexes go from 0 to n - 1.
I would suggest you just to modify the accessor to your geometry, than the map itself. Because it looks like, the function moveMap could have some kind of "loop" behaviour
getLocation (int index, offset = 0){
int accIndex = (index + offset) % colRect.length;
// ... probably better to modify your data-structure to simplify the handling
}