[ActionScript 3.0] AS3 绘制任意三角形任意顶点到对边的高

注:顶点坐标可以点击

  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 [email protected]
 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 }
时间: 2024-10-06 00:31:01

[ActionScript 3.0] AS3 绘制任意三角形任意顶点到对边的高的相关文章

[ActionScript 3.0] AS3 绘制立方体

1 package 2 { 3 import flash.display.Sprite; 4 import flash.events.Event; 5 import flash.geom.Vector3D; 6 import flash.text.TextField; 7 import flash.text.TextFormat; 8 9 /** 10 * @author Frost.Yen 11 * @E-mail [email protected] 12 * @create 2015-9-2

[ActionScript 3.0] AS3 绘制星形

1 package 2 { 3 import flash.display.Sprite; 4 import flash.events.Event; 5 6 /** 7 * @author Frost.Yen 8 * @E-mail [email protected] 9 * @create 2015-9-9 下午4:47:50 10 * 11 */ 12 [SWF(width="1024",height="1024",frameRate="5")

[ActionScript 3.0] AS3 绘制正八面体(线条)

分析: 将八面体置于3D坐标系中,其中心的坐标位于原点(0,0,0),让八面体的六个顶点恰好位于3D坐标系的x轴.y轴和z轴上,则从八面体的中心到这六个顶点的距离是相等的.我们可以假设这个距离为r,则六个顶点的坐标分别为上(0,-r,0).下(0,r,0).四周:左(-r,0,0).右(r,0,0).前(0,0,-r).后(0,0,r). 1 package 2 { 3 import flash.display.MovieClip; 4 import flash.display.Sprite;

[ActionScript 3.0] AS3 绘制正四面体(线条)

1 package 2 { 3 import flash.display.MovieClip; 4 import flash.display.Sprite; 5 import flash.events.Event; 6 import flash.geom.Point; 7 import flash.geom.Vector3D; 8 9 /** 10 * @author Frost.Yen 11 * @E-mail [email protected] 12 * @create 2015-9-8 上

[ActionScript 3.0] AS3 3D双圆环贴图

1 package 2 { 3 import flash.display.Bitmap; 4 import flash.display.BitmapData; 5 import flash.display.MovieClip; 6 import flash.display.Sprite; 7 import flash.display.TriangleCulling; 8 import flash.events.Event; 9 import flash.geom.ColorTransform;

[ActionScript 3.0] as3处理xml的功能和遍历节点

as3比as2处理xml的功能增强了N倍,获取或遍历节点非常之方便,类似于json对像的处理方式. XML 的一个强大功能是它能够通过文本字符的线性字符串提供复杂的嵌套数据.将数据加载到 XML 对象时,ActionScript 会分析数据并将其分层结构加载到内存(如果 XML 数据格式有误,它会发送运行时错误). 利用 XML 和 XMLList 对象的运算符和方法可以轻松遍历 XML 数据的结构. 1.读取外部 XML 文档 可以使用 URLLoader 类从 URL 加载 XML 数据.若

[ActionScript 3.0] AS3虚线绘制方法

import flash.geom.Point; import flash.display.MovieClip; import flash.display.Graphics; var mc:MovieClip=new MovieClip(); addChild(mc); drawDashed(mc,new Point(100,100),new Point(500,100),5,5); function drawDashed(mc:Sprite,p1:Point,p2:Point,length:N

[ActionScript 3.0] AS3 深入理解Flash的安全沙箱Security Domains

简介 如果你还没有与复杂的的安全域(security domain)和应用程序域(application domain)问题打过交道,那么你真是个幸运的家伙.当你在加载外部内容(然后他们开始播放)的时候,默认的设置工作的很好,你甚至不知道他们的存在. 但是某些时候你可能需要控制默认设置以外的更多行为和功能,这样你就会遇到前面所说的问题.你也许会困扰于Security.allowDomain和crossdomain.xml文件的区别,又或者你想要深究关于安全性的最佳实践.如果是这样,那么这篇文章就

[ActionScript 3.0] AS3.0和AS2.0的相互通信

AS3和AS2之间的通信,最好的方式可能就是LocalConnection了. AS2向AS3发送数据,即AS2调用AS3的函数: as2.0代码(按钮上写的发送信息代码): on (release) { var param = "this message is from as2"; var caller:LocalConnection = new LocalConnection(); caller.send("AS2 send to AS3","funI