| 1 |
/* Copyright (C) 2001-2007 Peter Selinger and nitoyon. |
|---|
| 2 |
Original code(Potrace v1.8) by Peter Selinger. |
|---|
| 3 |
Ported to ActionScript 3.0 by nitoyon. |
|---|
| 4 |
This file is part of PotrAs. It is free software and it is covered |
|---|
| 5 |
by the GNU General Public License. See the file COPYING for details. */ |
|---|
| 6 |
|
|---|
| 7 |
package com.nitoyon.potras |
|---|
| 8 |
{ |
|---|
| 9 |
import flash.geom.Point; |
|---|
| 10 |
import flash.display.Graphics; |
|---|
| 11 |
|
|---|
| 12 |
public class ClosedPath |
|---|
| 13 |
{ |
|---|
| 14 |
/** |
|---|
| 15 |
* |
|---|
| 16 |
*/ |
|---|
| 17 |
public var $a:Array; |
|---|
| 18 |
|
|---|
| 19 |
/** |
|---|
| 20 |
* Constructor. |
|---|
| 21 |
* |
|---|
| 22 |
* initialize the members of the given curve structure to size m. |
|---|
| 23 |
* Return 0 on success, 1 on error with errno set. |
|---|
| 24 |
*/ |
|---|
| 25 |
public function ClosedPath(array:Array = null):void |
|---|
| 26 |
{ |
|---|
| 27 |
$a = array || []; |
|---|
| 28 |
} |
|---|
| 29 |
|
|---|
| 30 |
/** |
|---|
| 31 |
* draw |
|---|
| 32 |
*/ |
|---|
| 33 |
public function draw(g:Graphics):void |
|---|
| 34 |
{ |
|---|
| 35 |
var pt:Point = $a[$a.length - 1].c[2]; |
|---|
| 36 |
g.moveTo(pt.x, pt.y); |
|---|
| 37 |
|
|---|
| 38 |
for(var i:int = 0; i < $a.length; i++) |
|---|
| 39 |
{ |
|---|
| 40 |
var c:Curve = $a[i]; |
|---|
| 41 |
|
|---|
| 42 |
if(c.tag == ProcessPath.POTRACE_CORNER) |
|---|
| 43 |
{ |
|---|
| 44 |
g.lineTo(c.c[1].x, c.c[1].y); |
|---|
| 45 |
g.lineTo(c.c[2].x, c.c[2].y); |
|---|
| 46 |
} |
|---|
| 47 |
else |
|---|
| 48 |
{ |
|---|
| 49 |
for(var t:Number = 0; t < 1.0; t += 0.02) |
|---|
| 50 |
{ |
|---|
| 51 |
var p:Point = getBezierPoint(pt, c.c[0], c.c[1], c.c[2], t); |
|---|
| 52 |
g.lineTo(p.x, p.y); |
|---|
| 53 |
} |
|---|
| 54 |
g.lineTo(c.c[2].x, c.c[2].y); |
|---|
| 55 |
} |
|---|
| 56 |
pt = c.c[2]; |
|---|
| 57 |
} |
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
/** |
|---|
| 61 |
* Get quad bezier curve point. |
|---|
| 62 |
*/ |
|---|
| 63 |
private function getBezierPoint(p0:Point, p1:Point, p2:Point, p3:Point, t:Number):Point |
|---|
| 64 |
{ |
|---|
| 65 |
return new Point(Math.pow(1 - t, 3) * p0.x + 3 * t * Math.pow(1 - t, 2) * p1.x |
|---|
| 66 |
+ 3 * t * t * (1 - t) * p2.x + t * t * t * p3.x, |
|---|
| 67 |
Math.pow(1 - t, 3) * p0.y + 3 * t * Math.pow(1 - t, 2) * p1.y |
|---|
| 68 |
+ 3 * t * t * (1 - t) * p2.y + t * t * t * p3.y); |
|---|
| 69 |
} |
|---|
| 70 |
} |
|---|
| 71 |
} |
|---|