注:顶点坐标可以点击

 package
{
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.text.TextField; /**
* @author Frost.Yen
* @E-mail [email protected]
* @create 2015-8-18 下午3:16:57
*
*/
[SWF(width="1024",height="768")]
public class DrawTriangle extends Sprite
{
private var _pointArr:Array = [new Point(200,150),new Point(500,300),new Point(150,400)];
private var _btn:TextField;
private var _container:Sprite;
private var _heightShape:Shape;
private var _pointA:TextField;
private var _pointB:TextField;
private var _pointC:TextField;
private var _pointD:TextField; public function DrawTriangle()
{
initViews();
initEventListeners();
}
private function initViews():void
{
_container = new Sprite();
_btn = new TextField();
_heightShape = new Shape();
_btn.selectable = false;
_btn.border = true;
_btn.htmlText = "点击随机生成三角形";
_btn.autoSize = "left";
_btn.background = true;
_btn.backgroundColor = 0xdddddd;
_btn.x = 100;
_btn.y = 100;
_pointA = getText(_container);
_pointB = getText(_container);
_pointC = getText(_container);
_pointD = getText(_container);
this.addChild(_btn);
this.addChild(_container);
this.addChild(_heightShape);
}
private function initEventListeners():void
{
_btn.addEventListener(MouseEvent.CLICK,onCreatClick);
_pointA.addEventListener(MouseEvent.CLICK,onGetHeight);
_pointB.addEventListener(MouseEvent.CLICK,onGetHeight);
_pointC.addEventListener(MouseEvent.CLICK,onGetHeight);
}
private function onCreatClick(e:MouseEvent):void
{
creatTriangle();
} private function creatTriangle():void
{
getPoint();
_pointArr.sortOn("y");
_heightShape.graphics.clear();
_container.graphics.clear();
_container.graphics.lineStyle(2,0xff0000);
_container.graphics.moveTo(_pointArr[0].x,_pointArr[0].y);
_container.graphics.lineTo(_pointArr[1].x,_pointArr[1].y);
_container.graphics.moveTo(_pointArr[1].x,_pointArr[1].y);
_container.graphics.lineTo(_pointArr[2].x,_pointArr[2].y);
_container.graphics.moveTo(_pointArr[2].x,_pointArr[2].y);
_container.graphics.lineTo(_pointArr[0].x,_pointArr[0].y);
setText(_pointD,"");
setText(_pointA,"A("+_pointArr[0].x+","+_pointArr[0].y+")",_pointArr[0].x,_pointArr[0].y);
setText(_pointB,"B("+_pointArr[1].x+","+_pointArr[1].y+")",_pointArr[1].x,_pointArr[1].y);
setText(_pointC,"C("+_pointArr[2].x+","+_pointArr[2].y+")",_pointArr[2].x,_pointArr[2].y);
}
private function onGetHeight(e:MouseEvent):void
{
var point:Point;
var start:Point;
switch(e.currentTarget){
case _pointA:
point = getCrossoverByVertex(_pointArr[0],_pointArr[1],_pointArr[2]);
break;
case _pointB:
point = getCrossoverByVertex(_pointArr[1],_pointArr[0],_pointArr[2]);
break;
case _pointC:
point = getCrossoverByVertex(_pointArr[2],_pointArr[0],_pointArr[1]);
break;
}
setText(_pointD,"C("+point.x+","+point.y+")",point.x,point.y); }
/**
* 随机生成三个顶点
*/
private function getPoint():void
{
_pointArr = [];
for(var i:int = 0;i<3;i++){
var point:Point = new Point(Math.floor(Math.random()*400)+150,Math.floor(Math.random()*400+150));
_pointArr.push(point);
}
var k:Number = (_pointArr[2].y-_pointArr[1].y)/(_pointArr[2].x-_pointArr[1].x);
var b:Number = _pointArr[2].y-k*_pointArr[2].x;
if(_pointArr[0].y == k*_pointArr[0].x+b ){//判断三个点是否在同一直线
trace("三个点处于同一直线");
getPoint();
}
}
/**
* 获取顶点的高线与对边的交点
* @param vertex 顶点
* @param point1 三角形另外两点之一
* @param point2 三角形另外两点之一
* @return
*/
private function getCrossoverByVertex(vertex:Point,point1:Point,point2:Point):Point
{
if(point2.y == point1.y){
return new Point(vertex.x,point1.y);
} var k1:Number;//顶点对角线的斜率(即A点对角线为BC)
var b1:Number;//顶点对角线的直线方程的常数
var k2:Number;//高所在直线的斜率
var b2:Number;//高所在直线方程的常数
var crossPoint:Point = new Point();//高线和顶点对角线的交点
k1 = (point2.y - point1.y)/(point2.x - point1.x);
b1 = point1.y - k1 * point1.x;
k2 = -1 / k1 ;
b2 = vertex.y-k2*vertex.x;
crossPoint.x = (b1 - b2)/(k2 - k1);
crossPoint.y = k1 * ((b1 - b2)/(k2 - k1)) + b1;
drawHeight(vertex,crossPoint);
return crossPoint;
}
private function drawHeight(start:Point,end:Point):void
{
_heightShape.graphics.clear();
_heightShape.graphics.lineStyle(2,0xff00ff);
_heightShape.graphics.moveTo(start.x,start.y);
_heightShape.graphics.lineTo(end.x,end.y);
_heightShape.graphics.endFill();
}
private function getText(parent:Sprite):TextField
{
var t:TextField = new TextField;
t.autoSize = "left";
t.selectable = false;
parent.addChild(t);
return t;
}
private function setText(t:TextField,text:String,x:Number=0,y:Number=0):void
{
t.text = text;
t.x = x;
t.y = y;
}
}
}
05-11 21:49