I have a method that takes in a rectangle shape as a parameter, uses it as a selection box, and finds shapes in an array called shapeObjects that are within the bounds of the selection box. The indexes of those shapes are then added into a new array which is then returned.
// Searches for shapes that matches the selection's bounding area
// and returns their indexes in the shapeObjects array
public int[] findSelectedShapes( Shape selection ) {
// Integer array for return
int shapeIndexes[] = new int[100];
int numMatching = 0;
// Refers current shape object being checked
int obj = 0;
// Shape coordinates
int x1;
int x2;
int y1;
int y2;
// Iterate checking as long as the object index is less than that of the shapeObjects array length
while( obj < numOfShapes ) {
// Actual object
Shape shape = shapeObjects[obj];
// Coordinates of the shape
x1 = shape.getX1();
x2 = shape.getX2();
y1 = shape.getY1();
y2 = shape.getY2();
// Range between x2 and x1, and y2 and y1 of the selection
for( int i = selection.getX2(); i >= selection.getX1(); i-- ) {
for( int j = selection.getY2(); j >= selection.getY2(); j-- ) {
// Check the shape's coordinates
// If any of the points match
if( ( x1 == i) || ( x2 == i) || (y1 == j) || (y2 == j) ) {
// Add shape index to the array
shapeIndexes[ numMatching ] = obj;
numMatching++;
}
}
}
obj++; // Increase the counter
}
// Update screen
repaint();
// Array with size of matching
int ret[] = new int[ numMatching ];
ret = shapeIndexes;
// Return the selected shapes' indexes
return ret;
}
When I print out the indexes it has found however, I get a million zeroes around a few numbers. Few things I want to do:
1) Get rid of the extra zeroes. I thought that creating a new array with a new size would work, but apparently not.
2) Make it find the right indexes. I currently can't find the problem in this algorithm that is making it find random shapes.
Thanks for any help.