git @ Cat's Eye Technologies yoob.js / 00041ff
Add .map() method to Playfield. catseye 12 years ago
1 changed file(s) with 29 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
187187 };
188188
189189 /*
190 * Analogous to (monoid) map in functional languages,
191 * iterate over this Playfield, transform each value using
192 * a supplied function, and write the transformed value into
193 * a destination Playfield.
194 *
195 * Supplied function should take a Playfield (this Playfield),
196 * x, and y, and return a value.
197 *
198 * The map source may extend beyond the internal bounds of
199 * the Playfield, by giving the min/max Dx/Dy arguments
200 * (which work like margin offsets.)
201 *
202 * Useful for evolving a cellular automaton playfield. In this
203 * case, min/max Dx/Dy should be computed from the neighbourhood.
204 */
205 this.map = function(destPf, fun, minDx, minDy, maxDx, maxDy) {
206 if (minDx === undefined) minDx = 0;
207 if (minDy === undefined) minDy = 0;
208 if (maxDx === undefined) maxDx = 0;
209 if (maxDy === undefined) maxDy = 0;
210 for (var y = this.minY + minDy; y <= this.maxY + maxDy; y++) {
211 for (var x = this.minX + minDx; x <= this.maxX + maxDx; x++) {
212 destPf.putDirty(x, y, fun(pf, x, y));
213 }
214 }
215 destPf.recalculateBounds();
216 };
217
218 /*
190219 * Draws elements of the Playfield in a drawing context.
191220 * x and y are canvas coordinates, and width and height
192221 * are canvas units of measure.