2012-01-30 80 views
0

我試圖從給定點(中心)在我的Canvas中旋轉一個點。在我的MouseDown處理程序,我救的地步,用戶點擊(oldPos),在我的MouseMove處理程序中,我這樣做:Flex中給定中心的旋轉點

private function onMouseMove(event:MouseEvent):void 
{ 
    // Where the user pointer right now 
    var endPoint:Point = new Point(event.localX,event.localY); 

    // Calculate angle in radians from the user pointer 
    var angle:Number = getLineAngleFromHorizontal(oldPos,endPoint); 
    var rad:Number = Math.PI * (angle/180); 

    // Point which I want to rotate 
    pTop = new Point(oldPos.x,oldPos.y - 30); 
    var distance:Number = Point.distance(oldPos,pTop); 

    // Calculate the translation point from previously distance and angle 
    var translatePoint:Point = Point.polar(distance, rad); 

    // New point coordinates (in theory) 
    pTop.x += translatePoint.x; 
    pTop.y += translatePoint.y; 

    // Then, draw the line... 
} 

哪裏getLineAngleFromHorizontal是返回由中心和形成的角度的功能給點:

private function getLineAngleFromHorizontal(p1:Point,p2:Point):Number 
{ 
    var RotVecOrigen:Point = new Point((p2.x-p1.x),(p2.y-p1.y)); 
    var ModRot:Number = Math.sqrt((RotVecOrigen.x*RotVecOrigen.x)+(RotVecOrigen.y*RotVecOrigen.y)); 
    var ret:Number; 
    if(((RotVecOrigen.x < 0) && (RotVecOrigen.y <= 0))||((RotVecOrigen.x >= 0) && (RotVecOrigen.y < 0))) 
    { 
    ret = Math.round((180.0*(Math.acos(RotVecOrigen.x/ModRot))/Math.PI)); 
    }else{ 
    ret = Math.round((180.0*(-Math.acos(RotVecOrigen.x/ModRot))/Math.PI)); 
    } 
    return ret; 
} 

要看到一個例子,看下面的圖片:

example

但我不硝酸鉀w爲什麼不工作。我的意思是,pTop點不移動我想要的地方,我認爲我的計算是正確的。

任何人都可以幫助我嗎? (也許有數學知識的人)

回答

0

我不完全確定你想完成什麼。你希望你的新點與你的中心點偏離330度嗎?

如果您想要移動點330度,使用此:

function directionalDistance($start:Point, $direction:Number, $distance:Number, $zeroDegreesUp:Boolean = false):Point{   

     if($zeroDegreesUp) $direction = ($direction + 270)%360; 

     var x:Number = Math.cos($direction * Math.PI/180) * $distance; 
     var y:Number = Math.sin($direction * Math.PI/180) * $distance; 

     return new Point($start.x +x, $start.y + y); 
    } 

     // 
     var newPoint:Point = directionalDistance(new Point(event.localX,event.localY), 330, 50, true);