英文:
I need class that will create rectangle using coordinates of the centers, high (X axis) and width (y axis)
问题
以下是您提供的内容的翻译:
// 测试中创建矩形
@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())
);
}
// 点类已经存在,应该能正确工作,主要是为了理解我大部分添加的。
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;
}
}
// 因此我在矩形本身的第二个类中卡住了。首先,我有点难以形成构造函数,该构造函数将形成矩形本身。
// 并且在矩形类中填充方法时也遇到了问题,因为我在理解上有些困难,因为我在Java和编码方面经验有限。
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;
width=x;
height=y;
}
public Point getTopLeft() {
Point point = new Point();
return point;
}
public Point getBottomRight() {
Point point = new Point();
return point;
}
public int getWidth() {
int x = 0;
return x;
}
public int getHeight() {
int y = 0;
return y;
}
}
英文:
Rectangle is formed using on this 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())
);
}
I already have class for point it should work correctly i mostly add it for understanding.
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 i stuck in second class for rectangle itself. First of all i kinda have hard time forming consructor that will form rectangle itself. And also on filling methods in rectangle class, as i have hard time in uderstanding what they should return becouse i have little experience in Java and coding.
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;
width=x;
height=y;
}
public Point getTopLeft() {
Point point = new Point();
return point;
}
public Point getBottomRight() {
Point point = new Point();
return point;
}
public int getWidth() {
int x = 0;
return x;
}
public int getHeight() {
int y = 0;
return y;
}
}
答案1
得分: 0
在getTopLeft()
和getBottomRight()
方法中,您可以使用中心坐标创建一个点,然后通过宽度和高度除以2来使用moveRel(int, int)
方法进行平移,因为您的中心点位于两者之间。
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;
}
构造函数应该将给定的值写入类的字段中。还要考虑为坐标使用小数类型,只有偶数的高度和宽度才能被2整除。
英文:
In getTopLeft()
and getBottomRight()
you could create a point with center coordinates and then moveRel(int, int)
it by width and height divided by 2, as you center point is in the middle of the both.
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;
}
Constructor should write given values to the fields of the class.
Also consider using decimal type for the coordinates, only even height and width would be divisible by 2.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论