[ActionScript 3.0] AS3 双A字模型

时间:2022-01-24 12:51:16
 package
{
import caurina.transitions.Tweener; import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.utils.setTimeout; /**
* @author Frost.Yen
* @E-mail 871979853@qq.com
* @create 2015-8-27 下午4:48:05
*
*/
[SWF(width="1024",height="800",backgroundColor="0x000000")]
public class Main extends Sprite
{
private var _overlapBtn:TextField;
private var _separateBtn:TextField;
private var _resetBtn:TextField;
private var _overlapContainer:Sprite;
private var _separateContainer:Sprite;
private var _part1:Sprite;
private var _part2:Sprite;
private var _dotA:Sprite;
private var _dotD:Sprite;
private var _dotE:Sprite;
private var _dotF:Sprite;
private var _tA:TextField;
private var _tB:TextField;
private var _tC:TextField;
private var _tD:TextField;
private var _tE:TextField;
private var _tF:TextField;
private var _tG:TextField;
private var _tA1:TextField;
private var _tB1:TextField;
private var _tC1:TextField;
private var _tD1:TextField;
private var _tE1:TextField;
private var _tF1:TextField;
private var _tG1:TextField;
private var __tA1:TextField;
private var __tC1:TextField;
private var __tE1:TextField;
private var _ratioABD:Number;//AB/AD比率
private var _ratioDEF:Number;//DE/DF比率
private var _tempB:Point;
private var _tempG:Point;
private var _dragDot:String="A";
private var _tf:TextFormat = new TextFormat("Times New Roman",26,0xffffff,true);
private var A:Point = new Point();
private var B:Point = new Point();
private var C:Point = new Point();
private var D:Point = new Point();
private var E:Point = new Point();
private var F:Point = new Point();
private var G:Point = new Point();
private var AE:Sprite = new Sprite();
private var BG:Sprite = new Sprite();
private var AD:Sprite = new Sprite();
private var DF:Sprite = new Sprite();
private var AF:Sprite = new Sprite();
private var BG_drag:Sprite = new Sprite();
public function Main()
{
initViews();
initEventListeners();
}
private function initViews():void
{
_overlapContainer = new Sprite();
_separateContainer = new Sprite();
_part1 = new Sprite();
_part2 = new Sprite();
_overlapBtn = getButton("overlap",this,50,100);
_separateBtn = getButton("separate",this,50,160);
_resetBtn = getButton("reset",this,50,220);
_tA = getTextField("A",_overlapContainer);
_tB = getTextField("B",_overlapContainer);
_tC = getTextField("C",_overlapContainer);
_tD = getTextField("D",_overlapContainer);
_tE = getTextField("E",_overlapContainer);
_tF = getTextField("F",_overlapContainer);
_tG = getTextField("G",_overlapContainer);
_tA1 = getTextField("A",_part1);
_tB1 = getTextField("B",_part1);
_tC1 = getTextField("C",_part1);
_tD1 = getTextField("D",_part1);
_tE1 = getTextField("E",_part1);
_tF1 = getTextField("F",_part2);
_tG1 = getTextField("G",_part2);
__tA1 = getTextField("A",_part2);
__tC1 = getTextField("C",_part2);
__tE1 = getTextField("E",_part2);
_dotA = getDot(4,_overlapContainer,"A");
_dotD = getDot(4,_overlapContainer,"D");
_dotF = getDot(4,_overlapContainer,"F");
_dotE = getDot(4,_overlapContainer,"E");
_separateContainer.addChild(_part1);
_separateContainer.addChild(_part2);
_overlapContainer.addChildAt(BG_drag,0);
_overlapContainer.addChildAt(_dotA,0);
_overlapContainer.addChildAt(AE,0);
_overlapContainer.addChildAt(BG,0);
_overlapContainer.addChildAt(AD,0);
_overlapContainer.addChildAt(DF,0);
_overlapContainer.addChildAt(AF,0); this.addChild(_overlapContainer);
this.addChild(_separateContainer);
BG_drag.alpha = 0;
}
private function initEventListeners():void
{
_overlapBtn.addEventListener(MouseEvent.CLICK,onOverlap);
_separateBtn.addEventListener(MouseEvent.CLICK,onSeparate);
_resetBtn.addEventListener(MouseEvent.CLICK,onReset);
_dotE.addEventListener(MouseEvent.MOUSE_DOWN,onDotEDown);
_dotA.addEventListener(MouseEvent.MOUSE_DOWN,onDotDown);
_dotD.addEventListener(MouseEvent.MOUSE_DOWN,onDotDown);
_dotF.addEventListener(MouseEvent.MOUSE_DOWN,onDotDown);
BG_drag.addEventListener(MouseEvent.MOUSE_DOWN,onBGDown);
reset();
} private function onOverlap(e:MouseEvent):void
{
Tweener.removeAllTweens();
overlap(_part1,_part2);
setTimeout(hidOverlap,800);
function hidOverlap():void{
if(_part1.x == _overlapContainer.x){
_separateContainer.visible = false;
_overlapContainer.visible = true;
}
}
}
private function onSeparate(e:MouseEvent):void
{
Tweener.removeAllTweens();
_part1.visible = true;
_part2.visible = true;
_separateContainer.visible = true;
_overlapContainer.visible = false;
drawPart();
separate(_part1,_part2);
}
private function onReset(e:MouseEvent):void
{
_part1.x = _part2.x = _overlapContainer.x;
_part1.y = _part2.y = _overlapContainer.y;
_separateContainer.visible = true;
_overlapContainer.visible = false;
reset();
}
private function onDotDown(e:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_UP,onDotUp);
stage.addEventListener(MouseEvent.MOUSE_MOVE,onDotMove);
e.currentTarget.startDrag();
_dragDot = e.currentTarget.name;
}
private function onDotMove(e:MouseEvent):void
{
drawAll();
}
private function onDotUp(e:MouseEvent):void
{
stopDrag();
stage.removeEventListener(MouseEvent.MOUSE_UP,onDotUp);
stage.removeEventListener(MouseEvent.MOUSE_MOVE,onDotMove);
drawAll();
}
private function onDotEDown(e:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_UP,onDotEUp);
stage.addEventListener(MouseEvent.MOUSE_MOVE,onDotEMove);
}
private function onDotEMove(e:MouseEvent):void
{
MoveAE();
}
private function MoveAE():void
{
var k_DF:Number = (D.y-F.y)/(D.x-F.x);//DF的k即BG的k
var b_DF:Number = D.y - k_DF * D.x;//DF的b
var k_AE:Number = (A.y-mouseY)/(A.x-mouseX);
var b_AE:Number = mouseY - k_AE * mouseX;
if(D.x == F.x){
E.x = D.x;
E.y = mouseY;
}else{
E.x = (b_AE - b_DF) / (k_DF - k_AE);
E.y = k_AE * E.x + b_AE;
} if (!isBetween(E,D,F)) {
if(Point.distance(D,E)<Point.distance(E,F)){
E.x = D.x;
E.y = D.y;
}else{
E.x = F.x;
E.y = F.y;
}
}
drawLine(A,E,AE);
_dotE.x = E.x;
_dotE.y = E.y;
setCoords();
}
private function onDotEUp(e:MouseEvent):void
{
_ratioDEF = Point.distance(D,E) / Point.distance(D,F);
stage.removeEventListener(MouseEvent.MOUSE_UP,onDotEUp);
stage.removeEventListener(MouseEvent.MOUSE_MOVE,onDotEMove);
}
private function onBGDown(e:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_UP,onBGUp);
stage.addEventListener(MouseEvent.MOUSE_MOVE,onBGMove);
}
private function onBGMove(e:MouseEvent):void
{
drawLineBG("BG");
}
private function onBGUp(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_UP,onBGUp);
stage.removeEventListener(MouseEvent.MOUSE_MOVE,onBGMove);
}
private function drawLine(startP:Point,endP:Point,sp:Sprite,thickness:Number = 2):void
{
sp.graphics.clear();
sp.graphics.lineStyle(thickness,0xffffff);
sp.graphics.moveTo(startP.x,startP.y);
sp.graphics.lineTo(endP.x,endP.y);
}
private function drawLineBG(dot:String):void
{
var k_AD:Number = (A.y-D.y)/(A.x-D.x);
var b_AD:Number = A.y - k_AD * A.x;
var k_AF:Number = (A.y-F.y)/(A.x-F.x);
var b_AF:Number = A.y - k_AF * A.x;
var k_DF:Number = (D.y-F.y)/(D.x-F.x);//DF的k即BG的k
var b_DF:Number = D.y - k_DF * D.x;//DF的b
var b_BG:Number; if(A.x == D.x||A.x==F.x||D.x==F.x||A.x == C.x){
trace("无斜率");
return;
}
if(A.y == k_DF*A.x+b_DF ){//判断三个点是否在同一直线
trace("三个顶点处于同一直线,不是三角形");
return;
}
if (dot == "F") {//拖动F
//B.x = (B.y-b_AD)/k_AD;
B.x = _ratioABD*(D.x-A.x)+A.x;
b_BG = B.y - k_DF * B.x;
G.x = (b_BG - b_AF) / (k_AF - k_DF);
G.y = k_DF * G.x + b_BG; drawLine(B,G,BG);
drawLine(B,G,BG_drag,4);
} else if (dot == "D") {//拖动D
//G.x = (G.y-b_AF)/k_AF;
G.x = _ratioABD*(F.x-A.x)+A.x;
b_BG = G.y - k_DF * G.x;
B.x = (b_BG - b_AD) / (k_AD - k_DF);
B.y = k_DF * B.x + b_BG;
drawLine(B,G,BG);
drawLine(B,G,BG_drag,4);
} else if (dot == "A") {
B.y = _ratioABD*(D.y-A.y)+A.y;
//B.x = (B.y-b_AD)/k_AD;
B.x = _ratioABD*(D.x-A.x)+A.x;
b_BG = B.y - k_DF * B.x;
G.x = (b_BG - b_AF) / (k_AF - k_DF);
G.y = k_DF * G.x + b_BG;
drawLine(B,G,BG);
drawLine(B,G,BG_drag,4);
} else {//拖动BG
b_BG = mouseY - k_DF * mouseX;
B.x = (b_BG - b_AD) / (k_AD - k_DF);
B.y = k_DF * B.x + b_BG;
G.x = (b_BG - b_AF) / (k_AF - k_DF);
G.y = k_DF * G.x + b_BG;
if (!isBetween(B,D,A)) {
if(Point.distance(A,B)>Point.distance(D,B)){
B.x = D.x;
B.y = D.y;
G.x = F.x;
G.y = F.y;
}else{
B.x = _tempB.x;
B.y = _tempB.y;
G.x = _tempG.x;
G.y = _tempG.y;
}
}
drawLine(B,G,BG);
drawLine(B,G,BG_drag,4);
_ratioABD = Point.distance(A,B) / Point.distance(A,D);
setCoords();
}
if(Point.distance(B,G)>2){
_tempB = new Point(B.x,B.y);
_tempG = new Point(G.x,G.y);
} E.y = _ratioDEF*(F.y-D.y)+D.y;
E.x = _ratioDEF*(F.x-D.x)+D.x;
_dotE.x = E.x;
_dotE.y = E.y;
} private function reset():void
{
_dotA.x = 625;
_dotA.y = 335;
_dotD.x = 523;
_dotD.y = 513;
_dotE.x = 598;
_dotE.y = 513;
_dotF.x = 690;
_dotF.y = 513;
_dragDot = "A";
setCoords();
_ratioABD = Point.distance(A,getB(428)) / Point.distance(A,D);
_ratioDEF = Point.distance(D,E) / Point.distance(D,F);
_separateContainer.visible = true;
_overlapContainer.visible = false;
drawAll();
drawPart();
separate(_part1,_part2);
}
private function drawAll():void{
drawLine(A,D,AD);
drawLine(D,F,DF);
drawLine(A,F,AF);
drawLineBG(_dragDot);
drawLine(A,E,AE);
setCoords();
}
private function setCoords():void
{
A.x = _dotA.x;
A.y = _dotA.y;
D.x = _dotD.x;
D.y = _dotD.y;
E.x = _dotE.x;
E.y = _dotE.y;
F.x = _dotF.x;
F.y = _dotF.y;
_tA.x = getPointByExtension(getMidpoint(D,F),A).x-_tA.width*0.5;
_tA.y = getPointByExtension(getMidpoint(D,F),A).y-_tA.height*0.5;;
_tB.x = getPointByExtension(G,B,25).x-_tA.width*0.5;
_tB.y = getPointByExtension(G,B,25).y-_tA.height*0.5;;
var extensionC:Point = getPointByExtension(getMidpoint(A,G),getC(),30);
getC().x > G.x ? _tC.x = extensionC.x + 5-_tA.width*0.5:_tC.x = extensionC.x - 5-_tA.width*0.5;;
getC().y > G.y ? _tC.y = extensionC.y + 5-_tA.height*0.5:_tC.y = extensionC.y - 5-_tA.height*0.5;;
_tD.x = getPointByExtension(getMidpoint(A,F),D).x-_tA.width*0.5;
_tD.y = getPointByExtension(getMidpoint(A,F),D).y-_tA.height*0.5;
_tE.x = getPointByExtension(A,E).x-_tA.width*0.5;
_tE.y = getPointByExtension(A,E).y-_tA.height*0.5;
_tF.x = getPointByExtension(getMidpoint(A,D),F).x-_tA.width*0.5;
_tF.y = getPointByExtension(getMidpoint(A,D),F).y-_tA.height*0.5;
_tG.x = getPointByExtension(B,G,25).x-_tA.width*0.5;
_tG.y = getPointByExtension(B,G,25).y-_tA.height*0.5; }
private function drawPart():void
{
_part1.graphics.clear();
_part2.graphics.clear();
drawPartLine(A,D,_part1);
drawPartLine(D,E,_part1);
drawPartLine(A,E,_part1);
drawPartLine(B,C,_part1);
drawPartLine(A,F,_part2);
drawPartLine(F,E,_part2);
drawPartLine(A,E,_part2);
drawPartLine(G,C,_part2); _tA1.x = getPointByExtension(getMidpoint(D,F),A).x-_tA.width*0.5;
_tA1.y = getPointByExtension(getMidpoint(D,F),A).y-_tA.height*0.5;
_tB1.x = getPointByExtension(G,B,25).x-_tA.width*0.5;
_tB1.y = getPointByExtension(G,B,25).y-_tA.height*0.5;
var extensionC:Point = getPointByExtension(getMidpoint(A,G),getC(),30);
getC().x > G.x ? _tC1.x = extensionC.x + 5-_tA.width*0.5:_tC1.x = extensionC.x - 5-_tA.width*0.5;
getC().y > G.y ? _tC1.y = extensionC.y + 5-_tA.height*0.5:_tC1.y = extensionC.y - 5-_tA.height*0.5;;
_tD1.x = getPointByExtension(getMidpoint(A,F),D).x-_tA.width*0.5;
_tD1.y = getPointByExtension(getMidpoint(A,F),D).y-_tA.height*0.5;;
_tE1.x = getPointByExtension(A,E).x-_tA.width*0.5;
_tE1.y = getPointByExtension(A,E).y-_tA.height*0.5;;
_tF1.x = getPointByExtension(getMidpoint(A,D),F).x-_tA.width*0.5;
_tF1.y = getPointByExtension(getMidpoint(A,D),F).y-_tA.height*0.5;;
_tG1.x = getPointByExtension(B,G,25).x-_tA.width*0.5;
_tG1.y = getPointByExtension(B,G,25).y-_tA.height*0.5;; __tA1.x = _tA1.x;
__tA1.y = _tA1.y;
__tC1.x = _tC1.x;
__tC1.y = _tC1.y;
__tE1.x = _tE1.x;
__tE1.y = _tE1.y; function drawPartLine(p1:Point,p2:Point,sp:Sprite):void
{
sp.graphics.lineStyle(2,0xffffff);
sp.graphics.moveTo(p1.x,p1.y);
sp.graphics.lineTo(p2.x,p2.y);
}
}
private function overlap(sp1:Sprite,sp2:Sprite):void
{
Tweener.addTween(sp1,{x:this.x,y:this.y,time:0.8,transition:"linear"});
Tweener.addTween(sp2,{x:this.x,y:this.y,time:0.8,transition:"linear"});
}
private function separate(sp1:Sprite,sp2:Sprite):void
{
var k_DF:Number = (D.y-F.y)/(D.x-F.x);//DF的k即BG的k
var b_DF:Number = D.y - k_DF * D.x;//DF的b
var oldY:Number = E.y;
var oldX:Number = E.x;
var offX1:Number;
var offX2:Number;
var offY1:Number;
var offY2:Number; if (k_DF<1&&k_DF>-1) {//以x方向为标准
if (D.x < F.x) {
offX1 = -39;
offX2 = 38;
} else {
offX1 = 39;
offX2 = -38;
}
oldY = k_DF * (oldX + offX1) + b_DF;
offY1 = oldY - E.y;
Tweener.addTween(sp1,{x:this.x+offX1,y:this.y+offY1,time:0.8,transition:"linear"});
oldY = k_DF * (oldX + offX2) + b_DF;
offY2 = oldY - E.y;
Tweener.addTween(sp2,{x:this.x+offX2,y:this.y+offY2,time:0.8,transition:"linear"});
} else {
if (D.y < F.y) {
offY1 = -39;
offY2 = 38;
} else {
offY1 = 39;
offY2 = -38;
}
oldX = (oldY+offY1-b_DF)/k_DF;
offX1 = oldX - E.x;
Tweener.addTween(sp1,{x:this.x+offX1,y:this.y+offY1,time:0.8,transition:"linear"});
oldX = (oldY+offY2-b_DF)/k_DF;
offX2 = oldX - E.x;
Tweener.addTween(sp2,{x:this.x+offX2,y:this.y+offY2,time:0.8,transition:"linear"});
}
} private function getB(y:Number):Point
{
var k_AD:Number = (A.y-D.y)/(A.x-D.x);
var b_AD:Number = A.y - k_AD * A.x;
if (A.x == D.x) {
B.x = A.x;
B.y = y;
} else {
B.y = y;
B.x = (B.y-b_AD)/k_AD;
} return B;
}
private function getC():Point
{
if(B.x == G.x||A.x == E.x) return C;
var k1:Number = (B.y-G.y)/(B.x-G.x);
var b1:Number = B.y - k1 * B.x; var k2:Number = (A.y-E.y)/(A.x-E.x);
var b2:Number = A.y - k2 * A.x;
if(k1==k2){
C.y = A.y;
C.x = E.x;
return C;
}
if (B.x == G.x) {
C.x = B.x;
C.y = B.y;
return C;
} else {
if (A.x == E.x) {
C.x = A.x;
C.y = k1 * C.x + b1;
} else {
C.x = (b2 - b1) / (k1 - k2);
C.y = k2 * C.x + b2;
}
return C;
} C.x = (b2 - b1) / (k1 - k2);
C.y = k2 * C.x + b2; return C;
}
/**
* 获取线段延长线上的某点point
* @param p1 线段上较point的近端点
* @param p2 线段上较point的远端点
* @param r point近端点到point的距离
* @return
*/
private function getPointByExtension(p1:Point,p2:Point,r:Number = 20):Point
{
var point:Point = new Point();
var a:Number = Math.atan2(p2.y - p1.y,p2.x - p1.x);
point.y = r * Math.sin(a) + p2.y;
var k:Number = (p1.y-p2.y)/(p1.x-p2.x);
var b:Number = p1.y - k * p1.x;
if(p1.x==p2.x){
point.x = p1.x;
return point;
}
if (p1.y == p2.y) {
p2.x>p1.x?(point.x = p2.x+20):point.x = p2.x-20;
point.y = p2.y;
return point;
}
if (p1.x == p2.x) {
point.x = p2.x;
p2.y > p1.y ? point.y = p2.y + 20:point.y = p2.y - 20;
}
point.x = (point.y-b)/k;
return point;
}
/**
* 获取线段的中点
* @param p1
* @param p2
* @return
*/
private function getMidpoint(p1:Point,p2:Point):Point
{
return new Point(p1.x+(p2.x-p1.x)*0.5,p1.y+(p2.y-p1.y)*0.5);
}
/**
* 同一直线上的三个点,判断p是否在p1和p2之间
* @param p
* @param p1
* @param p2
* @return
*/
private function isBetween(p:Point,p1:Point,p2:Point):Boolean
{
if (Point.distance(p1,p) < Point.distance(p1,p2) && Point.distance(p2,p) < Point.distance(p1,p2)) {
return true;
}
return false;
}
private function getTextField(text:String,parent:Sprite,x:Number=0,y:Number=0):TextField
{
var textField:TextField = new TextField();
textField.autoSize = "left";
textField.selectable = false;
textField.mouseEnabled = false;
_tf.italic = true;
textField.defaultTextFormat = _tf;
textField.text = text;
textField.x = x;
textField.y = y;
parent.addChild(textField);
return textField;
}
private function getButton(text:String,parent:Sprite,x:Number=0,y:Number=0):TextField
{
var textButton:TextField = new TextField();
textButton.width = 110;
textButton.height = 38;
textButton.selectable = false;
textButton.border = true;
textButton.borderColor = 0x999999;
textButton.background = true;
textButton.backgroundColor = 0x333333;
_tf.align = TextFormatAlign.CENTER;
textButton.defaultTextFormat = _tf;
textButton.htmlText = "<a href='event:#'>"+text+"</a>";
textButton.x = x;
textButton.y = y;
parent.addChild(textButton);
return textButton;
}
private function getDot(radius:Number,parent:Sprite,name:String="",x:Number=0,y:Number=0):Sprite
{
var dot:Sprite = new Sprite();
dot.graphics.lineStyle(2,0xffffff);
dot.graphics.beginFill(0x00ff00);
dot.graphics.drawCircle(0,0,radius);
dot.graphics.endFill();
dot.x = x;
dot.y = y;
dot.name = name;
parent.addChild(dot);
return dot;
}
}
}