简体   繁体   中英

Language Agonostic - Getting all coordinates within a rectangle

So let's say I have a rectangle, 10px by 10px, with the coordinates of the top left corner at 0, 0(those numbers are arbitrary and just an example). Below is a picture of such, with every box being one pixel and the green part being the rectangle in question.

If I wanted to get a list of every coordinate inside that rectangle, how would that be done? With this rectangle in particular, it would be a list of 100 coordinates. Please tell me if the question is not clear enough.

Any help is appreciated!

If the smaller rectangles have the same size then you can use the dimensions of the rectangles to come up with the coordinates. For example, if the size of rectangle is 1*1 and the green area has only one rectangle then coordinates will be (0,0) , (0,1), (1,0) and (1,1)

If the green area has four sub rectangles in the same example then coordinates will be (0,0) , (0,1), (1,0) ,(1,1) , (2,0), (2,1) (1,2), (2,2)

You can generalize this approach

Turns out with some hard thinking(I am rather unexperienced in geometry) I was able to solve this myself. In JS:

function getRectCoordinates(x, y, width, height) {
    var coordinates = [];

    for (var h = 0; h < height; h++) {
        for (var w = 0; w < width; w++) {
            coordinates.push({x: w + x, y: h + y});
        };
    };

    return coordinates;
}

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