// Hi guys,
// I want to draw a dynamic arc on mouse move using Actionscript. If,
// I click and drag on stage, allow him to draw anything and in the end
// when mouse is released, i want to make that as arc.
// http://board.flashkit.com/board/showthread.php?831367-Drawing-an-arc-dynamically
package
{
import flash.display.Sprite;
import flash.geom.Point;
/**
* @author makc
* @license WTFPLv2, http://sam.zoy.org/wtfpl/
*/
public class Circle3 extends Sprite
{
public var ps:Vector.<Point>;
public function Circle3 ()
{
stage.addEventListener("mouseDown", onMouseDown);
stage.addEventListener("mouseUp", onMouseUp);
}
public function onMouseDown (...yay):void {
ps = new Vector.<Point>;
ps.push(new Point(mouseX, mouseY));
graphics.clear();
graphics.lineStyle(2, 0xFF7F);
graphics.moveTo(mouseX, mouseY);
stage.addEventListener("mouseMove", onMouseMove);
}
public function onMouseMove (...yay):void {
ps.push(new Point(mouseX, mouseY));
graphics.lineTo(mouseX, mouseY);
}
public function onMouseUp (...yarr):void {
stage.removeEventListener("mouseMove", onMouseMove);
var N:int = 0;
var C:Point = new Point;
for (var i:int = 1; i < ps.length - 1; i++) {
var A:Point = ps[0];
var B:Point = ps[ps.length - 1];
var P:Point = findCircleCenter(A, B, ps[i]);
if (P) {
C.x += P.x; C.y += P.y; N++;
}
}
if (N > 0) {
C.normalize(C.length / N);
graphics.lineStyle (2, 0xFF7F00);
graphics.drawCircle (C.x, C.y, A.subtract(C).length);
}
}
/*
* Finds circle center from three points.
* @see http://mathforum.org/library/drmath/view/54323.html
*/
public function findCircleCenter (p1:Point, p2:Point, p3:Point):Point
{
var bc:Number = (p1.length * p1.length - p2.length * p2.length) * 0.5;
var cd:Number = (p2.length * p2.length - p3.length * p3.length) * 0.5;
var det:Number = (p1.x - p2.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p2.y);
if (det == 0) {
// center at infinity
return null;
}
return new Point (
(bc * (p2.y - p3.y) - cd * (p1.y - p2.y)) / det,
(cd * (p1.x - p2.x) - bc * (p2.x - p3.x)) / det
);
}
}
}