UGrid2D ugrid; float sx, sy, vx, vy;
void setup() { size(300, 200); ugrid = new UGrid2D(50, 40, 250, 160, 8, 6);
sx = 0; sy = 0; vx = 1; vy = 0.5;
render(); noLoop(); }
void draw() {}
void render() { noStroke(); fill(153); rect(0, 0, width, height);
draw_ugrid(ugrid); traverse(sx, sy, vx, vy); }
void mousePressed() { sx = mouseX; sy = mouseY; vx = vy = 0; }
void mouseDragged() { vx = mouseX - sx; vy = mouseY - sy; float l = mag(vx, vy); if (l > 0) { vx /= l; vy /= l; render(); } loop(); }
void mouseReleased() { noLoop(); }
void traverse(float sx, float sy, float vx, float vy) { UGrid2D.Iterator it = ugrid.ray_traverse(sx, sy, vx, vy); if (it != null) { stroke(0, 0, 0); fill(255, 0, 0); do { draw_cell(ugrid, it.X, it.Y); } while (it.next()); } stroke(0, 0, 0); line(sx, sy, sx + vx * 1000, sy + vy * 1000); }
void draw_ugrid(UGrid2D ugrid) { stroke(0, 0, 0); noFill(); for (int i = 0; i < ugrid.m_ny; ++i) { for (int j = 0; j < ugrid.m_nx; ++j) { draw_cell(ugrid, j, i); } } }
void draw_cell(UGrid2D ugrid, int bx, int by) { float x = ugrid.m_xmin + ugrid.m_xw * bx; float y = ugrid.m_ymin + ugrid.m_yw * by; rect(x, y, ugrid.m_xw, ugrid.m_yw); }
|