forked from: ブレゼンハム
forked from ブレゼンハム (diff: 192)
ちょうど、BitmapDataに直線、曲線を引くアルゴリズムを考えていたのでforkさせていただきました。
ActionScript3 source code
/**
* Copyright shohei909 ( http://wonderfl.net/user/shohei909 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/laen
*/
// forked from hacker_v1lx13we's ブレゼンハム
/*
ちょうど、BitmapDataに直線、曲線を引くアルゴリズムを考えていたのでforkさせていただきました。
*/
package {
import flash.geom.ColorTransform;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.geom.Rectangle;
import frocessing.color.ColorHSV;
[SWF(width="465", height="465", backgroundColor="0x000000", framerate="60")]
public class Bresenham extends Sprite {
private var canvas:BitmapData;
private var pen:Pen = new Pen(0,RabBit.sphere(10,0xF000000));
private var color:ColorHSV = new ColorHSV( 0, 0.8, 1 );
private var colorTransform:ColorTransform = new ColorTransform(0.99,0.99,0.99);
public function Bresenham() {
canvas = new BitmapData(465, 465, false, 0x000000);
addChild(new Bitmap(canvas));
addEventListener( "enterFrame", onFrame );
}
private function onFrame( e:Event ):void{
canvas.lock();
for(var i:int = 0; i < 1; i++ ){
color.h += 10;
pen.color = color.value;
pen.drawLine( canvas, Math.random() * 465 , Math.random() * 465 , Math.random() * 465 , Math.random() * 465 );
}
canvas.colorTransform( canvas.rect, colorTransform );
canvas.unlock();
}
}
}
import flash.geom.Matrix;
import flash.geom.Rectangle;
import flash.geom.ColorTransform;
import flash.display.BitmapData;
class Pen{
public var shape:BitmapData;
private var _colorTransform:ColorTransform = new ColorTransform();
private var _color:uint;
public function set color( u:uint ):void{ _color = u; _colorTransform.color = u; }
public function get color():uint{ return _color; }
function Pen( color:uint = 0xFF000000, shape:BitmapData = null ){
_colorTransform.color = color;
_color = color;
this.shape = shape;
}
public function drawLine( bitmapData:BitmapData, x1:Number, y1:Number, x2:Number, y2:Number ):void {
if( shape ){
x1 -= shape.width / 2;
x2 -= shape.width / 2;
y1 -= shape.height / 2;
y2 -= shape.height / 2;
var mtr:Matrix = new Matrix(1,0,0,1,x1,y1)
bitmapData.draw( shape, mtr, _colorTransform )
}else{
bitmapData.setPixel( x1, y1, _color );
}
var a:Number = Math.abs(y2 - y1);
var b:Number = Math.abs(x2 - x1);
var dx:int = (x2 >= x1) ? 1 : -1;
var dy:int = (y2 >= y1) ? 1 : -1;
var swap:Boolean = (a > b);
if (swap == true) {
a = Math.abs(x2 - x1);
b = Math.abs(y2 - y1);
}
var df1:int = ((b - a) << 1);
var df2:int = -(a << 1);
var d:int = b - (a << 1);
if (swap == true) {
if( shape ){
while (y1 >>> 0 != y2 >>> 0) {
y1 += dy;
if (d < 0) { x1 += dx; d += df1; }
else { d += df2; }
mtr.tx = x1; mtr.ty = y1;
bitmapData.draw( shape, mtr, _colorTransform );
}
} else {
while (y1>>>0 != y2>>>0) {
y1 += dy;
if (d < 0) { x1 += dx; d += df1; }
else { d += df2; }
bitmapData.setPixel32( x1, y1, _color );
}
}
} else {
if( shape ){
while (x1>>>0 != x2>>>0) {
x1 += dx;
if (d < 0) { y1 += dy; d += df1; }
else { d += df2; }
mtr.tx = x1; mtr.ty = y1;
bitmapData.draw( shape, mtr, _colorTransform );
}
}else{
while (x1>>>0 != x2>>>0) {
x1 += dx;
if (d < 0) { y1 += dy; d += df1; }
else { d += df2; }
bitmapData.setPixel32( x1, y1, _color );
}
}
}
}
}
class RabBit{
static public function sphere(r:int, color:uint = 0x000000 ):BitmapData {
var r2:Number = r*2;
var rr:Number = r*r
var b:BitmapData = new BitmapData(r2,r2,true,0);
b.lock();
for( var x:int = 0; x<r2; x++ ){
for( var y:int = 0; y<r2; y++ ){
var dx:Number = r - x;
var dy:Number = r - y;
var dr:Number = Math.sqrt(dx * dx + dy * dy);
if( dr < r ){
var alpha:uint = (r - dr) / r * 10;
b.setPixel32( x, y, color + (alpha << 24) );
}
}
}
b.unlock();
return b;
}
}