图形程序中的气泡



嘿all

我有一个程序,可以通过触摸屏幕(手势检测器),在任何地方绘制气泡我想处理一个问题,不要通过Intersect方法在同一位置绘制两个气泡我该怎么做?**

我假设您为现有气泡有某种收集。每当您有新气泡时,都会使用类似的东西来找出它是否与收藏中的任何气泡相交。如果没有,请绘制并将其添加到集合中。

import java.util.Collection;
public class Bubble {
    private int centreX;
    private int centreY;
    private int radius;
    public Bubble(int centreX, int centreY, int radius) {
        this.centreX = centreX;
        this.centreY = centreY;
        this.radius = radius;
    }
    public boolean intersectsAny(Collection<Bubble> others){
        for (Bubble other : others) {
            if (intersects(other)) {
                return true;
            }
        }
        return false;
    }
    private boolean intersects(Bubble other) {
        int distanceSquared = (centreX - other.centreX) * (centreX - other.centreX)
            + (centreY - other.centreY) * (centreY - other.centreY);
        return distanceSquared <= (radius + other.radius) * (radius + other.radius);
    }
}

最新更新