用于Java的矩形交叉(冻结标记)

时间:2016-11-17 22:32:25

标签: java class intersection

**编辑仍然没有得到正确的答复** 我不太明白如何弄清楚我的项目的交叉方面。到目前为止,我确定了顶部,底部,左侧和右侧,但我不确定从那里开始。

主驾驶员应该打电话来检查我的移动矩形是否相交,如果矩形是冻结的,那么与它相交的移动矩形应该解冻它并改变它的颜色。我理解如何解冻它并改变颜色,但无论什么原因,当它们相交时它不会将值返回为真,我知道这段代码是错误的。任何有用的提示都表示赞赏。

*CLASS CODE*

import edu.princeton.cs.introcs.StdDraw;
import java.util.Random;
import java.awt.Color;

public class MovingRectangle {
Random rnd = new Random();
private int xCoord;
private int yCoord;
private int width;
private int height;
private int xVelocity;
private int yVelocity;
private Color color;
private boolean frozen;
private int canvas;

public MovingRectangle(int x, int y, int w, int h, int xv, int yv, int canvasSize) {
    canvas = canvasSize;
    xCoord = x;
    yCoord = y;
    width = w;
    height = h;
    xVelocity = xv;
    yVelocity = yv;
    frozen = false;
    int c = rnd.nextInt(5);

    if (c == 0) {
        color = StdDraw.MAGENTA;
    }
    if (c == 1) {
        color = StdDraw.BLUE;
    }
    if (c == 2) {
        color = StdDraw.CYAN;
    }
    if (c == 3) {
        color = StdDraw.ORANGE;
    }
    if (c == 4) {
        color = StdDraw.GREEN;
    }

}

public void draw() {
    StdDraw.setPenColor(color);
    StdDraw.filledRectangle(xCoord, yCoord, width, height);
}

public void move() {
    if (frozen == false) {
        xCoord = xCoord + xVelocity;
        yCoord = yCoord + yVelocity;
    }
    else {
        xCoord +=0;
        yCoord +=0;
    }
    if (xCoord >= canvas || xCoord < 0) {
        xVelocity *= -1;
        this.setRandomColor();
    }

    if (yCoord >= canvas || yCoord < 0) {
        yVelocity *= -1;
        this.setRandomColor();
    }


}

public void setColor(Color c) {
    StdDraw.setPenColor(color);
}

public void setRandomColor() {
    int c = rnd.nextInt(5);

    if (c == 0) {
        color = StdDraw.MAGENTA;
    }
    if (c == 1) {
        color = StdDraw.BLUE;
    }
    if (c == 2) {
        color = StdDraw.CYAN;
    }
    if (c == 3) {
        color = StdDraw.ORANGE;
    }
    if (c == 4) {
        color = StdDraw.GREEN;
    }

}

public boolean containsPoint(double x, double y) {
    int bottom = yCoord - height / 2;
    int top = yCoord + height / 2;
    int left = xCoord - width / 2;
    int right = xCoord + width / 2;

    if (x > left && x < right && y > bottom && y < top) {
        color = StdDraw.RED;
        return true;
    } else {
        return false;
    }

}

public boolean isFrozen() {
    if (frozen) {
        return true;

    } else {
        return false;
    }

}

public void setFrozen(boolean val) {
    frozen = val;


}

public boolean isIntersecting(MovingRectangle r) {
int top = xCoord + height/2;
int bottom = xCoord - height/2;
int right = yCoord + width/2;
int left = yCoord - width/2;

int rTop = r.xCoord + r.height/2;
int rBottom = r.xCoord - r.height/2;
int rRight = r.yCoord + r.width/2;
int rLeft = r.yCoord - r.width/2;

  if(right <= rRight && right >= rLeft || bottom <= rBottom && bottom    
   >= rTop){

  return true;

    } else {
        return false;
    }

    }

}

这也是我的主要驱动因素,因为我也可能在这里做错了。

 import edu.princeton.cs.introcs.StdDraw;
 import java.util.Random;

public class FreezeTagDriver {
public static final int CANVAS_SIZE = 800;

public static void main(String[] args) {
    StdDraw.setCanvasSize(CANVAS_SIZE, CANVAS_SIZE);
    StdDraw.setXscale(0, CANVAS_SIZE);
    StdDraw.setYscale(0, CANVAS_SIZE);

    Random rnd = new Random();
    MovingRectangle[] recs;
    recs = new MovingRectangle[5];
    boolean frozen = false;

    for (int i = 0; i < recs.length; i++) {
        int xv = rnd.nextInt(4);
        int yv = rnd.nextInt(4);
        int x = rnd.nextInt(400);
        int y = rnd.nextInt(400);
        int h = rnd.nextInt(100) + 10;
        int w = rnd.nextInt(100) + 10;

        recs[i] = new MovingRectangle(x, y, w, h, xv, yv, CANVAS_SIZE);
    }
    while (true) {
        StdDraw.clear();

        for (int i = 0; i < recs.length; i++) {

            recs[i].draw();
            recs[i].move();

        }

        if (StdDraw.mousePressed()) {

            for (int i = 0; i < recs.length; i++) {
                double x = StdDraw.mouseX();
                double y = StdDraw.mouseY();

                if (recs[i].containsPoint(x, y)) {
                    recs[i].setFrozen(true);
                }
            }
        }

        for (int i = 0; i < recs.length; i++) {
//for 0
        if(recs[0].isFrozen() && recs[0].isIntersecting(recs[1])){
            recs[0].setFrozen(false);
                }

        if(recs[0].isFrozen() && recs[0].isIntersecting(recs[2])){
            recs[0].setFrozen(false);
                }

        if(recs[0].isFrozen() && recs[0].isIntersecting(recs[3])){
            recs[0].setFrozen(false);

                }

 //for 1    
        if(recs[1].isFrozen() && recs[1].isIntersecting(recs[2])){
            recs[1].setFrozen(false);

                }
        if(recs[1].isFrozen() && recs[1].isIntersecting(recs[3])){
            recs[1].setFrozen(false);
                }

        if(recs[1].isFrozen() && recs[1].isIntersecting(recs[4])){
            recs[1].setFrozen(false);
                }

//for 2
        if(recs[2].isFrozen() && recs[2].isIntersecting(recs[0])){
            recs[2].setFrozen(false);

                }
        if(recs[2].isFrozen() && recs[2].isIntersecting(recs[1])){
            recs[2].setFrozen(false);
                }

        if(recs[2].isFrozen() && recs[2].isIntersecting(recs[3])){
            recs[2].setFrozen(false);

                }
        if(recs[2].isFrozen() && recs[2].isIntersecting(recs[4])){
            recs[2].setFrozen(false);

                }
//for 3                 
        if(recs[3].isFrozen() && recs[3].isIntersecting(recs[0])){
            recs[3].setFrozen(false);

                }

        if(recs[3].isFrozen() && recs[3].isIntersecting(recs[1])){
            recs[3].setFrozen(false);
                }

        if(recs[3].isFrozen() && recs[3].isIntersecting(recs[2])){
            recs[3].setFrozen(false);
                }

        if(recs[3].isFrozen() && recs[3].isIntersecting(recs[4])){
            recs[3].setFrozen(false);
                }
//for 4         
        if(recs[4].isFrozen() && recs[4].isIntersecting(recs[0])){
            recs[4].setFrozen(false);
                }

        if(recs[4].isFrozen() && recs[4].isIntersecting(recs[1])){
            recs[4].setFrozen(false);
                }

        if(recs[4].isFrozen() && recs[4].isIntersecting(recs[3])){
                recs[4].setFrozen(false);
                }

        if(recs[4].isFrozen() && recs[4].isIntersecting(recs[2]))
                recs[4].setFrozen(false);
                }










        if (recs[0].isFrozen() && recs[1].isFrozen() &&      
   recs[2].isFrozen() && recs[3].isFrozen()
                && recs[4].isFrozen()) {
            StdDraw.text(400, 400, "YOU WIN");

        }
        StdDraw.show(20);

    }

}

}

1 个答案:

答案 0 :(得分:0)

请记住,您在此处使用OR运算符。因此,如果权利小于rLeft,则您的interector将返回true。这不是它应该如何运作。

你需要检查右边是否在矩形边界内。

if(right <= rRight && right >= rLeft || the other checks here)

上面的代码检查右边是否小于矩形右边,还有右边比矩形左边大,这意味着它在矩形中间的某个位置&#39 ; s左右。

如果这变得太复杂,你可以简单地使用java的rectangle类,因为它有方法containsintersects