/// <summary> /// constructor /// </summary> /// <param name="dimension_x">number of cells across the grid</param> /// <param name="dimension_y">number of cells down the grid</param> public grid2D(int dimension_x, int dimension_y) { cell = new grid2Dcell[dimension_x][]; for (int x = 0; x < dimension_x; x++) { cell[x] = new grid2Dcell[dimension_y]; } }
/// <summary> /// creates grid cells based upon line interception points /// </summary> private void initialiseCells() { int cells_across = line_intercepts.GetLength(0); int cells_down = line_intercepts.GetLength(1); cell = new grid2Dcell[cells_across-1][]; for (int x = 0; x < cells_across-1; x++) cell[x] = new grid2Dcell[cells_down-1]; for (int x = 0; x < cells_across - 1; x++) { for (int y = 0; y < cells_down - 1; y++) { cell[x][y] = new grid2Dcell(); cell[x][y].perimeter = new polygon2D(); cell[x][y].perimeter.Add(line_intercepts[x, y, 0], line_intercepts[x, y, 1]); cell[x][y].perimeter.Add(line_intercepts[x+1, y, 0], line_intercepts[x+1, y, 1]); cell[x][y].perimeter.Add(line_intercepts[x+1, y+1, 0], line_intercepts[x+1, y+1, 1]); cell[x][y].perimeter.Add(line_intercepts[x, y + 1, 0], line_intercepts[x, y + 1, 1]); } } }
/// <summary> /// creates grid cells based upon line interception points /// </summary> private void initialiseCells() { int cells_across = line_intercepts.GetLength(0); int cells_down = line_intercepts.GetLength(1); cell = new grid2Dcell[cells_across - 1][]; for (int x = 0; x < cells_across - 1; x++) { cell[x] = new grid2Dcell[cells_down - 1]; } for (int x = 0; x < cells_across - 1; x++) { for (int y = 0; y < cells_down - 1; y++) { cell[x][y] = new grid2Dcell(); cell[x][y].perimeter = new polygon2D(); cell[x][y].perimeter.Add(line_intercepts[x, y, 0], line_intercepts[x, y, 1]); cell[x][y].perimeter.Add(line_intercepts[x + 1, y, 0], line_intercepts[x + 1, y, 1]); cell[x][y].perimeter.Add(line_intercepts[x + 1, y + 1, 0], line_intercepts[x + 1, y + 1, 1]); cell[x][y].perimeter.Add(line_intercepts[x, y + 1, 0], line_intercepts[x, y + 1, 1]); } } }
/// <summary> /// constructor /// </summary> /// <param name="dimension_x">number of cells across the grid</param> /// <param name="dimension_y">number of cells down the grid</param> public grid2D(int dimension_x, int dimension_y) { cell = new grid2Dcell[dimension_x][]; for (int x = 0; x < dimension_x; x++) cell[x] = new grid2Dcell[dimension_y]; }
/// <summary> /// initialise the grid /// </summary> /// <param name="dimension_x"></param> /// <param name="dimension_y"></param> /// <param name="perimeter"></param> /// <param name="border_cells"></param> /// <param name="simple_orientation"></param> public void init(int dimension_x, int dimension_y, polygon2D perimeter, int border_cells, bool simple_orientation) { this.border_cells = border_cells; this.perimeter = perimeter; cell = new grid2Dcell[dimension_x][]; for (int x = 0; x < dimension_x; x++) cell[x] = new grid2Dcell[dimension_y]; line = new ArrayList[2]; float length3, length4; int index = 0; for (int i = 0; i < 2; i++) { line[i] = new ArrayList(); int idx1 = index + i; if (idx1 >= 4) idx1 -= 4; int idx2 = index + i + 1; if (idx2 >= 4) idx2 -= 4; float x0 = (float)perimeter.x_points[idx1]; float y0 = (float)perimeter.y_points[idx1]; float x1 = (float)perimeter.x_points[idx2]; float y1 = (float)perimeter.y_points[idx2]; int next_idx1 = idx1 + 1; if (next_idx1 >= 4) next_idx1 -= 4; length3 = perimeter.getSideLength(next_idx1); float w0 = Math.Abs(x1 - x0); float h0 = Math.Abs(y1 - y0); int idx3 = index + i + 2; if (idx3 >= 4) idx3 -= 4; int idx4 = index + i + 3; if (idx4 >= 4) idx4 -= 4; float x2 = (float)perimeter.x_points[idx3]; float y2 = (float)perimeter.y_points[idx3]; float x3 = (float)perimeter.x_points[idx4]; float y3 = (float)perimeter.y_points[idx4]; int next_idx3 = next_idx1 + 2; if (next_idx3 >= 4) next_idx3 -= 4; length4 = perimeter.getSideLength(next_idx3); float w1 = Math.Abs(x3 - x2); float h1 = Math.Abs(y3 - y2); int dimension = dimension_x; if (!simple_orientation) { if (i > 0) dimension = dimension_y; } else { if (h0 > w0) dimension = dimension_y; } // how much shorter is one line than the other on the opposite axis? float shortening = 0; //if (h0 > w0) { if (length3 > length4) shortening = (length3 - length4) / length3; else shortening = (length4 - length3) / length4; } for (int j = -border_cells; j <= dimension + border_cells; j++) { // locate the position along the first line float xx0, yy0; // position along the first line float fraction = j / (float)dimension; // modify for foreshortening //if ((h0 > w0) && (shortening > 0)) if (shortening > 0) { fraction = (fraction * (1.0f - shortening)) + ((float)Math.Sin(fraction * (float)Math.PI / 2) * shortening); if (length3 > length4) fraction = 1.0f - fraction; } if (w0 > h0) { float grad = (y1 - y0) / (x1 - x0); if (x1 > x0) xx0 = x0 + (w0 * fraction); else xx0 = x0 - (w0 * fraction); yy0 = y0 + ((xx0 - x0) * grad); } else { float grad = (x1 - x0) / (y1 - y0); if (y1 > y0) yy0 = y0 + (h0 * fraction); else yy0 = y0 - (h0 * fraction); xx0 = x0 + ((yy0 - y0) * grad); } // locate the position along the second line float xx1, yy1; // position along the second line if (w1 > h1) { float grad = (y2 - y3) / (x2 - x3); if (x2 > x3) xx1 = x3 + (w1 * fraction); else xx1 = x3 - (w1 * fraction); yy1 = y3 + ((xx1 - x3) * grad); } else { float grad = (x2 - x3) / (y2 - y3); if (y2 > y3) yy1 = y3 + (h1 * fraction); else yy1 = y3 - (h1 * fraction); xx1 = x3 + ((yy1 - y3) * grad); } // add the line to the list line[i].Add(xx0); line[i].Add(yy0); line[i].Add(xx1); line[i].Add(yy1); } } // find interceptions between lines poltLineIntercepts(); // create grid cells initialiseCells(); }
/// <summary> /// initialise the grid /// </summary> /// <param name="dimension_x"></param> /// <param name="dimension_y"></param> /// <param name="perimeter"></param> /// <param name="border_cells"></param> /// <param name="simple_orientation"></param> public void init(int dimension_x, int dimension_y, polygon2D perimeter, int border_cells, bool simple_orientation) { this.border_cells = border_cells; this.perimeter = perimeter; cell = new grid2Dcell[dimension_x][]; for (int x = 0; x < dimension_x; x++) { cell[x] = new grid2Dcell[dimension_y]; } line = new ArrayList[2]; float length3, length4; int index = 0; for (int i = 0; i < 2; i++) { line[i] = new ArrayList(); int idx1 = index + i; if (idx1 >= 4) { idx1 -= 4; } int idx2 = index + i + 1; if (idx2 >= 4) { idx2 -= 4; } float x0 = (float)perimeter.x_points[idx1]; float y0 = (float)perimeter.y_points[idx1]; float x1 = (float)perimeter.x_points[idx2]; float y1 = (float)perimeter.y_points[idx2]; int next_idx1 = idx1 + 1; if (next_idx1 >= 4) { next_idx1 -= 4; } length3 = perimeter.getSideLength(next_idx1); float w0 = Math.Abs(x1 - x0); float h0 = Math.Abs(y1 - y0); int idx3 = index + i + 2; if (idx3 >= 4) { idx3 -= 4; } int idx4 = index + i + 3; if (idx4 >= 4) { idx4 -= 4; } float x2 = (float)perimeter.x_points[idx3]; float y2 = (float)perimeter.y_points[idx3]; float x3 = (float)perimeter.x_points[idx4]; float y3 = (float)perimeter.y_points[idx4]; int next_idx3 = next_idx1 + 2; if (next_idx3 >= 4) { next_idx3 -= 4; } length4 = perimeter.getSideLength(next_idx3); float w1 = Math.Abs(x3 - x2); float h1 = Math.Abs(y3 - y2); int dimension = dimension_x; if (!simple_orientation) { if (i > 0) { dimension = dimension_y; } } else { if (h0 > w0) { dimension = dimension_y; } } // how much shorter is one line than the other on the opposite axis? float shortening = 0; //if (h0 > w0) { if (length3 > length4) { shortening = (length3 - length4) / length3; } else { shortening = (length4 - length3) / length4; } } for (int j = -border_cells; j <= dimension + border_cells; j++) { // locate the position along the first line float xx0, yy0; // position along the first line float fraction = j / (float)dimension; // modify for foreshortening //if ((h0 > w0) && (shortening > 0)) if (shortening > 0) { fraction = (fraction * (1.0f - shortening)) + ((float)Math.Sin(fraction * (float)Math.PI / 2) * shortening); if (length3 > length4) { fraction = 1.0f - fraction; } } if (w0 > h0) { float grad = (y1 - y0) / (x1 - x0); if (x1 > x0) { xx0 = x0 + (w0 * fraction); } else { xx0 = x0 - (w0 * fraction); } yy0 = y0 + ((xx0 - x0) * grad); } else { float grad = (x1 - x0) / (y1 - y0); if (y1 > y0) { yy0 = y0 + (h0 * fraction); } else { yy0 = y0 - (h0 * fraction); } xx0 = x0 + ((yy0 - y0) * grad); } // locate the position along the second line float xx1, yy1; // position along the second line if (w1 > h1) { float grad = (y2 - y3) / (x2 - x3); if (x2 > x3) { xx1 = x3 + (w1 * fraction); } else { xx1 = x3 - (w1 * fraction); } yy1 = y3 + ((xx1 - x3) * grad); } else { float grad = (x2 - x3) / (y2 - y3); if (y2 > y3) { yy1 = y3 + (h1 * fraction); } else { yy1 = y3 - (h1 * fraction); } xx1 = x3 + ((yy1 - y3) * grad); } // add the line to the list line[i].Add(xx0); line[i].Add(yy0); line[i].Add(xx1); line[i].Add(yy1); } } // find interceptions between lines poltLineIntercepts(); // create grid cells initialiseCells(); }