简体   繁体   中英

How do I get line coordinates from scratch?

To start off, I want to say this is less of a syntax related question and more of a theory related question(Also I am working with Java, but the question/answer should be the same regardless of the language.). I want a function that has the parameters of x1, y1, x2, y2 (which are all integers). The function will take these and edit a 2d array (also integer) called pixelData[][]. Each coordinate that the line occupies will be added to pixelData in this format: pixelData[x][y];

For example: If the function worked properly and I called it with the following parameters:

int pixelData[][][];
pixelData = new int[10][10]; //grid size of 10x10;

x1 = 1;
y1 = 1;
x2 = 3;
y2 = 3;

The function would put the following values into pixel data:

pixelData[1][1] = 1;
pixelData[2][2] = 1;
pixelData[3][3] = 1;

For the math, my initial approach was to take the absolute value of x1-x2 and the absolute value of y1-y2 and put those is an int called xLength and yLength accordingly. Then I would store the ratio of the two (xLength/yLength) in a double called xyRatio. Then for every xyRatio x movements there is 1 y movement. The problem is that this only works for a handful of line angles, and mostly just produces inaccurate coordinates. For example, if the ratio is less than 1, there will be 0 change open the x axis every time the y axis changes, and other problems like that. If somebody could give me a formula or some help that would be great.

If you need any clarification on the question just ask. Thanks in advance!

PS I am aware of the drawLine function, but I do not want to simply draw the line, I want a mapping of every coordinate. If there is an existing function that does what I need it to do, then PLEASE tell me about it.

-Ampck

You need some algorithm for line rasterization .

For example, look for the most popular Bresenham's algorithm

Implementation from Rosetta code (replace plot with array entry filling)

private void drawLine(Graphics g, int x1, int y1, int x2, int y2) {
        // delta of exact value and rounded value of the dependent variable
        int d = 0;

        int dx = Math.abs(x2 - x1);
        int dy = Math.abs(y2 - y1);

        int dx2 = 2 * dx; // slope scaling factors to
        int dy2 = 2 * dy; // avoid floating point

        int ix = x1 < x2 ? 1 : -1; // increment direction
        int iy = y1 < y2 ? 1 : -1;

        int x = x1;
        int y = y1;

        if (dx >= dy) {
            while (true) {
                plot(g, x, y);
                if (x == x2)
                    break;
                x += ix;
                d += dy2;
                if (d > dx) {
                    y += iy;
                    d -= dx2;
                }
            }
        } else {
            while (true) {
                plot(g, x, y);
                if (y == y2)
                    break;
                y += iy;
                d += dx2;
                if (d > dy) {
                    x += ix;
                    d -= dy2;
                }
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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