Skip to content

Commit 2fa3808

Browse files
committed
that code was horrible, made it at least a tiny bit better
1 parent b365fe0 commit 2fa3808

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

pascal.pde

+18-22
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ int rectWidth = 20, rectHeight = 20;
55
int MAX_DIVISOR = 10; //maximum modulo divisor that should be used
66
int MAX_N_ROWS = 675; //max number of rows which should be displayed
77

8-
String[][] binomials = new String[0][];
8+
int[][][] binomials = new int[MAX_DIVISOR][0][];
99

1010
PImage imgControls; //controls instruction image
1111

@@ -71,9 +71,9 @@ void drawBoxes(int nRows, int mod)
7171
{
7272
//As computing the binomial coefficients is slow, only compute it once a
7373
//bigger row count than the current length is requested
74-
if (nRows > binomials.length) {
75-
binomials = new String[nRows][];
76-
generateBinomials(nRows, binomials);
74+
if (nRows > binomials[mod].length) {
75+
binomials[mod] = new int[nRows][];
76+
generateBinomials(nRows, mod, binomials);
7777
}
7878

7979
//Drawing the boxes
@@ -83,17 +83,13 @@ void drawBoxes(int nRows, int mod)
8383
float x = startX + j * rectWidth;
8484
float y = rectHeight * i;
8585

86-
int colorNum = int(Long.parseLong(binomials[i - 1][j]) % mod);
86+
int colorNum = binomials[mod][i - 1][j];
8787

8888
int filler = int(map(colorNum, 0, mod - 1, 0, 250));
8989
int fillerR = int(map(colorNum, 0, mod - 1, 128, 250));
9090
int fillerG = int(map(colorNum, 0, mod - 1, 128, 250));
9191
int fillerB = int(map(colorNum, 0, mod - 1, 128, 250));
9292

93-
if (colorNum < 0)
94-
//print("\ni: " + i + ", j: " + j + " " + int(binomials[i - 1][j]));
95-
;
96-
9793
switch(colorMode) {
9894
case 0:
9995
fill(filler);
@@ -119,7 +115,7 @@ void drawBoxes(int nRows, int mod)
119115
fill(0);
120116
textAlign(CENTER, CENTER);
121117
textSize(rectHeight / 2);
122-
text(binomials[i - 1][j], x,y, rectWidth, rectHeight);
118+
text(String.valueOf(binomials[mod][i - 1][j]), x,y, rectWidth, rectHeight);
123119
}
124120
}
125121
}
@@ -130,7 +126,7 @@ void drawBoxes(int nRows, int mod)
130126

131127
}
132128

133-
void generateBinomials(int nRows, String[][] binomTbl) {
129+
void generateBinomials(int nRows, int mod, int[][][] binomTbl) {
134130
//Generates Pascal's triangle which looks like this:
135131
//1
136132
//1 1
@@ -142,25 +138,25 @@ void generateBinomials(int nRows, String[][] binomTbl) {
142138
//Very slow, should be improved further, with bigger data types to compute more rows
143139

144140
//binomTbl is an array of string arrays in which the binomial coefficients are stored
145-
binomTbl[0] = new String[]{"1"};
146-
binomTbl[1] = new String[]{"1","1"};
141+
binomTbl[mod][0] = new int[]{1};
142+
binomTbl[mod][1] = new int[]{1,1};
147143

148144
for (int i = 2; i < nRows; ++i) {
149-
int prevLen = binomTbl[i - 1].length;
150-
String[]row = new String[prevLen + 1];
145+
int prevLen = binomTbl[mod][i - 1].length;
146+
int[]row = new int[prevLen + 1];
151147

152148
//Add a "1" to start and end
153-
row[0] = "1";
154-
row[prevLen] = "1";
149+
row[0] = 1;
150+
row[prevLen] = 1;
155151

156152
//Fill up the values in between with the sum of the two values (left, right) of the previous row
157153
for (int j = 0; j < prevLen - 1; ++j) {
158-
long left = Long.parseLong(binomTbl[i - 1][j] + "");
159-
long right = Long.parseLong(binomTbl[i - 1][j + 1] + "");
160-
row[1 + j] = Long.toString(left + right);
154+
int left = binomTbl[mod][i - 1][j];
155+
int right = binomTbl[mod][i - 1][j + 1];
156+
row[1 + j] = (left + right)%mod;
161157
}
162158

163-
binomTbl[i] = row;
159+
binomTbl[mod][i] = row;
164160
}
165161
}
166162

@@ -214,4 +210,4 @@ void zoom(float scaling) {
214210
rectWidth *= scaling;
215211
rectHeight *= scaling;
216212
}
217-
}
213+
}

0 commit comments

Comments
 (0)