英文:
Y axis inverted in code that should form a rectangle
问题
代码包含3个部分,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 {
// 类的定义
}
第二个类是Rectangle,它包含了构造函数和其他一些用于构造矩形的方法。
public class Rectangle {
// 类的定义
}
问题在于似乎y轴被反转了,因为测试运行时返回了错误的值。比如,assertEquals(20, rect.getTopLeft().getY())
应该返回20,但却返回了40;而测试assertEquals(40, rect.getBottomRight().getY())
应该返回40,但却返回了20。看起来,左上角和右下角的点在y轴上被反转了。
英文:
So i code consist of 3 parts 2 classes and test Idea is that 2 classes form a rectangle using inputs form the test.
This is code for test
@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())
);
}
First class called Point it works fine.
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;
}
}
So this is teh second class that forms recatangle itself. It consist of constructor on a few aditional methods for forming my rectangle
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;
}
}
So the problem is it seems that y axis is somehow inverted becouse when i run the test it returns wrong value,
() -> assertEquals(20, rect.getTopLeft().getY())
, Where it returns 40 instead of 20 and test
() -> assertEquals(40, rect.getBottomRight().getY())
, returns 2o instead of 40. So it seems that top left and bottom right point is inverted on y axis.
答案1
得分: 0
你在moveRel中添加了height/2以获取topLeft。因此你得到40而不是20。
在以下代码中,在height参数之前似乎存在一个符号错误:
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;
}
英文:
You add height/2 in moveRel to get topLeft. Hence it is normal that you get 40 and not 20.
Seems there is a sign error before height parameters in:
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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论