# Flash/Flex学习笔记(48)：反向运动学(下)

或

1.根据鼠标所在位置(mouseX,mouseY)得到dy,dx，进而确定角度D 2.根据a,b,c边长，确定角度B 3.蓝色seg1的旋转角度为 D+B

4.蓝色seg1旋转后，将红色seg0重新挂到seg1末端 5.红色seg0的旋转角度，我们借助向量平移，可以得到最终的旋转角度E为： D + B + 180度 + C

package {
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;

public class Cosines extends Sprite {

private var seg0:Segment;
private var seg1:Segment;
private var seg0Width:uint = 80;
private var seg1Width:uint = 100;

public function Cosines() {
init();
}

private function init():void {
seg0=new Segment(seg0Width,10,0xff0000);
seg1=new Segment(seg1Width,20,0x0000ff);
seg1.x=stage.stageWidth/2;
seg1.y=stage.stageHeight/2;
}

private function onEnterFrame(event:Event):void {
var dx:Number=mouseX-seg1.x;
var dy:Number=mouseY-seg1.y;
var dist:Number=Math.sqrt(dx*dx+dy*dy);
var a:Number=seg0Width;
var b:Number=seg1Width;
var c:Number=Math.min(dist,a+b);//注:如果鼠标离自由端太远，构不成三角形时，c边以a+b为准，相当于此时三角形退化为二个角接近0,	另一角接近180度的特殊情况
var B:Number = Math.acos((b * b - a * a - c * c) / (-2 * a * c));//注：flash中的坐标系跟数学中的常规坐标系，y轴是反向的，所以"2"前要加负号
var C:Number = Math.acos((c * c - a * a - b * b) / (-2 * a * b));
var D:Number=Math.atan2(dy,dx);

//处理固定端的旋转
seg1.rotation = (D + B) * 180 / Math.PI;

//重新将seg0挂到seg1末端
seg0.x=seg1.getPin().x;
seg0.y=seg1.getPin().y;

//处理自由端的旋转
var E:Number=D+B+Math.PI+C;
seg0.rotation=E*180/Math.PI;
}
}
}

private function onEnterFrame(event:Event):void {
var dx:Number=mouseX-seg1.x;
var dy:Number=mouseY-seg1.y;
var dist:Number=Math.sqrt(dx*dx+dy*dy);
var a:Number=seg1Width;
var b:Number=seg0Width;
var c:Number=Math.min(dist,a+b);
var B:Number = Math.acos((b * b - a * a - c * c) / (-2 * a * c));
var C:Number = Math.acos((c * c - a * a - b * b) / (-2 * a * b));
var D:Number=Math.atan2(dy,dx);
seg1.rotation = (D + B) * 180 / Math.PI;
seg0.x=seg1.getPin().x;
seg0.y=seg1.getPin().y;
var E:Number=D+B+Math.PI+C;
seg0.rotation=E*180/Math.PI;

//新增的画线部分，以方便观察
graphics.clear();
graphics.lineStyle(1,0xff0000,0.5);
graphics.moveTo(mouseX,mouseY);
graphics.lineTo(seg0.getPin().x,seg0.getPin().y);

graphics.lineStyle(1,0x0000ff,0.5);
graphics.moveTo(mouseX,mouseY);
graphics.lineTo(seg1.getPin().x,seg1.getPin().y);

}

package {
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;

public class Cosines extends Sprite {

private var seg0:Segment;
private var seg1:Segment;
private var seg0Width:uint=80;
private var seg1Width:uint=100;

//用于保存上次自由端的dx,dy值
private var dxOld:Number=0;
private var dyOld:Number=0;

public function Cosines() {
init();
}

private function init():void {
seg0=new Segment(seg0Width,10,0xff0000);
seg1=new Segment(seg1Width,20,0x0000ff);
seg1.x=stage.stageWidth/2;
seg1.y=stage.stageHeight/2;
}

private function onEnterFrame(event:Event):void {
var dx:Number=mouseX-seg1.x;
var dy:Number=mouseY-seg1.y;
if (dx==dxOld&&dy==dyOld) {
//trace("已经调整到位了!");
return;//直接返回，不作处理了
}
dxOld=dx;
dyOld=dy;
//trace(dx,dy);
var dist:Number=Math.sqrt(dx*dx+dy*dy);
var a:Number=seg1Width;
var b:Number=seg0Width;
if (dist>=(a+b)) {
//trace(dist,a+b);
seg1.rotation=seg0.rotation=Math.atan2(dy,dx)*180/Math.PI;
} else {
var c:Number=Math.min(dist,a+b);
var B:Number = Math.acos((b * b - a * a - c * c) / (-2 * a * c));
var C:Number = Math.acos((c * c - a * a - b * b) / (-2 * a * b));
var D:Number=Math.atan2(dy,dx);
seg1.rotation = (D + B) * 180 / Math.PI;
var E:Number=D+B+Math.PI+C;
seg0.rotation=E*180/Math.PI;
}
seg0.x=seg1.getPin().x;
seg0.y=seg1.getPin().y;
}
}
}

seg1.rotation = (D - B) * 180 / Math.PI;
var E:Number=D - B + Math.PI - C;

992 篇文章76 人订阅

0 条评论

## 相关文章

26510

2065

2345

2833

### 浅扒Android动态设置字体大小

Android开发中,TextView类的控件应该说是很常用了。一般来说我们是通过android:textSize="20sp" 来设置字体大小，但是很多时候...

1503

3649

43711

35310

28110

5088