简体   繁体   English

带网格的2D阵列

[英]2D Array with Grid

I have created a program using turtle graphics to draw different shapes. 我创建了一个使用乌龟图形绘制不同形状的程序。 There is a discrete grid underneath, 5x5, on top of the 400px by 400px JFrame. 在400px x 400px JFrame的顶部,有一个5x5下的离散网格。 It wraps around top/bottom and left/right in case the shapes go over. 它可以包裹顶部/底部和左侧/右侧,以防形状溢出。

What I now need to do is add a 2d array that creates a 400x400 array of 0s. 我现在需要做的是添加一个2d数组,该数组创建一个0x的400x400数组。 If a turtle moves through any pixels (corresponding to a point in the 2d array), the 0 needs to be changed to a 1. What is the best way to go about this? 如果乌龟移动通过任何像素(对应于2d数组中的一个点),则需要将0更改为1。解决此问题的最佳方法是什么? We're eventually working up to the functionality of Conway's Game of Life. 我们最终正在努力完善Conway的“人生游戏”的功能。

In my view the best way to go about this would be to keep track of the turtle's position (X & Y), direction with double variables, and then use basic trigonometry to calculate the next 'pixel' that the the tutrtle hits. 在我看来,解决此问题的最佳方法是使用double变量跟踪乌龟的位置(X&Y),方向,然后使用基本三角函数来计算龟背击中的下一个“像素”。 I assume you're going to be runnibg this code using a timer or loop, so it could look something like this: 我假设您将使用计时器或循环来运行此代码,因此看起来可能像这样:

//This code is somehwere in your program
class Turtle
{
     private double x;
     private double y;
     private double direction; //direction in radians
    public double getX() {
        return x;
    }
    public void setX(double x) {
        this.x = x;
    }
    public double getY() {
        return y;
    }
    public void setY(double y) {
        this.y = y;
    }
    public double getDirection() {
        return direction;
    }
    public void setDirection(double direction) {
        while(direction > Math.PI * 2)
        {
            direction -= Math.PI * 2;
        }
        while(direction < 0)
        {
            direction += Math.PI * 2;
        }
        this.direction = direction;
    }
}

private static final int GRID_WIDTH = 400;
private static final int GRID_HEIGHT = 400;


private Turtle myTurtle = new Turtle();
private boolean[][] grid = new boolean[GRID_WIDTH][GRID_HEIGHT];
JFrame myJFrame = new JFrame();
private Graphics gridImage = myJFrame.getGraphics();

private void initialise()
{
    for(int y = 0; y < GRID_HEIGHT; y++)
    {
        for(int x = 0; x < GRID_WIDTH; x++)
        {
            grid[x][y] = false;
        }
    }

    gridImage.setColor(Color.BLACK);
    gridImage.fillRect(0, 0, GRID_WIDTH, GRID_HEIGHT);
    gridImage.setColor(Color.white);
}

//This code would be inside the loop or timer callback function
private void myMainFunction()
{
    double newX, newY;
    double deltaX, deltaY;

    deltaX = Math.cos(myTurtle.getDirection());
    deltaY = Math.sin(myTurtle.getDirection());

    newX = myTurtle.getX() + deltaX;
    newY = myTurtle.getY() + deltaY;

    if(newX < 0)
    {
        newX += GRID_WIDTH; 
    }
    else if(newX > GRID_WIDTH)
    {
        newX -=  GRID_WIDTH;    
    }

    if(newY < 0)
    {
        newY += GRID_HEIGHT; 
    }
    else if(newY > GRID_HEIGHT)
    {
        newY -=  GRID_HEIGHT;   
    }

    grid[(int)Math.floor(newX)][(int)Math.floor(newY)] = true;

    gridImage.fillRect((int)Math.floor(newX), (int)Math.floor(newY), 1, 1);
    myJFrame.update(gridImage);

    myTurtle.setX(newX);
    myTurtle.setY(newY);
}

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

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