在二维数组中创建一个三角形

时间:2015-10-03 21:34:11

标签: java arrays

所以我想在java中创建0的数组[64] [64]并在这里创建三角形。我的意思是:

0001000
0010100
0100010
1111111

所以我创建了课程点:

public class Point {
    public int x;
    public int y;

    Point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}

我正以这种方式在主要的新点对象中创建:

int amountOfPowers = 6;
Point a = new Point((int) (Math.pow(2, amountOfPowers))/2, 0);
     Point b = new Point((int) Math.pow(2, amountOfPowers), (int) (Math.pow(2, amountOfPowers) * Math.sqrt(3.0) / 2));
     Point c = new Point(0, (int) (Math.pow(2, amountOfPowers) * Math.sqrt(3.0) / 2));

所以A是(0, 64),B是(64, 64),C是(32, 55)。有人知道如何将数组中从0,64到64,64的所有位置写为1? (像[0][64] = 1, [1][64] = 1, ..., [64][64] = 1)。 还有如何从(0,64)到(32,55)和从(32,55)到(64,64)创建一行。有人知道我该怎么办?

@edit

我试过用它: https://en.wikipedia.org/wiki/Line_drawing_algorithm

这是我的方法:

static void drawLine(Point from, Point to) {
        // https://en.wikipedia.org/wiki/Line_drawing_algorithm
        int dx = to.x - from.x;
        int dy = to.y - from.y;

        for (int x = from.x; x < to.x; x++) {
            int y = from.y + dx * (x - from.x) / dy;
            System.out.println("X: " + x + " Y: " + y);
            canvas[x][y] = 1;
        }
    }

我在main中写道:

drawLine(a, b);
drawLine(b, c);
drawLine(c, a);

这是我的结果:/这是错的。

0000000000000000000000000000000000000000000000000000000100000000
0000000000000000000000000000000000000000000000000000000100000000
0000000000000000000000000000000000000000000000000000001000000000
0000000000000000000000000000000000000000000000000000001000000000
0000000000000000000000000000000000000000000000000000010000000000
0000000000000000000000000000000000000000000000000000010000000000
0000000000000000000000000000000000000000000000000000100000000000
0000000000000000000000000000000000000000000000000001000000000000
0000000000000000000000000000000000000000000000000001000000000000
0000000000000000000000000000000000000000000000000010000000000000
0000000000000000000000000000000000000000000000000010000000000000
0000000000000000000000000000000000000000000000000100000000000000
0000000000000000000000000000000000000000000000000100000000000000
0000000000000000000000000000000000000000000000001000000000000000
0000000000000000000000000000000000000000000000010000000000000000
0000000000000000000000000000000000000000000000010000000000000000
0000000000000000000000000000000000000000000000100000000000000000
0000000000000000000000000000000000000000000000100000000000000000
0000000000000000000000000000000000000000000001000000000000000000
0000000000000000000000000000000000000000000010000000000000000000
0000000000000000000000000000000000000000000010000000000000000000
0000000000000000000000000000000000000000000100000000000000000000
0000000000000000000000000000000000000000000100000000000000000000
0000000000000000000000000000000000000000001000000000000000000000
0000000000000000000000000000000000000000001000000000000000000000
0000000000000000000000000000000000000000010000000000000000000000
0000000000000000000000000000000000000000100000000000000000000000
0000000000000000000000000000000000000000100000000000000000000000
0000000000000000000000000000000000000001000000000000000000000000
0000000000000000000000000000000000000001000000000000000000000000
0000000000000000000000000000000000000010000000000000000000000000
0000000000000000000000000000000000000100000000000000000000000000
1000000000000000000000000000000000000000000000000000000000000000
1000000000000000000000000000000000000000000000000000000000000000
0100000000000000000000000000000000000000000000000000000000000000
0100000000000000000000000000000000000000000000000000000000000000
0010000000000000000000000000000000000000000000000000000000000000
0010000000000000000000000000000000000000000000000000000000000000
0001000000000000000000000000000000000000000000000000000000000000
0000100000000000000000000000000000000000000000000000000000000000
0000100000000000000000000000000000000000000000000000000000000000
0000010000000000000000000000000000000000000000000000000000000000
0000010000000000000000000000000000000000000000000000000000000000
0000001000000000000000000000000000000000000000000000000000000000
0000001000000000000000000000000000000000000000000000000000000000
0000000100000000000000000000000000000000000000000000000000000000
0000000010000000000000000000000000000000000000000000000000000000
0000000010000000000000000000000000000000000000000000000000000000
0000000001000000000000000000000000000000000000000000000000000000
0000000001000000000000000000000000000000000000000000000000000000
0000000000100000000000000000000000000000000000000000000000000000
0000000000010000000000000000000000000000000000000000000000000000
0000000000010000000000000000000000000000000000000000000000000000
0000000000001000000000000000000000000000000000000000000000000000
0000000000001000000000000000000000000000000000000000000000000000
0000000000000100000000000000000000000000000000000000000000000000
0000000000000100000000000000000000000000000000000000000000000000
0000000000000010000000000000000000000000000000000000000000000000
0000000000000001000000000000000000000000000000000000000000000000
0000000000000001000000000000000000000000000000000000000000000000
0000000000000000100000000000000000000000000000000000000000000000
0000000000000000100000000000000000000000000000000000000000000000
0000000000000000010000000000000000000000000000000000000000000000
0000000000000000001000000000000000000000000000000000000000000000

有人知道如何解决我的问题? :/

1 个答案:

答案 0 :(得分:0)

如果您将阵列视为一个屏幕,并将大约1分为暗像素和零作为白色像素,则需要在屏幕上绘制一条线。实际上,正是在视频卡中积极执行的任务。

算法在这里表达https://en.wikipedia.org/wiki/Line_drawing_algorithm

注意:在算法中,他们使用int&due和舍入问题,如果你将交换coords并使用y而不是x迭代,你可能会有更好的结果。