summaryrefslogtreecommitdiff
path: root/c2/divide_canvas/Client.java
blob: 8ed3828efb34f89e0b147ec8d8d7a3973dbc013f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.awt.*;

public class Client {
    public static void main(String[] args) {
        // 1. Create a new picture sized 400 x 400 pixels
        // 2. Call divide divide canvas
        // 3. Save the image to display it
        Picture pic = new Picture(500,500);
        divideCanvas(pic, 4);
        pic.save("boner.jpg");
    }

    public static void divideCanvas(Picture p, int n) {
        Color[][] pixels = p.getPixels();
        divideCanvas(p, pixels, n, new Point(0, 0), new Point(p.width() - 1, p.height() - 1));
        p.setPixels(pixels);
    }

    private static void divideCanvas(Picture p, Color[][] pixels, int n, Point one, Point two) {
        // extract coordinates from points
        int x1 = one.x;
        int x2 = two.x;
        int y1 = one.y;
        int y2 = two.y;

        // make useful boundary points
        Point topLeft = new Point(x1, y1);
        Point topMiddle = new Point((x1 + x2) / 2, y1);
        // Point topRight = new Point(x2,y1);
        Point middleLeft = new Point(x1, (y1 + y2) / 2);
        Point middleMiddle = new Point(topMiddle.x, middleLeft.y);
        Point middleRight = new Point(x2, middleLeft.y);
        // Point bottomLeft = new Point(x1,y2);
        Point bottomMiddle = new Point(topMiddle.x, y2);
        Point bottomRight = new Point(x2, y2);

        // if we are actually dividing, do this
        if (n != 0) {
            // System.out.println(topLeft);
            // System.out.println(topMiddle);
            // System.out.println(middleLeft);
            // System.out.println(middleMiddle);
            // System.out.println(middleRight);
            // System.out.println(bottomMiddle);
            // System.out.println(bottomRight);

            fill(pixels, topLeft.x, middleMiddle.x, topLeft.y, middleMiddle.y);
            fill(pixels, topMiddle.x, middleRight.x, topMiddle.y, middleRight.y);
            fill(pixels, middleLeft.x, bottomMiddle.x, middleLeft.y, bottomMiddle.y);
            fill(pixels, middleMiddle.x, bottomRight.x, middleMiddle.y, bottomRight.y);

            divideCanvas(p, pixels, n - 1, topLeft, middleMiddle);
            divideCanvas(p, pixels, n - 1, topMiddle, middleRight);
            divideCanvas(p, pixels, n - 1, middleLeft, bottomMiddle);
            divideCanvas(p, pixels, n - 1, middleMiddle, bottomRight);
        } 
        
        // otherwise, just draw the border
        else {
            fill(pixels,topLeft.x,bottomRight.x,topLeft.y,bottomRight.y);
        }
    }

    public static void fill(Color[][] pixels, int x1, int x2, int y1, int y2) {
        for (int i = y1 + 1; i <= y2 - 1; i++) {
            for (int n = x1 + 1; n <= x2-1; n++) {
                pixels[i][n] = Color.WHITE;
            }
        }

        // outer borders
        for (int i = y1; i <= y2; i++) {
            pixels[i][x1] = Color.BLACK;
            pixels[i][x2] = Color.BLACK;
        }

        for (int i = x1; i <= x2; i++) {
            pixels[y1][i] = Color.BLACK;
            pixels[y2][i] = Color.BLACK;
        }
    }
}