我目前有一个程序与网格和线,将绘制在网格中的点之间。它使用标准绘图。我希望限制线的长度,使它们只能从相邻的点开始,但不知道如何做到这一点。
谢谢
StdDraw.setCanvasSize(400, 400);
StdDraw.setXscale(0, 10);
StdDraw.setYscale(0, 10);
//dots
double radius = .15;
double spacing = 2.0;
for (int i = 0; i <= 4; i++) {
for (int j = 0; j <= 4; j++) {
StdDraw.setPenColor(StdDraw.GRAY);
StdDraw.filledCircle(i * spacing, j * spacing, radius );
}
}
StdDraw.setPenColor(StdDraw.BLUE);
StdDraw.text(0, 9.5, player1_name);
StdDraw.setPenColor(StdDraw.RED);
StdDraw.text(5, 9.5, player2_name);
int turn = 1;
for (int i = 0; i <= 40; i++) {
if (turn % 2 == 0)
StdDraw.setPenColor(StdDraw.RED);
else
StdDraw.setPenColor(StdDraw.BLUE);
while(!StdDraw.mousePressed()) { }
double x = StdDraw.mouseX();
double y = StdDraw.mouseY();
System.out.println(x + " " + y);
StdDraw.setPenRadius(.01);
StdDraw.show(200);
while(!StdDraw.mousePressed()) { }
double x2 = StdDraw.mouseX();
double y2 = StdDraw.mouseY();
StdDraw.show(200);
double xround = Math.round(x);
double yround = Math.round(y);
double x2round = Math.round(x2);
double y2round = Math.round(y2);
int xroundb = (int) xround;
int yroundb = (int) yround;
int x2roundb = (int) x2round;
int y2roundb = (int) y2round;
StdDraw.line(xround, yround, x2round, y2round);
System.out.println("Line Drawn");
StdDraw.show(); 发布于 2013-01-05 04:38:52
啊我明白了。您询问的不是正确工作的实际line方法,而是这样的逻辑:如果没有选择邻接点,则不会调用line。
首先,我们需要知道哪些相邻连接是允许的。那就是我们可以使用Vertical吗?水平?对角线?我会逐一解释,以防万一。
所以你有了spacing = 2.0。好吧,这应该足以检查邻接关系了。
if (Math.abs(x2round - xround) > spacing) {
// don't draw
} else if (Math.abs(y2round - yround) > spacing)) {
// don't draw
} else if (Math.abs(y2round - yround) > 0.0) && Math.abs(x2round - xround) > 0.0) {
// don't draw if diagonal connections are forbidden
// if diagonal is allowed, remove this else if condition
} else {
StdDraw.line(xround, yround, x2round, y2round);
}因此,如果你不画画,那么你必须强制执行你的游戏逻辑。也许玩家失去了一次机会。也许玩家又有机会选择相邻的点。这取决于你。由于舍入,比较双精度值总是有点疯狂,所以不使用0.0,您可能希望选择一个非常小的epsilon双精度值,以确保捕获所有情况。
https://stackoverflow.com/questions/13920368
复制相似问题