简体   繁体   English

在两点之间绘制正方形

[英]Draw squares between two points

I'm working on some exercises and I've been stuck on this for some hours now (quite new to Java). 我正在做一些练习,现在我已经坚持了几个小时(对Java来说很新)。 Anyhow, this is what I'm supposed to do: When I run the program I will have a square in the middle of the screen and when I then click somewhere within that screen another square will be drawn at the place where I clicked and in-between these two points there are supposed to be 10 squares. 无论如何,这是我应该做的:当我运行程序时,我将在屏幕中间有一个正方形,然后当我点击该屏幕中的某个地方时,将在我点击的地方绘制另一个正方形 - 在这两点之间应该有10个方格。 So wherever I click there should always be 10 squares drawn between. 所以无论我在哪里点击,都应该总是画10个方格。

However, I can't make it to function properly. 但是,我无法让它正常运作。

This is what I've managed to do so far: 这是我到目前为止所做的事情:

import se.lth.cs.ptdc.window.SimpleWindow;  
import se.lth.cs.ptdc.square.Square;


public class PrintSquares2 {


public static void main(String[] args) {
    SimpleWindow w = new SimpleWindow(600, 600, "PrintSquares2");
    int posX = 300;
    int posY = 300;
    int loop = 0;
    System.out.println("Skriv rotation");
    Square sq1 = new Square(posX,posY,200);
    sq1.draw(w);


            w.waitForMouseClick();
            int destX = w.getMouseX();
            int destY = w.getMouseY();
            System.out.println("Dest X: " + destX + " Dest Y: " + destY);
            System.out.println("Pos X: " + posX + " Pos Y: " + posY);
            SimpleWindow.delay(10);
            //sq1.erase(w);
            int jumpX = (destX - posX) / 10;
            int jumpY = (destY - posY) / 10;
            System.out.println(jumpX);


                while (posX < destX)
                {       
                    posX = posX+10;
                    SimpleWindow.delay(100);
                    loop++;
                    System.out.println("Loop: " + loop);
                    System.out.println("Dest X: " + destX + " Dest Y: " + destY);
                    System.out.println("Pos X: " + posX + " Pos Y: " + posY);       
                    Square sq2 = new Square(posX,posY,200);         
                    sq2.draw(w);                        
                }

                while (posX > destX)
                {
                    posX = posX-10;
                    SimpleWindow.delay(100);
                    loop++;
                    System.out.println("Loop: " + loop);
                    System.out.println("Dest X: " + destX + " Dest Y: " + destY);
                    System.out.println("Pos X: " + posX + " Pos Y: " + posY);
                    sq1.draw(w);
                    Square sq2 = new Square(posX,posY,200);         
                    sq2.draw(w);
                }

                while (posY < destY)
                {       
                    posY = posY+10;
                    SimpleWindow.delay(100);
                    loop++;
                    System.out.println("Loop: " + loop);
                    System.out.println("Dest X: " + destX + " Dest Y: " + destY);
                    System.out.println("Pos X: " + posX + " Pos Y: " + posY);
                    sq1.draw(w);
                    Square sq2 = new Square(posX,posY,200);         
                    sq2.draw(w);
                }

                while (posY > destY)
                {
                    posY = posY-10;
                    SimpleWindow.delay(100);
                    loop++;
                    System.out.println("Loop: " + loop);
                    System.out.println("Dest X: " + destX + " Dest Y: " + destY);
                    System.out.println("Pos X: " + posX + " Pos Y: " + posY);
                    sq1.draw(w);
                    Square sq2 = new Square(posX,posY,200);         
                    sq2.draw(w);
                }


            SimpleWindow.delay(10);
            sq1.draw(w);

            //SimpleWindow.clear(w);


    }

} }

I'm pretty sure that I overcomplicated everything since this should be pretty basic. 我很确定我过于复杂,因为这应该是非常基本的。

The end result is supposed to look like this: End result 最终结果应如下所示: 最终结果

This is the way I'd have solved it: 这是我解决它的方式:

I didn't quite understand the documentation on se.lth.cs.ptdc.square.Square but I'll assume it draws a square given the coordinates of its top-left corner and a side size. 我对se.lth.cs.ptdc.square.Square上的文档并不十分了解,但我会假设它根据左上角的坐标和边长来绘制一个正方形。

So you have the coodinates of your first square's left-top corner and the coordinates of the last square's center. 所以你有第一个方格左上角的坐标和最后一个方格中心的坐标。 Having that it's not difficult to get the coords of the last square's top-left corner: 得到最后一个方格的左上角的坐标并不困难:
lastX = centerX - side/2
lastY = centerY - side/2

After you have that you find the difference between the starting and ending points: 完成后,您会发现起点和终点之间的差异:
diffX = posX - lastX
diffY = posY - lastY

and after that just draw 9 more squares: 然后再抽出9个方块:

for (int i=1; i<10; i++){
    squareX = posX + (diffX/10)*i;
    squareY = posY + (diffY/10)*i;
    Square square = new Square(squareX,squareY,200);         
    square.draw(w);
}

Actually you did the first part right, just messed up with those unnecessary checks. 实际上你做的第一部分是正确的,只是搞砸了那些不必要的检查。 Hope it helps. 希望能帮助到你。

-- -
Regards, svz. 问候,svz。

Update both X and Y at the SAME time : 同时更新X和Y:

    int jumpX = (destX - posX) / 10;
    int jumpY = (destY - posY) / 10;
    if (posX > destX) {
        int temp = destX;
        destX = posX;
        posX = temp;
    }

    while (posX <= destX)
    {       
            SimpleWindow.delay(100);
            loop++;
            System.out.println("Loop: " + loop);
            System.out.println("Dest X: " + destX + " Dest Y: " + destY);
            System.out.println("Pos X: " + posX + " Pos Y: " + posY);       
            Square sq2 = new Square(posX,posY,200);         
            sq2.draw(w);                        
            posX = posX+jumpX;
            posY = posY+jumpY;
    }    

    SimpleWindow.delay(10);
    sq1.draw(w);

Here's how you move in two directions at once (on a diagonal). 这是你一次向两个方向移动(在对角线上)。

static final int Steps = 10;

private void test() {
  int x1 = 100;
  int y1 = 100;
  int x2 = 300;
  int y2 = 500;

  double dx = (double)(x2 - x1) / (double) Steps;
  double dy = (double)(y2 - y1) / (double) Steps;

  double x = x1;
  double y = x2;
  for ( int i = 0; i < Steps; i++) {
    // Simulate the drawing of the square.
    System.out.println("("+x+","+y+")");
    x += dx;
    y += dy;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM