有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Y轴在代码中反转,应该形成一个矩形

所以我的代码由3个部分组成,2个类和测试思想是,2个类使用来自测试的输入形成一个矩形。 这是测试代码

@Test
    public void testRectangle1() {
        Point center = new Point(20, 30);
        Rectangle rect = new Rectangle(center, 20, 20);
        assertAll(
                () -> assertEquals(10, rect.getTopLeft().getX()),
                () -> assertEquals(20, rect.getTopLeft().getY()),
                () -> assertEquals(30, rect.getBottomRight().getX()),
                () -> assertEquals(40, rect.getBottomRight().getY()),
                () -> assertEquals(20, rect.getWidth()),
                () -> assertEquals(20, rect.getHeight())
        );
    }

第一节课叫Point,效果很好

public class Point {

    private int x, y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public Point() {
        this(0, 0);
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public void moveTo(int newX, int newY) {
        x = newX;
        y = newY;
    }

    public void moveRel(int dx, int dy) {
        x += dx;
        y += dy;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + x;
        result = prime * result + y;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Point other = (Point) obj;
        if (x != other.x)
            return false;
        if (y != other.y)
            return false;
        return true;
    }
}

所以这是第二类形成了纠结。它由几个传统的矩形构造方法组成

public class Rectangle {
    public int width = 0;
    public int height = 0;
    public Point center;

    public Rectangle(Point center, int width, int height) {
        int x = 0;
        int y = 0;
        this.width=width;
        this.height=height;
        this.center=center;
    }

    public Point getTopLeft() {
        Point point = new Point(center.getX(), center.getY());
        point.moveRel(- width / 2, height / 2);
        return point;
    }

    public Point getBottomRight() {
        Point point = new Point(center.getX(), center.getY());
        point.moveRel(width / 2, - height / 2);
        return point;
    }

    public int getWidth() {

        return width;
    }


    public int getHeight() {

        return height;
    }
}

所以问题是,y轴似乎在某种程度上是颠倒的,因为当我运行测试时,它返回了错误的值, () -> assertEquals(20, rect.getTopLeft().getY()),返回40而不是20,然后进行测试 () -> assertEquals(40, rect.getBottomRight().getY()),返回2o而不是40。所以看起来左上角和右下角的点在y轴上是倒转的


共 (1) 个答案

  1. # 1 楼答案

    在moveRel中添加高度/2以获得左上角。因此,你得到40而不是20是正常的

    在高度参数之前似乎存在符号错误:

    public Point getTopLeft() {
        Point point = new Point(center.getX(), center.getY());
        point.moveRel(- width / 2, height / 2);
        return point;
    }
    
    public Point getBottomRight() {
        Point point = new Point(center.getX(), center.getY());
        point.moveRel(width / 2, - height / 2);
        return point;
    }