FakeCurve
segment line to fake curve...try to simulate tiny wings ground generating
♥0 |
Line 98 |
Modified 2011-07-23 03:09:55 |
MIT License
archived:2017-03-20 09:12:24
ActionScript3 source code
/**
* Copyright faseer ( http://wonderfl.net/user/faseer )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/6w0N
*/
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Point;
public class FakeCurve extends Sprite
{
private const numVertices:int = 10;
private const segWidth:Number = 5.0;
private const minWidth:Number = 5 * segWidth;
private const maxWidth:Number = 20 * segWidth;
private const vHeight:Number = 120;
private const startX:Number = 2;
private const startY:Number = 200;
private const bgc:int = 0x000000;
private const lineClr:int = 0x6666ff;
private const curveClr:int = 0xffaaaa;
private var vertices:Vector.<Point> = new Vector.<Point>( numVertices, true );
private var segments:Vector.<Point> = new Vector.<Point>;
private var canvas:Sprite = new Sprite();
public function FakeCurve()
{
init();
refresh();
}
private function init():void
{
graphics.beginFill( bgc );
graphics.drawRect( 0,0, stage.stageWidth, stage.stageHeight );
graphics.endFill();
addChild( canvas );
stage.addEventListener( MouseEvent.CLICK, refresh );
}
private function refresh( e:MouseEvent = null ):void
{
createVertices();
segmentVertices();
drawLines();
}
private function createVertices():void
{
var x:Number = startX,
y:Number,
i:int;
for( i = 0; i < numVertices; ++i )
{
x = x + minWidth + Math.random() * ( maxWidth - minWidth );
y = startY + Math.random() * vHeight * (i&1 ? 1 : -1);
vertices[i] = new Point( x, y );
}
}
private function segmentVertices():void
{
var i:int, j:int,
w:Number, h:Number,
sx:Number, sy:Number,
seg:int,
dx:Number,
da:Number,
x:Number, y:Number;
segments.length = 0;
for( i = 1; i < numVertices; ++i )
{
sx = vertices[ i-1 ].x;
sy = vertices[ i-1 ].y;
w = vertices[ i ].x - sx;
h = vertices[ i ].y - sy;
seg = w / segWidth;
dx = w / seg;
da = Math.PI / seg;
// skip the lastest vertex which lapped the first vertex of next segment
for( j = 0; j < seg - 1; ++j )
{
x = sx + dx * j;
y = sy + h * ( 1 - Math.cos(da * j) ) * .5;
segments.push( new Point( x, y ) );
}
}
// add the lastest vertex
segments.push( new Point( vertices[ i-1 ].x, vertices[ i-1 ].y ) );
}
private function drawLines():void
{
var i:int;
canvas.graphics.clear();
// draw origin vertices
canvas.graphics.lineStyle( 1, lineClr );
canvas.graphics.moveTo( vertices[0].x, vertices[0].y );
for( i = 1; i < numVertices; ++i )
{
canvas.graphics.lineTo( vertices[i].x, vertices[i].y );
}
// draw segments to fake curve
canvas.graphics.lineStyle( 1.5, curveClr );
canvas.graphics.moveTo( segments[0].x, segments[0].y );
for( i = 1; i < segments.length; ++i )
{
canvas.graphics.lineTo( segments[i].x, segments[i].y );
}
}
}
}