2009-12-19 106 views
1

我發現了計算不同堆棧溢出帖子後向量旋轉的邊界框寬度和高度的最佳方法。這很好。我現在的問題是計算旋轉矢量的邊界框的新的x,y座標。這是我的JavaScript。 newWidth, newHeight變量是正確的 - rotatedXPos, rotatedYPos是不正確的,但是,這是因爲x0, y0 - 我認爲應該是向量的旋轉中間 - 也是不正確的(它們被設置爲x,y座標該向量的右上/左側,我知道這是錯誤的)。如何找到旋轉的矢量的x,y座標

this.options.rotationAngle = parseInt(this.options.lastRotationAngle); 
var degreesAsRadians = this.options.rotationAngle*Math.PI/180; 
var points = new Array(); 
points.push({x:0, y:0}); 
points.push({x:this.options.width, y:0}); 
points.push({x:0, y:this.options.height}); 
points.push({x:this.options.width, y:this.options.height}); 
var bb = new Array(); 
bb['left'] = 0; bb['right'] = 0; bb['top'] = 0; bb['bottom'] = 0; 

$A(points).each(function(p) { 
    var newX = Math.abs(parseInt(p.x * Math.cos(degreesAsRadians) + p.y * Math.sin(degreesAsRadians))); 
    var newY = Math.abs(parseInt(p.x * Math.sin(degreesAsRadians) + p.y * Math.cos(degreesAsRadians))); 
    bb['left'] = Math.min(bb['left'], newX); 
    bb['right'] = Math.max(bb['right'], newX); 
    bb['top'] = Math.min(bb['top'], newY); 
    bb['bottom'] = Math.max(bb['bottom'], newY); 
}); 

var newWidth = parseInt(Math.abs(bb['right'] - bb['left'])); 
var newHeight = parseInt(Math.abs(bb['bottom'] - bb['top'])); 

Object.extend(this.options, { 
rotatedWidth: newWidth 
,rotatedHeight: newHeight 
}); 

var x0 = this.options.xPos; 
var y0 = this.options.yPos; 

this.options.rotatedXPos = x0+(this.options.xPos-x0)*Math.cos(degreesAsRadians)+(this.options.yPos-y0)*Math.sin(degreesAsRadians); 
this.options.rotatedYPos = y0-(this.options.xPos-x0)*Math.sin(degreesAsRadians)+(this.options.yPos-y0)*Math.cos(degreesAsRadians); 

And here is a video.在視頻中,紅色框顯示newWidth,newHeight正確,但rotatedXPos,和rotatedYPos不被在上/左新旋轉矢量的計算。我很樂意提供任何幫助,非常感謝!

+0

您可能會在此獲得更好的牽引力:http://mathoverflow.net – 2009-12-19 18:14:42

回答

1

如果您知道哪裏有橢圓形的中心(XC,YC),那麼邊框的左上角是

(xc - newWidth/2, yc - newHeight/2)

(假設+ X順利,和+ Y下降)。

+0

謝謝先生。這個變化對我有效: var box = this.DiagramNodeVector [0] .getBBox(); this.options.rotatedXPos =(box.x +(box.width)/ 2) - newWidth/2; this.options.rotatedYPos =(box.y +(box.height)/ 2) - newHeight/2; – TimDog 2009-12-19 19:29:57

相關問題