用程序画出任意一个顶点所在的高,三角形由程序在某一个范围内随机生成
解析:
1、题目解析:已知三角形三个顶点的坐标例如A(x1,y1),B(x2,y2),C(x3,y3),画出任意一个顶点的高线,所有必须求出高另一个点(这里我们叫H点)的坐标,然后两个点连成线。
2、逻辑解析:要计算出高线的垂直点的坐标必须用一个方程组。
3、原理解析:根据数学里面的知识,已知两个顶点(例如B、C)的坐标,即可计算出这两个点连成线的直线方程 y = kx + b;其中 k 指的是直线方程中的斜率,b是一个未知常数,因为知道B、C两点的坐标,所以可以计算出直线BC的直线方程,
那么,垂直于直线BC的斜率就是:-1/k,所以经过A点的高线(即直线:AH)的斜率是:-1/k,所以,直线AH的直线方程就是 y = (-1/k)*x + b1,这里的 b1 我们可以根据顶点A的坐标计算出来。
ok,现在知道直线 BC 的直线方程,和直线 AH 的直线方程,H 点是这两条直线的交点,通过这两条直线方程,组成的方程组 即可计算出 H 点的坐标了。
package
{
import flash.display.DisplayObjectContainer;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.text.TextField;
/**
* @author chb
* @QQ 645338868
* @data 2015-8-25
*/
[SWF(width = "1024",height="768",frameRate="60")]
public class Main extends Sprite
{
private var triangleSp:Sprite;
private var heightSp:Sprite;
private var pointTagSp:Sprite;
private var pointArr:Array = new Array();
private var clickCreatHeight:TextField;
public function Main()
{
init();
}
private function init():void
{
triangleSp = new Sprite();
heightSp = new Sprite();
pointTagSp = new Sprite();
this.addChild(triangleSp);
this.addChild(heightSp);
this.addChild(pointTagSp);
clickCreatHeight = new TextField();
clickCreatHeight.text = "点击随机画顶点的高线";
clickCreatHeight.autoSize = "left";
clickCreatHeight.x = 10;
clickCreatHeight.y = stage.stageHeight - 50;
clickCreatHeight.selectable = false;
clickCreatHeight.border = true;
clickCreatHeight.addEventListener(MouseEvent.CLICK,onCreatHeight);
this.addChild(clickCreatHeight);
getRandomTriangle();
}
private function onCreatHeight(e:MouseEvent):void
{
var r:Number = Math.random()*10;
if(r<4)
{
drawHeight(pointArr[0],getHeightPoint("A"));
getText(heightSp,"D("+getHeightPoint("A").x+","+getHeightPoint("A").y+")",getHeightPoint("A").x,getHeightPoint("A").y);
}
if(r>=4 && r<6)
{
drawHeight(pointArr[1],getHeightPoint("B"));
getText(heightSp,"D("+getHeightPoint("B").x+","+getHeightPoint("B").y+")",getHeightPoint("B").x,getHeightPoint("B").y);
}
if(r>=6 && r<10)
{
drawHeight(pointArr[2],getHeightPoint("C"));
getText(heightSp,"D("+getHeightPoint("C").x+","+getHeightPoint("C").y+")",getHeightPoint("C").x,getHeightPoint("C").y);
}
}
private function getRandomTriangle():void
{
var _pointA:Point = new Point();
var _pointB:Point = new Point();
var _pointC:Point = new Point();
for(var i:Number = 0;i<3;i++)
{
switch(i)
{
case 0:
_pointA = getRandomPoint();
break;
case 1:
_pointB = getRandomPoint();
if(matchPoint(_pointA,_pointB))
{
//假如两个点是一样的话,跳回去再次执行
i = i - 1;
}
break;
case 2:
_pointC = getRandomPoint();
if(matchPoint(_pointA,_pointB,_pointC))
{
//假如三个点在同一直线,跳回去再次执行
i = i - 1;
}
break;
default:
break;
}
if(i == 2)
{
pointArr.push(_pointA,_pointB,_pointC);
var _shape:Shape = new Shape();
_shape.graphics.clear();
_shape.graphics.lineStyle(1,0xff00ff,1);
_shape.graphics.moveTo(_pointA.x,_pointA.y);
_shape.graphics.lineTo(_pointB.x,_pointB.y);
_shape.graphics.moveTo(_pointB.x,_pointB.y);
_shape.graphics.lineTo(_pointC.x,_pointC.y);
_shape.graphics.moveTo(_pointC.x,_pointC.y);
_shape.graphics.lineTo(_pointA.x,_pointA.y);
triangleSp.addChild(_shape);
getText(pointTagSp,"A("+_pointA.x+","+_pointA.y+")",_pointA.x,_pointA.y);
getText(pointTagSp,"B("+_pointB.x+","+_pointB.y+")",_pointB.x,_pointB.y);
getText(pointTagSp,"C("+_pointC.x+","+_pointC.y+")",_pointC.x,_pointC.y);
}
}
}
private function matchPoint(_point1:Point,_point2:Point,_point3:Point = null):Boolean
{
if(_point1.x == _point2.x && _point1.y == _point2.y)
{
return true;
}
if(_point3)
{
var k:Number = (_point2.y - _point1.y)/(_point2.x - _point1.x);//直线方程 y = kx + b 中k的值 也叫斜率
var b:Number = _point1.y - k*_point1.x;
//trace(k,b);
if(_point3.y == k*_point3.x + b)
{
//三个点不能再同一直线上
return true;
}
}
return false;
}
private function drawHeight(_point1:Point,_point2:Point):void
{
while(heightSp.numChildren)
{
heightSp.removeChildAt(0);
}
var _heightShape:Shape = new Shape();
_heightShape.graphics.clear();
_heightShape.graphics.lineStyle(1,0x000000,1);
_heightShape.graphics.moveTo(_point1.x,_point1.y);
_heightShape.graphics.lineTo(_point2.x,_point2.y);
heightSp.addChild(_heightShape);
}
private function getHeightPoint(selectPoint:String):Point
{
//获取高点坐标
var _pointD:Point = new Point();
var k:Number;
var k1:Number;
var b:Number;
var b1:Number;
switch(selectPoint)
{
case "A":
k = (pointArr[2].y - pointArr[1].y)/(pointArr[2].x - pointArr[1].x);
b = pointArr[1].y - k*pointArr[1].x;
k1 = (-1)/k;
b1 = pointArr[0].y - k1*pointArr[0].x;
break;
case "B":
k = (pointArr[2].y - pointArr[0].y)/(pointArr[2].x - pointArr[0].x);
b = pointArr[0].y - k*pointArr[0].x;
k1 = (-1)/k;
b1 = pointArr[1].y - k1*pointArr[1].x;
break;
case "C":
k = (pointArr[0].y - pointArr[1].y)/(pointArr[0].x - pointArr[1].x);
b = pointArr[1].y - k*pointArr[1].x;
k1 = (-1)/k;
b1 = pointArr[2].y - k1*pointArr[2].x;
break;
default:
break;
}
_pointD.x = (b1-b)/(k-k1);
_pointD.y = ((k+k1)*_pointD.x+(b+b1))/2;
return _pointD;
}
private function getRandomPoint():Point
{
var _point:Point = new Point();
var _ponitX:Number = Number(Number(Math.random()*(stage.stageWidth-100)+100).toFixed(0));//保留小数点后面几位
var _pointY:Number = Number(Number(Math.random()*(stage.stageHeight-100)+50).toFixed(0));
_point.x = _ponitX;
_point.y = _pointY;
trace(_ponitX,_pointY)
return _point;
}
private function getText(_sp:DisplayObjectContainer,_str:String,_x:Number,_y:Number):void
{
var pointTag:TextField = new TextField();
pointTag.selectable = false;
pointTag.autoSize = "left";
pointTag.text = _str;
pointTag.x = _x;
pointTag.y = _y;
_sp.addChild(pointTag);
}
}
}

浙公网安备 33010602011771号