原理图

判断鼠标位置与圆直径形成的等腰三角形的底边与圆的交点的坐标原理图

效果

您的浏览器不支持JavaScript,或者您禁用了JavaScript。

请启用浏览器的JavaScript支持。

如果您确认启用了JavaScript,请使用下面的链接升级Flash Player到最新版本:

安装最新的Flash Player插件

源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
[SWF(width=400,height=400)]
public class SimpleSample5 extends Sprite
{
private var _centerX:Number;
private var _centerY:Number;
private var _radius:Number = 80;
public function SimpleSample5()
{
_centerX = stage.stageWidth/2;
_centerY = stage.stageHeight/2;
draw();
drawTriangle(0, 0);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
}
private function draw():void
{
graphics.clear();
graphics.lineStyle(1);
graphics.moveTo(0, stage.stageHeight/2);
graphics.lineTo(stage.stageWidth, _centerY);
graphics.moveTo(_centerX, 0);
graphics.lineTo(_centerX, stage.stageHeight);
graphics.drawCircle(_centerX, _centerY, _radius);
}
private function drawTriangle($mouseX:Number, $mouseY:Number):void
{
var __dx:Number = _centerX - $mouseX;
var __dy:Number = _centerY - $mouseY;
var __distance:Number = Math.sqrt(__dx*__dx + __dy*__dy);
//计算点击处与圆心夹角的角度并在坐标系中旋转90度
var __radian:Number = Math.atan2(__dy, __dx) - Math.PI/2;
var __x2:Number = _radius * Math.cos(__radian);
var __y2:Number = _radius * Math.sin(__radian);
graphics.moveTo(_centerX, _centerY);
graphics.lineTo($mouseX, $mouseY);
graphics.lineTo(_centerX+__x2, _centerY+__y2);
graphics.lineTo(_centerX, _centerY);
graphics.lineTo(_centerX-__x2, _centerY-__y2);
graphics.lineTo($mouseX, $mouseY);
}
private function mouseMoveHandler(evt:MouseEvent):void
{
draw();
drawTriangle(mouseX, mouseY);
}
}
}

留言

2010-08-18
次访问