// draw N-by-N percolation system public static void draw(Percolation perc, int N) { StdDraw.clear(); StdDraw.setPenColor(StdDraw.BLACK); StdDraw.setXscale(0, N); StdDraw.setYscale(0, N); StdDraw.filledSquare(N / 2.0, N / 2.0, N / 2.0); // draw N-by-N grid int opened = 0; for (int row = 1; row <= N; row++) { for (int col = 1; col <= N; col++) { if (perc.isFull(row, col)) StdDraw.setPenColor(StdDraw.BOOK_LIGHT_BLUE); else if (perc.isOpen(row, col)) StdDraw.setPenColor(StdDraw.WHITE); else StdDraw.setPenColor(StdDraw.BLACK); StdDraw.filledSquare(col - 0.5, N - row + 0.5, 0.45); if (perc.isOpen(row, col)) opened++; } } // write status text StdDraw.setFont(new Font("SansSerif", Font.PLAIN, 12)); StdDraw.setPenColor(StdDraw.BLACK); StdDraw.text(.25 * N, -N * .025, opened + " open sites"); if (perc.percolates()) StdDraw.text(.75 * N, -N * .025, "percolates"); else StdDraw.text(.75 * N, -N * .025, "does not percolate"); }
private static void draw( boolean[][] maze, boolean[][] solution, int rowS, int colS, int rowE, int colE) { StdDraw.clear(); StdDraw.setPenColor(StdDraw.BLACK); int N = maze[0].length; StdDraw.setXscale(-1, N); StdDraw.setYscale(-1, N); for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) if (!maze[i][j]) StdDraw.filledSquare(j, N - i - 1, .5); StdDraw.setPenColor(StdDraw.RED); for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) { if (solution[i][j]) { StdDraw.filledSquare(j, N - i - 1, .4); } } StdDraw.setPenColor(StdDraw.GREEN); StdDraw.filledSquare(colS, N - rowS - 1, .4); StdDraw.setPenColor(StdDraw.YELLOW); StdDraw.filledSquare(colE, N - rowE - 1, .4); }
public static void main(String[] args) { // scale of the coordinate system StdDraw.setXscale(-1.0, 1.0); StdDraw.setYscale(-1.0, 1.0); double rx = 0.0, ry = 0.0; double rx2 = rx + 0.10, ry2 = ry + 0.10; double rx3 = rx2 + 0.10, ry3 = ry2 + 0.10; double vx = 0.005, vy = 0.003; double vx2 = vx + 0.05, vy2 = vy + 0.05; double vx3 = vx2 + 0.05, vy3 = vy2 + 0.05; double radius = 0.10; // main loop while (true) { // bounce off wall according to law of elastic collision if (Math.abs(rx + vx) > 1.0 - radius) vx = -vx; if (Math.abs(ry + vy) > 1.0 - radius) vy = -vy; if (Math.abs(rx2 + vx2) > 1.0 - radius) vx2 = -vx2; if (Math.abs(ry2 + vy2) > 1.0 - radius) vy2 = -vy2; if (Math.abs(rx3 + vx3) > 1.0 - radius) vx3 = -vx3; if (Math.abs(ry3 + vy3) > 1.0 - radius) vy3 = -vy3; // position rx += vx; ry += vy; rx2 = vx2 / Math.tan(vy / vx); ry2 = vy2 / Math.tan(vy / vx); rx3 += vx3; ry3 += vy3; // clear StdDraw.setPenColor(StdDraw.GRAY); StdDraw.filledSquare(0, 0, 1.0); // ball StdDraw.setPenColor(StdDraw.BLUE); StdDraw.filledCircle(rx, ry, radius); // ball2 StdDraw.setPenColor(StdDraw.BLACK); StdDraw.filledCircle(rx2, ry2, radius / 2); // ball3 StdDraw.setPenColor(StdDraw.YELLOW); StdDraw.filledCircle(rx3, ry3, radius / 2); // display and pause for 20 ms StdDraw.show(20); } }
/** * Draws one square (and any pieces piled there) at coordinates x, y. * * @param highlight If true, highlights this square as a legal move. */ public void drawSquare(Deque<Integer> pile, double x, double y, boolean highlight) { if (pile != null) { if (highlight) { StdDraw.setPenColor(VERY_LIGHT_BLUE); } else { StdDraw.setPenColor(LIGHT_BLUE); } StdDraw.filledSquare(x, y, 0.45); x -= 0.2; y -= 0.2; for (int c : pile) { StdDraw.setPenColor(c == FocusModel.BLACK ? java.awt.Color.BLACK : java.awt.Color.WHITE); StdDraw.filledEllipse(x, y, 0.2, 0.1); StdDraw.setPenColor(c == FocusModel.BLACK ? java.awt.Color.WHITE : java.awt.Color.BLACK); StdDraw.ellipse(x, y, 0.2, 0.1); x += 0.1; y += 0.1; } } }