Created
March 25, 2017 12:18
-
-
Save tai2/243af4210e644266b625501e46c5e9df to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package { | |
import flash.display.Sprite; | |
import flash.display.Bitmap; | |
import flash.display.BitmapData; | |
import flash.events.Event; | |
public class MunchingSquare extends Sprite { | |
private var canvas:BitmapData; | |
private var canvasBitmap:Bitmap; | |
private var n:int = 1; | |
private var d:int = 1; | |
public function MunchingSquare() { | |
canvas = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0x000000); | |
canvasBitmap = new Bitmap(canvas); | |
addChild(canvasBitmap); | |
addEventListener(Event.ENTER_FRAME, enterFrame); | |
} | |
private function enterFrame(event:Event) : void { | |
canvas.lock(); | |
var noplot:int = 0; | |
var plot:int = 0; | |
var x:int; | |
var y:int; | |
for (y = 0; y < stage.stageHeight; y++) { | |
for (x = 0; x < stage.stageWidth; x++) { | |
if ((x^y) < n) { | |
var v:int = 1 < n ? 255 * (n - 1 - (x^y)) / (n - 1) : 0; | |
canvas.setPixel(x, y, (v<<16) | (v<<8) | (v<<0)); | |
plot = 1 | |
} else { | |
canvas.setPixel(x, y, 0x000000); | |
noplot = 1; | |
} | |
} | |
} | |
canvas.unlock(); | |
n += d; | |
if ((d == 1 && !noplot) || (d == -1) && !plot) { | |
d = -d; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment