@@ -5,7 +5,7 @@ int rectWidth = 20, rectHeight = 20;
5
5
int MAX_DIVISOR = 10 ; // maximum modulo divisor that should be used
6
6
int MAX_N_ROWS = 675 ; // max number of rows which should be displayed
7
7
8
- String [][] binomials = new String [0 ][];
8
+ int [][][] binomials = new int [ MAX_DIVISOR ] [0 ][];
9
9
10
10
PImage imgControls; // controls instruction image
11
11
@@ -71,9 +71,9 @@ void drawBoxes(int nRows, int mod)
71
71
{
72
72
// As computing the binomial coefficients is slow, only compute it once a
73
73
// 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);
77
77
}
78
78
79
79
// Drawing the boxes
@@ -83,17 +83,13 @@ void drawBoxes(int nRows, int mod)
83
83
float x = startX + j * rectWidth;
84
84
float y = rectHeight * i;
85
85
86
- int colorNum = int ( Long . parseLong( binomials[i - 1 ][j]) % mod) ;
86
+ int colorNum = binomials[mod][ i - 1 ][j];
87
87
88
88
int filler = int (map (colorNum, 0 , mod - 1 , 0 , 250 ));
89
89
int fillerR = int (map (colorNum, 0 , mod - 1 , 128 , 250 ));
90
90
int fillerG = int (map (colorNum, 0 , mod - 1 , 128 , 250 ));
91
91
int fillerB = int (map (colorNum, 0 , mod - 1 , 128 , 250 ));
92
92
93
- if (colorNum < 0 )
94
- // print("\ni: " + i + ", j: " + j + " " + int(binomials[i - 1][j]));
95
- ;
96
-
97
93
switch (colorMode) {
98
94
case 0 :
99
95
fill (filler);
@@ -119,7 +115,7 @@ void drawBoxes(int nRows, int mod)
119
115
fill (0 );
120
116
textAlign (CENTER , CENTER );
121
117
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);
123
119
}
124
120
}
125
121
}
@@ -130,7 +126,7 @@ void drawBoxes(int nRows, int mod)
130
126
131
127
}
132
128
133
- void generateBinomials (int nRows , String [][] binomTbl ) {
129
+ void generateBinomials (int nRows , int mod , int [] [][] binomTbl ) {
134
130
// Generates Pascal's triangle which looks like this:
135
131
// 1
136
132
// 1 1
@@ -142,25 +138,25 @@ void generateBinomials(int nRows, String[][] binomTbl) {
142
138
// Very slow, should be improved further, with bigger data types to compute more rows
143
139
144
140
// 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 };
147
143
148
144
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 ];
151
147
152
148
// Add a "1" to start and end
153
- row[0 ] = " 1 " ;
154
- row[prevLen] = " 1 " ;
149
+ row[0 ] = 1 ;
150
+ row[prevLen] = 1 ;
155
151
156
152
// Fill up the values in between with the sum of the two values (left, right) of the previous row
157
153
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 ;
161
157
}
162
158
163
- binomTbl[i] = row;
159
+ binomTbl[mod][ i] = row;
164
160
}
165
161
}
166
162
@@ -214,4 +210,4 @@ void zoom(float scaling) {
214
210
rectWidth *= scaling;
215
211
rectHeight *= scaling;
216
212
}
217
- }
213
+ }
0 commit comments