【问题标题】:How to find the x,y coordinates of a rotated vector如何找到旋转矢量的 x,y 坐标
【发布时间】:2010-12-28 08:50:42
【问题描述】:

我找到了计算来自不同堆栈溢出帖子的矢量旋转后边界框宽度和高度的最佳方法。这很好用。我现在的问题是计算旋转矢量边界框的新 x,y 坐标。这是我的javascript。 newWidth, newHeight 变量是正确的——rotatedXPos, rotatedYPos 不正确,但是,这是因为 x0, y0——我认为应该是向量的旋转中间——也不正确(它们被设置为向量右上角/左上角的 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,但在新旋转矢量的顶部/左侧没有计算 rotatedXPosrotatedYPos。我很想得到任何帮助,非常感谢!

【问题讨论】:

标签: javascript geometry rotation trigonometry


【解决方案1】:

如果你知道椭圆的中心在哪里 (xc, yc) 那么边界框的左上角就是

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

(假设 +x 向右,+y 向下)。

【讨论】:

  • 谢谢先生。这对我有用: 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;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-04
  • 1970-01-01
  • 1970-01-01
  • 2022-11-23
相关资源
最近更新 更多