summaryrefslogtreecommitdiff
path: root/c2/mondrian/Mondrian.java
blob: 398a60e25a9cf9b3bf6d34fdf1b12c0e0d099e98 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import java.awt.*;
import java.util.Random;

public class Mondrian {

    private int canvasWidth;
    private int canvasHeight;
    private static final int MIN_REGION_SIZE = 10;
    private Random rand;

    public void paintBasicMondrian(Color[][] pixels) {
        rand = new Random();
        int width = pixels[0].length;
        int height = pixels.length;
        this.canvasWidth = width;
        this.canvasHeight = height;

        divideCanvas(pixels, 0, width, 0, height);
    }

    public void paintComplexMondrian(Color[][] pixels) {

    }

    private void divideCanvas(Color[][] pixels, int x1, int x2, int y1, int y2) {
    if (x2 - x1 >= MIN_REGION_SIZE * 3 && y2 - y1 >= MIN_REGION_SIZE * 3) {
        //int hSplit = (x2 - x1) / 2 + x1;
        //int vSplit = (y2 - y1) / 2 + y1;

        int hSplit = rand.nextInt(x1+1, x2-10);
        int vSplit = rand.nextInt(y1+1, y2-10);

        //if (x2 - x1 >= canvasWidth / 4) {
        //int rangeX = (x2 - x1 - MIN_REGION_SIZE * 2) / MIN_REGION_SIZE;
        //    hSplit = rand.nextInt(rangeX) * MIN_REGION_SIZE + x1 + MIN_REGION_SIZE;
        //    hSplit = Math.min(hSplit, x2 - MIN_REGION_SIZE); // Ensure hSplit is within canvas bounds
        //    hSplit = Math.max(hSplit, x1 + MIN_REGION_SIZE); // Ensure hSplit is at least MIN_REGION_SIZE away from the canvas edge
        //}

        //if (y2 - y1 >= canvasHeight / 4) {
        //    int rangeY = (y2 - y1 - MIN_REGION_SIZE * 2) / MIN_REGION_SIZE;
        //    vSplit = rand.nextInt(rangeY) * MIN_REGION_SIZE + y1 + MIN_REGION_SIZE;
        //    vSplit = Math.min(vSplit, y2 - MIN_REGION_SIZE); // Ensure vSplit is within canvas bounds
        //    vSplit = Math.max(vSplit, y1 + MIN_REGION_SIZE); // Ensure vSplit is at least MIN_REGION_SIZE away from the canvas edge
        //}

        //// Additional constraints for the horizontal splitting points to prevent rectangles wider than canvasWidth/4
        //if (x2 - x1 >= canvasWidth / 4) {
        //    hSplit = Math.min(hSplit, x1 + (x2 - x1) / 2); // Ensure hSplit is not farther than halfway across the canvas
        //}

        fill(pixels, x1, x2, y1, y2);

        if (x2 - x1 >= canvasWidth / 4 && y2 - y1 >= canvasHeight / 4) {
            divideCanvas(pixels, x1, hSplit, y1, vSplit);
            divideCanvas(pixels, hSplit, x2, y1, vSplit);
            divideCanvas(pixels, x1, hSplit, vSplit, y2);
            divideCanvas(pixels, hSplit, x2, vSplit, y2);
        } else if (x2 - x1 >= canvasWidth / 4) {
            divideCanvas(pixels, x1, hSplit, y1, y2);
            divideCanvas(pixels, hSplit, x2, y1, y2);
        } else if (y2 - y1 >= canvasHeight / 4) {
            divideCanvas(pixels, x1, x2, y1, vSplit);
            divideCanvas(pixels, x1, x2, vSplit, y2);
        }
    } else {
        fill(pixels, x1, x2, y1, y2);
    }
}


    private void fill(Color[][] pixels, int x1, int x2, int y1, int y2) {
        Color region = getRandomColor();
        for (int i = y1; i < y2; i++) {
            pixels[i][x1] = Color.BLACK;
            pixels[i][x2 - 1] = Color.BLACK;
        }

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

        for (int i = y1 + 1; i < y2 - 1; i++) {
            for (int j = x1 + 1; j < x2 - 1; j++) {
                pixels[i][j] = region;
            }
        }
    }


    private Color getRandomColor() {
        Color[] colors = {Color.RED, Color.YELLOW, Color.CYAN, Color.WHITE};
        return colors[rand.nextInt(4)];
    }
}