Showing posts with label Graphics. Show all posts
Showing posts with label Graphics. Show all posts

Actionscript 3 Dashed Line

Friday, 15 June 2007 | Labels: , | 11 comments |

Some code to draw dashed lines.

public static function dashLine(g:Graphics,x1:Number,y1:Number,x2:Number,y2:Number,onLength:Number = 0,offLength:Number = 0):void {
g.moveTo(x1,y1);
if
(offLength==0) {
g.lineTo(x2,y2);
return
;
}


var
dx:Number = x2-x1,
dy:Number = y2-y1,
lineLen:Number = Math.sqrt(dx*dx + dy*dy),
angle:Number = Math.atan2(dy, dx),
cosAngle:Number = Math.cos(angle),
sinAngle:Number = Math.sin(angle),
ondx:Number = cosAngle*onLength,
ondy:Number = sinAngle*onLength,
offdx:Number = cosAngle*offLength,
offdy:Number = sinAngle*offLength,

x:Number = x1,
y:Number = y1;


var
fullDashCountNumber:int = Math.floor(lineLen/(onLength+offLength));

for
(var i:int=0; i<fullDashCountNumber; i++){
g.lineTo(x+=ondx,y+=ondy);
g.moveTo(x+=offdx,y+=offdy);
}

var remainder:Number = lineLen - ((onLength+offLength)*fullDashCountNumber);

if (remainder>=onLength) {
g.lineTo(x+=ondx,y+=ondy);
} else {
g.lineTo(x2,y2);
}
}