标签:
注:顶点坐标可以点击
1 package 2 { 3 import flash.display.Shape; 4 import flash.display.Sprite; 5 import flash.events.MouseEvent; 6 import flash.geom.Point; 7 import flash.text.TextField; 8 9 /** 10 * @author Frost.Yen 11 * @E-mail 871979853@qq.com 12 * @create 2015-8-18 下午3:16:57 13 * 14 */ 15 [SWF(width="1024",height="768")] 16 public class DrawTriangle extends Sprite 17 { 18 private var _pointArr:Array = [new Point(200,150),new Point(500,300),new Point(150,400)]; 19 private var _btn:TextField; 20 private var _container:Sprite; 21 private var _heightShape:Shape; 22 private var _pointA:TextField; 23 private var _pointB:TextField; 24 private var _pointC:TextField; 25 private var _pointD:TextField; 26 27 public function DrawTriangle() 28 { 29 initViews(); 30 initEventListeners(); 31 } 32 private function initViews():void 33 { 34 _container = new Sprite(); 35 _btn = new TextField(); 36 _heightShape = new Shape(); 37 _btn.selectable = false; 38 _btn.border = true; 39 _btn.htmlText = "点击随机生成"; 40 _btn.autoSize = "left"; 41 _btn.background = true; 42 _btn.backgroundColor = 0xdddddd; 43 _btn.x = 100; 44 _btn.y = 100; 45 _pointA = getText(_container); 46 _pointB = getText(_container); 47 _pointC = getText(_container); 48 _pointD = getText(_container); 49 this.addChild(_btn); 50 this.addChild(_container); 51 this.addChild(_heightShape); 52 } 53 private function initEventListeners():void 54 { 55 _btn.addEventListener(MouseEvent.CLICK,onCreatClick); 56 _pointA.addEventListener(MouseEvent.CLICK,onGetHeight); 57 _pointB.addEventListener(MouseEvent.CLICK,onGetHeight); 58 _pointC.addEventListener(MouseEvent.CLICK,onGetHeight); 59 } 60 private function onCreatClick(e:MouseEvent):void 61 { 62 creatTriangle(); 63 } 64 65 private function creatTriangle():void 66 { 67 _pointArr = []; 68 for(var i:int = 0;i<3;i++){ 69 var point:Point = new Point(Math.floor(Math.random()*400)+150,Math.floor(Math.random()*400+150)); 70 _pointArr.push(point); 71 } 72 _pointArr.sortOn("y"); 73 _heightShape.graphics.clear(); 74 _container.graphics.clear(); 75 _container.graphics.lineStyle(2,0xff0000); 76 _container.graphics.moveTo(_pointArr[0].x,_pointArr[0].y); 77 _container.graphics.lineTo(_pointArr[1].x,_pointArr[1].y); 78 _container.graphics.moveTo(_pointArr[1].x,_pointArr[1].y); 79 _container.graphics.lineTo(_pointArr[2].x,_pointArr[2].y); 80 _container.graphics.moveTo(_pointArr[2].x,_pointArr[2].y); 81 _container.graphics.lineTo(_pointArr[0].x,_pointArr[0].y); 82 setText(_pointD,""); 83 setText(_pointA,"A("+_pointArr[0].x+","+_pointArr[0].y+")",_pointArr[0].x,_pointArr[0].y); 84 setText(_pointB,"B("+_pointArr[1].x+","+_pointArr[1].y+")",_pointArr[1].x,_pointArr[1].y); 85 setText(_pointC,"C("+_pointArr[2].x+","+_pointArr[2].y+")",_pointArr[2].x,_pointArr[2].y); 86 } 87 private function onGetHeight(e:MouseEvent):void 88 { 89 var point:Point; 90 var start:Point; 91 switch(e.currentTarget){ 92 case _pointA: 93 point = getCrossoverByVertex(_pointArr[0],_pointArr[1],_pointArr[2]); 94 break; 95 case _pointB: 96 point = getCrossoverByVertex(_pointArr[1],_pointArr[0],_pointArr[2]); 97 break; 98 case _pointC: 99 point = getCrossoverByVertex(_pointArr[2],_pointArr[0],_pointArr[1]); 100 break; 101 } 102 setText(_pointD,"C("+point.x+","+point.y+")",point.x,point.y); 103 104 } 105 /** 106 * 获取顶点的高线与对边的交点 107 * @param vertex 顶点 108 * @param point1 三角形另外两点之一 109 * @param point2 三角形另外两点之一 110 * @return 111 */ 112 private function getCrossoverByVertex(vertex:Point,point1:Point,point2:Point):Point 113 { 114 var k1:Number;//顶点对角线的斜率(即A点对角线为BC) 115 var b1:Number;//顶点对角线的直线方程的常数 116 var k2:Number;//高所在直线的斜率 117 var b2:Number;//高所在直线方程的常数 118 var crossPoint:Point = new Point();//高线和顶点对角线的交点 119 k1 = (point2.y - point1.y)/(point2.x - point1.x); 120 b1 = point1.y - k1 * point1.x; 121 k2 = -1 / k1 ; 122 b2 = vertex.y-k2*vertex.x; 123 crossPoint.x = (b1 - b2)/(k2 - k1); 124 crossPoint.y = k1 * ((b1 - b2)/(k2 - k1)) + b1; 125 drawHeight(vertex,crossPoint); 126 return crossPoint; 127 } 128 private function drawHeight(start:Point,end:Point):void 129 { 130 _heightShape.graphics.clear(); 131 _heightShape.graphics.lineStyle(2,0xff00ff); 132 _heightShape.graphics.moveTo(start.x,start.y); 133 _heightShape.graphics.lineTo(end.x,end.y); 134 _heightShape.graphics.endFill(); 135 } 136 private function getText(parent:Sprite):TextField 137 { 138 var t:TextField = new TextField; 139 t.autoSize = "left"; 140 t.selectable = false; 141 parent.addChild(t); 142 return t; 143 } 144 private function setText(t:TextField,text:String,x:Number=0,y:Number=0):void 145 { 146 t.text = text; 147 t.x = x; 148 t.y = y; 149 } 150 } 151 }
[ActionScript 3.0] AS3 绘制任意三角形任意顶点到对边的高
标签:
原文地址:http://www.cnblogs.com/frost-yen/p/4744783.html