如何检查形状是否相互相交?

wwodge7n  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(157)

我想知道我怎样才能检查两个形状是否相交。我需要使用定制的shape类,所以使用java.awt.shape不能作为解决方案。例如,如果有两个圆共用一个区域,我怎么说它们相交,我怎么检查?我正在尝试添加类似于“前置”和“后置”的方法,就像你在powerpoint中所能做的那样,也可以将这些形状向前或向后移动一个位置。形状类别:

public abstract class Shape implements Moveable, Comparable {

    private boolean selected;
    private Color color;

    public abstract void draw(Graphics g);
    public abstract boolean contains(int x, int y);

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }
}

有多个类可以扩展 Shape 同学们,我会发帖的 Circle 班级。 Fill 以及 Area 方法来自 SurfaceShapes 类,它扩展了 Shape .

public class Circle extends SurfaceShapes {

    private Point center = new Point();
    private int radius;

    public Circle() {
    }

    public Circle(Point center, int radius) {
        this.center = center;
        this.radius = radius;
    }

    public Circle(Point center, int radius, boolean selected) {
        this(center, radius);
        setSelected(selected);
    }

    public Circle(Point center, int radius, boolean selected, Color color) {
        this(center, radius, selected);
        setColor(color);
    }

    public Circle(Point center, int radius, boolean selected, Color color, Color innerColor) {
        this(center, radius, selected, color);
        setInnerColor(innerColor);
    }

    public static void metoda() {
        System.out.println("klasa circle");
    }

    @Override
    public int compareTo(Object o) {
        if (o instanceof Circle) {
            return (this.radius - ((Circle) o).radius);
        }
        return 0;
    }

    @Override
    public void moveBy(int byX, int byY) {
        this.center.moveBy(byX, byY);
    }

    @Override
    public void fill(Graphics g) {
        g.setColor(getInnerColor());
        g.fillOval(this.center.getX() - radius + 1, this.center.getY() - radius + 1, radius *2 - 2, radius * 2 - 2);
    }

    @Override
    public void draw(Graphics g) {
        g.setColor(getColor());
        g.drawOval(this.center.getX() - radius, this.center.getY() - radius, this.radius*2, this.radius*2);
        fill(g);
        if (isSelected()) {
            g.setColor(Color.BLUE);
            g.drawRect(this.center.getX() - 3, this.center.getY() - 3, 6, 6);
            g.drawRect(this.center.getX() - radius - 3, this.center.getY() - 3, 6, 6);
            g.drawRect(this.center.getX() + radius - 3, this.center.getY() - 3, 6, 6);
            g.drawRect(this.center.getX() - 3, this.center.getY() - radius - 3, 6, 6);
            g.drawRect(this.center.getX() - 3, this.center.getY() + radius - 3, 6, 6);
        }
    }

    public double area() {
        return radius * radius * Math.PI;
    }

    public boolean contains(int x, int y) {
        return this.center.distance(x, y) <= radius;
    }

    public boolean contains(Point p) {
        return center.distance(p.getX(), p.getY()) <= radius;
    }

    public boolean equals(Object obj) {
        if (obj instanceof Circle) {
            Circle prosledjeni = (Circle) obj;
            if (this.center.equals(prosledjeni.getCenter()) && this.radius == prosledjeni.getRadius()) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public Point getCenter() {
        return center;
    }

    public void setCenter(Point center) {
        this.center = center;
    }

    public int getRadius() {
        return radius;
    }

    public void setRadius(int radius) throws Exception {
        if (radius > 0) {
            this.radius = radius;
        } else {
            throw new NumberFormatException("Radius has to be a value greater then 0!");
        }
    }

    public String toString() {
        return "Center=" + center + ", radius=" + radius;
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题