java - 检查arraylist中是否存在具有特定坐标的圆?

标签 java arraylist geometry

我的应用程序中有一个圆圈数组列表。基本上,我正在绘制一个由六边形瓷砖制成的游戏板。我想在每个瓷砖的每个角上画圆圈。我将所有图 block 存储在 Tile 对象数组中。每个图 block 都有一个组成该图 block 的点数组。所以我基本上遍历每个图 block 和图 block 的每个角。我的问题是大多数图 block 共享交叉点。因此,为共享该交点的每个图 block 绘制一个圆圈。但我只想在每个路口画一个。我需要做的是检查数组中是否存在具有特定坐标集的圆。如果确实存在,则不要绘制圆圈。我知道 ArrayList.contains(),但不知道如何将其用于我的目的。有什么想法吗?

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);
            }
        });
    }

}

最佳答案

由于 Collection 类,您正在使用的 Circle 类可以在方法 contains(Object) 中实现方法 equals o),使用逻辑:

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

其中 e 是列表中的当前对象,o 是要比较的对象。

因此,您的 Circle 类将是:

类(class)圈子

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;
    }
}

关于java - 检查arraylist中是否存在具有特定坐标的圆?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42008047/

相关文章:

java - Applet 的替代品 - 客户端需要文件访问权限

java - 如何在模板templates.jsp中使用if语句?

java - 将 sqlite 中的数据放入 ArrayList<String>

java - 计算 Arraylist 中字符串出现的次数

java - Android速度BufferedReader读取行和ArrayList添加和获取

c - 求三角形c的最长边

java - Java Float 和 Double 数据类型中的上溢和下溢

Java FX : How to change the style of a cell when a property of the object relating to a row changes in TableView?

javascript - Fabric JS - 在两点之间绘制百分比线

geometry - 射线-正方形相交 3D