I'm trying to generate a grid of points from its 4 corners. As this corners can be freely placed, it will look as the grid has a perspective.
I've written the following code in Processing, where corners are in clockwise order (starting at top-left)
PVector[][] setGrid(PVector[] corners, int cols, int rows) {
PVector[][] grid = new PVector[rows][cols];
for(int y = 0; y < rows; y++) {
float fY = (float)y / (rows - 1);
PVector p1 = PVector.lerp(corners[0], corners[3], fY);
PVector p2 = PVector.lerp(corners[1], corners[2], fY);
for(int x = 0; x < cols; x++) {
grid[y][x] = PVector.lerp(p1, p2, (float)x / (cols-1));
}
}
return grid;
}
This generates a grid with interpolated points, but it doesn't correspond to a perspective grid. All in-line points are equidistant, while in perspective closest points should be more separated than farthest.
I would appreciate some orientation, if possible, in Java/Processing
EDIT
To clarify my answer. I define 4 random corner points, I want to get all the points that create a perspective deformed grid. Note that because of perspective dX1 != dX2 as well as dY1 != dY2 . The code I wrote does not this effect (I know this, but I don't know how to do what I require) as points are interpolated resulting dX1 = dX2 = ... = dXi and dY1 = dY2 = ... = dYi
I've read about perspective transform, but I don't need to transform an image, I just need to get the grid points coordinates.