如何找到旋转向量的 x,y 坐标

发布于 2024-08-14 23:02:17 字数 1859 浏览 3 评论 0原文

我找到了从不同的堆栈溢出帖子计算向量后旋转的边界框的宽度和高度的最佳方法。这效果很好。我现在的问题是计算旋转矢量边界框的新 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);

这是一个视频。 在视频中,红色框正确显示了 newWidth、newHeight,但是rotatedXPosrotatedYPos 不会在新旋转向量的顶部/左侧计算。我希望得到任何帮助,非常感谢!

I found the best way of calculating the width and height of the bounding box of a vector post-rotation from a different stack overflow post. This worked great. My problem now is calculating the new x,y coordinates of the rotated vector's bounding box. Here is the my javascript. The newWidth, newHeight variables are correct -- the rotatedXPos, rotatedYPos are not correct, however, and it's because x0, y0 -- which I think should be the rotating middle of the vector -- are also incorrect (They're set to just the x,y coordinates of the upper-right/left of the vector, which I know is wrong).

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. In the video, the red box shows the newWidth, newHeight correctly, but the rotatedXPos, and rotatedYPos are not being calculated at the top/left of the newly rotated vector. I'd love any help, thanks much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开浅夏 2024-08-21 23:02:17

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

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

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

If you know where the center of the ellipse is (xc, yc) then the upper left corner of the bounding box is

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

(assuming +x goes right and +y goes down).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文