简体   繁体   中英

Check if a circle with certain coordinates exists in arraylist?

I have an arraylist of circles in my app. Basically, I am drawing a game board made of hexagon tiles. I want to draw circles on each of the corners of each of the tiles. I store all the tiles in an array of Tile objects. Each tile has an array of points that make up the tile. So I basically loop through each tile and each corner of the tile. My problem is that most of the tiles share intersections. Therefore, one circle is drawn for each tile that shares that intersection. But I only want to draw one at each intersection. What I need to do is check if a circle exists in the array with a specific set of coordinates. If it does exist, don't draw the circle. I am aware of ArrayList.contains(), but have no clue how I can use that for my purpose. Any ideas?

for (int t = 0; t < tiles.length; t++) {

    //Loop through the vertices of the current tile
    for (int v = 0; v < tiles[t].vertices.length; v++) {
        final double x = tiles[t].vertices[v].x;
        final double y = tiles[t].vertices[v].y;
        Platform.runLater(() -> {
            //This if statement doesn't work because the arraylist contains shapes, not points!
            if(!highlightedVertices.contains(new Point((int)x, (int)y))){ 
                Circle cir = new Circle();                         
                cir.setFill(Color.web("rgba(255, 235, 138, .3)"));
                cir.setCenterX(x);
                cir.setCenterY(y);
                cir.setRadius(windowHeight * .025);
                highlightedVertices.add(cir);
                wrapperPane.getChildren().add(cir);
            }
        });
    }

}

The Circle class you are using could implement the method equals , due the Collection class, in the method contains(Object o) , uses the logic:

(o == null ? e == null: o.equals(e));

Where e is a current object in the list and o is a object to compare.

So, your Circle class would be:

Class Circle

public class Circle {
    private double centerX; //Asumming is a double
    private double centerY; //Asumming is a double
    private double radius;

    // +getters and setters

    @Override
    public boolean equals(Object o) {
        if (o == null)
            return false;
        if (o instance of Circle) {
            Circle paramCircle = (Circle) o;
            if (paramCircle.getCenterX() == this.centerX && paramCircle.getCenterY() == this.centerY && paramCircle.getRadius() == this.radius) {
                return true;
        }
        return false;
    }
}

After a bit more pondering, I realized that Sebastian's answer wouldn't work because there was an offset of 1 pixel at each "intersection". The code below that I wrote worked perfectly. I just looped through each circle in the arraylist and determined if they each contained the origin of the new circle.

    private boolean CircleExists(Point origin, ArrayList<Circle> circles){

        boolean output = false;
        for(int i = 0; i < circles.size(); i++){

            Circle cir = circles.get(i);
            if(cir.contains(origin.x, origin.y)){
                output = true;
            }

        }

        return output;
    }

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