Joehot200
Joehot200

Reputation: 49

Checking 2 arrays to see if they have the same values?

I would like to compare two arrays to see if they have the same values.

If I have a array called

public static float data[][]

which holds Y coordinates of a terrain, how can I check that array with another

public static int coords[][]

without iterating through all the coordinates?

Both arrays have over 1000 values in them. Iterating through them is not an option, since I must iterate through them over four times per second.

I am doing this to attempt to find if two objects are colliding. I have attempted using libraries for this, however I cannot find per-coordinate collision detection as specific as I need it.

Edit: Why I am unable to just iterate through this small amount of vertices is this. The problem is, this is a MultiPlayer game,and I would have to iterate through all 1000 coordinates for every player. Meaning that just 10 players online is 10,000 100 online is 100,000. You can see how easily that would lag or at least take up a large percentage of the CPU.

Input of coordinates into the "Data" variable:

try {
        // Load the heightmap-image from its resource file

        BufferedImage heightmapImage = ImageIO.read(new File(
                "res/images/heightmap.bmp"));
        //width = heightmapImage.getWidth();
        //height = heightmapImage.getHeight();
        BufferedImage heightmapColour = ImageIO.read(new File(
                "res/images/colours.bmp"));
        // Initialise the data array, which holds the heights of the
        // heightmap-vertices, with the correct dimensions
        data = new float[heightmapImage.getWidth()][heightmapImage
                .getHeight()];
        // collide = new int[heightmapImage.getWidth()][50][heightmapImage.getHeight()];
        red = new float[heightmapColour.getWidth()][heightmapColour
                .getHeight()];
        blue = new float[heightmapColour.getWidth()][heightmapColour
                .getHeight()];
        green = new float[heightmapColour.getWidth()][heightmapColour
                .getHeight()];
        // Lazily initialise the convenience class for extracting the
        // separate red, green, blue, or alpha channels
        // an int in the default RGB color model and default sRGB
        // colourspace.
        Color colour;
        Color colours;
        // Iterate over the pixels in the image on the x-axis
        for (int z = 0; z < data.length; z++) {
            // Iterate over the pixels in the image on the y-axis
            for (int x = 0; x < data[z].length; x++) {
                colour = new Color(heightmapImage.getRGB(z, x));

                data[z][x] = setHeight;

            }
        }
    }catch (Exception e){
        e.printStackTrace();
        System.exit(1);
    }

And how coordinates are put into the "coords" variable (Oh wait, it was called "Ship", not coords. I forgot that):

try{
        File f = new File("res/images/coords.txt");
        String coords = readTextFile(f.getAbsolutePath());

        for (int i = 0; i < coords.length();){
            int i1 = i;
            for (; i1 < coords.length(); i1++){
                if (String.valueOf(coords.charAt(i1)).contains(",")){
                    break;
                }
            }
            String x = coords.substring(i, i1).replace(",", "");
            i = i1;
            i1 = i + 1;
            for (; i1 < coords.length(); i1++){
                if (String.valueOf(coords.charAt(i1)).contains(",")){
                    break;
                }
            }
            String y = coords.substring(i, i1).replace(",", "");;
            i = i1;
            i1 = i + 1;
            for (; i1 < coords.length(); i1++){
                if (String.valueOf(coords.charAt(i1)).contains(",")){
                    break;
                }
            }
            String z = coords.substring(i, i1).replace(",", "");;
            i = i1 + 1;
                //buildx.append(String.valueOf(coords.charAt(i)));
                ////System.out.println(x);
                ////System.out.println(y);
                ////System.out.println(z);
                //x = String.valueOf((int)Double.parseDouble(x));
                //y = String.valueOf((int)Double.parseDouble(y));
                //z = String.valueOf((int)Double.parseDouble(z));
            double sx = Double.valueOf(x);
            double sy =  Double.valueOf(y);
            double sz = Double.valueOf(z);
            javax.vecmath.Vector3f cor = new javax.vecmath.Vector3f(Float.parseFloat(x), Float.parseFloat(y), Float.parseFloat(z));
            //if (!arr.contains(cor)){
            if (cor.y > 0)
                arr.add(new javax.vecmath.Vector3f(cor));


            if (!ship.contains(new Vector3f((int) sx, (int) sy, (int) sz)))
                ship.add(new Vector3f((int) sx, (int) sy, (int) sz));

// arr.add(new javax.vecmath.Vector3f(Float.parseFloat(x), Float.parseFloat(y), Float.parseFloat(z))); } Thanks!

Upvotes: 1

Views: 226

Answers (3)

Prabhakaran Ramaswamy
Prabhakaran Ramaswamy

Reputation: 26094

You can do like this but only applicable for same data type.

 Arrays.deepEquals(data, coords);

For single dimension Array you can use this

 Arrays.equals(array1, array1);

Upvotes: 1

BaN3
BaN3

Reputation: 435

Arrays.deepEquals(a, b);

Try this but this will work only if the elements are in order.

Upvotes: 1

schmop
schmop

Reputation: 1440

No way around it, I'm afraid. Comparing data sets to see if they are identical demands looking at all elements, by definition. On a side note, comparing 1000 values is nothing even on relatively old hardware. You can do it thousands of time per second.

Upvotes: 0

Related Questions