我无法进行比较。该项目应该从用户输入中绘制一个正方形,然后用户单击的任何位置都将在其中绘制一个不同颜色的点。例如,如果他们在正方形内单击,则应形成一个红色圆圈,如果在正方形边缘上,则应形成一个绿色圆圈,在外部,则应形成一个蓝色圆圈。目前,我的程序绘制了红色和蓝色的圆圈,但没有绿色。实际上,当它高于某个点时,它也会绘制红色圆圈。
public class Square extends GraphicsProgram {
// Instance variables
private int side; // the length of a side
private int anchorX; // the X value at the upper left corner
private int anchorY; // the Y value at the upper left corner
public Square(int x, int y, int side) {
anchorX = x;
anchorY = y;
this.side = side;
}
// mouseClicked method
public void mouseClicked(MouseEvent e) {
// Find the location where the mouse was clicked
int x = e.getX();
int y = e.getY();
// boolean variables to indicate location
boolean isInside = false;
boolean isOutside = false;
boolean isOnEdge = false;
if (x > anchorX + 1 && anchorY + 1 < y && anchorY + side + 1 > y) {
isInside = true;
}
if (x > anchorX + side + 1 && anchorY + side + 1 < y && x > anchorX + side - 1 & y > anchorY + side - 1) {
isOutside = true;
}
/*** NOTE: There a hard, and an easy way to do this! ***/
if (anchorX - 1 <= x && x <= anchorX - 3 && anchorY - 1 <= y && anchorY + side - 3 >= y) {
isOnEdge = true;
}
if (isOnEdge == true) {
System.out.println("(" + x + ", " + y + ") is on the square");
GOval circle = new GOval(x - 2, y - 2, 4, 4);
circle.setFillColor(Color.GREEN);
circle.setFilled(true);
add(circle);
}
else if (isInside == true) {
System.out.println("(" + x + ", " + y + ") is inside the square");
GOval circle = new GOval(x - 2, y - 2, 4, 4);
circle.setFillColor(Color.RED);
circle.setFilled(true);
add(circle);
}
else if (isOutside == true) {
System.out.println("(" + x + ", " + y + ") is outside the square");
GOval circle = new GOval(x - 2, y - 2, 4, 4);
circle.setFillColor(Color.BLUE);
circle.setFilled(true);
add(circle);
}
}
}
我们得到了如何将正方形的(x,y)位置设置为
的提示“例如,正方形的左边缘具有:
x值范围:anchorX-1≤x≤anchorX + 1,以及 y值的范围为:anchorY-1≤y≤anchorY + side + 1。
这意味着如果我们有一个带有anchorX 50,anchor Y 100和side 60的正方形,则将在左侧边缘考虑像(49-51,99-161)之类的坐标。
答案 0 :(得分:0)
我认为问题是与构成正方形的边界有些混乱-可以理解,因为很难可视化。
if (x > anchorX+1 && anchorY+1 < y && anchorY+side+1 > y) {
isInside = true;
}
现在,这个:
x > anchorX+1
的右侧anchorY+1 < y
下方anchorY+side+1 > y
上方与之相关的问题是:
anchorY+side+1 > y
将包含边缘-+1将结束一个可行的解决方案是:
if (x > anchorX+1 && y > anchorY+1 && x < anchorX+side-1 && y < anchorY+side-1) {
isInside = true;
}
if (x > anchorX+side+1 && anchorY+side+1 < y && x > anchorX+side-1 & y > anchorY+side-1) {
isOutside = true;
}
现在,这个:
x > anchorX+side+1
的右侧anchorY+side+1 < y
下方x > anchorX+side-1
的右侧y > anchorY+side-1
上方与之相关的问题是:
x > anchorX+side-1
和y > anchorY+side-1
将在考虑到的区域中包括边。x > anchorX+side+1
和x > anchorX+side-1
几乎一样。&
符号。区别在于&&
仅在前一个都为true时运行,而&
始终运行。因此&&
更快,通常是首选。一个可行的解决方案是:
if (x < anchorX-1 || y < anchorY-1 || x > anchorX+side+1 || y > anchorY+side+1) {
isOutside = true;
}
这是比较棘手的
if (anchorX-1 <= x && x <= anchorX-3 && anchorY-1 <= y && anchorY+side-3 >= y) {
isOnEdge = true;
}
现在,这个:
anchorX-1 <= x
左侧的右侧x <= anchorX-3
的左侧a点的左侧anchorY-1 <= y
的顶部下方anchorY+side-3 >= y
上方的点上方与之相关的问题是:
anchorX-1 <= x
和x <= anchorX-3
是矛盾的,因此不存在x都为真的x,因此整个事物必须为假。&&
而不是||
(OR)时,只有当整个情况为真时,它才会为真-它必须同时在两个边缘内-仅意味着角落就算在内了。可以解决此问题,以为此创建一个可行的解决方案,但是手动定义所有条件将需要大量工作。这就是在简单易懂的注释中所指的内容。
作为提示,您可以使用已知的事实来简化它。 我建议考虑一下。
如果您正在努力理解所有这些,我建议手动将其绘制出来并将所有值写下来-比尝试动手要容易得多。您做得越多,就越容易。