What is the relationship between a 2d Cartesian coordinate grid and a matrix of arrays? -
i working complex problem involves array matrix. think of matrices having row index , column index.
matrix[row][column]
but particular problem think more useful think of in context of cartesian coordinates. when doing though noticed there few distinct issues such matrix indices can positive integers opposed cartesian coordinates can span in direction. find myself confused how x , y indices map rows , columns indices.
what relationship between 2d cartesian coordinate grid , matrix of arrays?
it seems major concern of yours how represent cartesian coordinate system using java 2 dimensional array. confusing part of how deal negative coordinates since java arrays can indexed positive numbers. here class cartesiangrid
contains 2d array of integers. array can initialized range of cartesian coordinates. getter , setter offset negative coordinates map range of array java expects.
public class cartesianarray { private int[][] grid; private int minx, miny; private int sizex, sizey; public cartesianarray(int minx, int miny, int maxx, int maxy) { this.minx = minx; this.miny = miny; sizex = maxx - minx + 1; sizey = maxy - miny + 1; grid = new int[sizex][sizey]; } public void setpoint(int xcart, int ycart, int value) { int x = xcart - xmin; // offset negative x value int y = ycart - ymin; // offset negative y value // check out of bounds coordinates if (x < 0 || x >= sizex || y < 0 || y >= sizey) { throw new exception("cannot set point outside grid."); } else { grid[x][y] = value; } } public int getpoint(int xcart, int ycart) { int x = xcart - xmin; // offset negative x value int y = ycart - ymin; // offset negative y value if (x < 0 || x >= sizex || y < 0 || y >= sizey) { throw new exception("cannot point outside grid."); } else { return grid[x][y]; } } }
Comments
Post a Comment