简体   繁体   中英

how can I navigate through an array, check all the elements between them and if they meet a criteria display them?

Okay so the problem is simple but I don't understand how to do it. I tried to check 2 elements, one by one but this isnt the best way. I have an array of Circle objects. The array has position fields, X and Y. I want to go through the array and check all the elements if they are on a line(horizontal or vertical). How can I go through the array and check if the center of the circles are on the same line.

I tried to do something like this to see if the circles are on the same vertical line but how I said it takes one element with the neighbour.

void isCenterSameHorizOrVert(Circle[] c) {
    for(int i = 0; i < c.length; i++) {
        for(int j = i + 1; j < c.length - 1; j++) {
            if(c[i].getPlanarX() == c[j].getPlanarX()) {

            }
        }
    }
}

Here is my approach:

package amedv;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.junit.Test;

class Circle {
    int positionX;
    int positionY;

    public Circle(int positionX, int positionY) {
        super();
        this.positionX = positionX;
        this.positionY = positionY;
    }
    public int getPositionX() {
        return positionX;
    }
    public void setPositionX(int positionX) {
        this.positionX = positionX;
    }
    public int getPositionY() {
        return positionY;
    }
    public void setPositionY(int positionY) {
        this.positionY = positionY;
    }
}

class CircleHM {
    HashMap hmCirlesX;
    HashMap hmCirlesY;

    public HashMap getHmCirlesX() {
        return hmCirlesX;
    }
    public void setHmCirlesX(HashMap hmCirlesX) {
        this.hmCirlesX = hmCirlesX;
    }
    public HashMap getHmCirlesY() {
        return hmCirlesY;
    }
    public void setHmCirlesY(HashMap hmCirlesY) {
        this.hmCirlesY = hmCirlesY;
    }

}

public class CircleTest {

    private Circle[] circles = new Circle[] {new Circle(1,2), new Circle(1,5), new Circle(6,7), new Circle(2,7), new Circle(3,8), new Circle(3,5)};

    private HashMap<Integer, HashMap<Integer, ArrayList<Integer>>> hmCirlesX = new HashMap<Integer, HashMap<Integer, ArrayList<Integer>>>();
    private HashMap<Integer, HashMap<Integer, ArrayList<Integer>>> hmCirlesY = new HashMap<Integer, HashMap<Integer, ArrayList<Integer>>>();

    private boolean findCircleWithCenterSameHorizOrVert(Circle[] cirArr) {
        if (cirArr.length == 0) {
            return false;
        }

        int hashCode = cirArr.hashCode();

        if (!hmCirlesX.containsKey(hashCode) && !hmCirlesY.containsKey(hashCode)) {
            hmCirlesX.put(hashCode, new HashMap<Integer, ArrayList<Integer>>());
            hmCirlesY.put(hashCode, new HashMap<Integer, ArrayList<Integer>>());
        for (int i=0; i<circles.length; i++) {      
            ArrayList arrlCirclesX = hmCirlesX.get(hashCode).containsKey(circles[i].positionX) ? hmCirlesX.get(hashCode).get(circles[i].positionX) : new ArrayList();
            arrlCirclesX.add(circles[i]);
            hmCirlesX.get(hashCode).put(circles[i].positionX, arrlCirclesX);

            ArrayList arrlCirclesY = hmCirlesY.get(hashCode).containsKey(circles[i].positionY) ? hmCirlesY.get(hashCode).get(circles[i].positionY) : new ArrayList();
            arrlCirclesY.add(circles[i]);
            hmCirlesY.get(hashCode).put(circles[i].positionY, arrlCirclesY);
        }
        }

        return (hmCirlesX.size() > 0 && hmCirlesY.size() > 0);      
    }

    private void showCenterSameHorizOrVert(Circle[] cirArr) {
        if (findCircleWithCenterSameHorizOrVert(cirArr)) {
            int hashCode = cirArr.hashCode();

            HashMap<Integer, ArrayList<Integer>> hmX = hmCirlesX.get(hashCode);
            HashMap<Integer, ArrayList<Integer>> hmY = hmCirlesY.get(hashCode);

            Iterator itmX = hmX.entrySet().iterator();
        while (itmX.hasNext()) {
            Map.Entry hmArrListX = (Map.Entry)itmX.next();
            ArrayList cirX = (ArrayList) hmArrListX.getValue();
            System.out.println("Circles on the same X = " + hmArrListX.getKey());
            for (int i=0; i < cirX.size(); i++) {
                Circle c = (Circle) cirX.get(i);
              System.out.println("c.positionX = " + c.positionX + " c.positionY = " + c.positionY);         
            }
            System.out.println("");
        }

        System.out.println("");
        System.out.println("");

        Iterator itmY = hmY.entrySet().iterator();
        while (itmY.hasNext()) {
            Map.Entry hmArrListY = (Map.Entry)itmY.next();
            ArrayList cirY = (ArrayList) hmArrListY.getValue();
            System.out.println("Circles on the same Y = " + hmArrListY.getKey());
            for (int i=0; i < cirY.size(); i++) {
                Circle c = (Circle) cirY.get(i);
              System.out.println("c.positionX = " + c.positionX + " c.positionY = " + c.positionY);         
            }
            System.out.println("");
        }

        }
    }

    @Test
    public void test() {
        System.out.println("circles.length = " + circles.length);
        System.out.println("");
        showCenterSameHorizOrVert(circles);
    }

}

It will give me result:

circles.length = 6

Circles on the same X = 1
c.positionX = 1 c.positionY = 2
c.positionX = 1 c.positionY = 5

Circles on the same X = 2
c.positionX = 2 c.positionY = 7

Circles on the same X = 3
c.positionX = 3 c.positionY = 8
c.positionX = 3 c.positionY = 5

Circles on the same X = 6
c.positionX = 6 c.positionY = 7



Circles on the same Y = 2
c.positionX = 1 c.positionY = 2

Circles on the same Y = 5
c.positionX = 1 c.positionY = 5
c.positionX = 3 c.positionY = 5

Circles on the same Y = 7
c.positionX = 6 c.positionY = 7
c.positionX = 2 c.positionY = 7

Circles on the same Y = 8
c.positionX = 3 c.positionY = 8

I used Integer for X and Y to simplify printout, may me you need to replace it to Double. I store result in HashMap based on array hashcode to not loop each time you run this function the whole array and inside it loop whole array only once and put all values in two HashMaps based on X and Y coordinates (its group all values based on this keys) and then I just loop resulted HashMaps with groupped values to show which objects on the same line based on X or Y. Hope my logic will help you adjust it to your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM