旋转矩形内的点

发布于 2024-08-24 21:08:47 字数 566 浏览 6 评论 0原文

我在一个矩形中有一个点,我需要旋转任意角度并找到该点的 xy 。我怎样才能使用 javascript 来做到这一点。

x,y 下面是 1,3 之类的东西,在我将 90 传递给方法后,它将返回 3,1。

|-------------|
|  *          |
|             |
|             |
|-------------|
 _____
|    *|
|     |
|     |
|     |
|     |
 _____

|-------------|
|             |
|             |
|            *|
|-------------|
 _____
|     |
|     |
|     |
|     |
|*    |
 _____

基本上我正在寻找这种方法的勇气

function Rotate(pointX,pointY,rectWidth,rectHeight,angle){
   /*magic*/    
   return {newX:x,newY:y};
}

I have a point in a rectangle that I need to rotate an arbitrary degree and find the x y of the point. How can I do this using javascript.

Below the x,y would be something like 1,3 and after I pass 90 into the method it will return 3,1.

|-------------|
|  *          |
|             |
|             |
|-------------|
 _____
|    *|
|     |
|     |
|     |
|     |
 _____

|-------------|
|             |
|             |
|            *|
|-------------|
 _____
|     |
|     |
|     |
|     |
|*    |
 _____

Basically I am looking for the guts to this method

function Rotate(pointX,pointY,rectWidth,rectHeight,angle){
   /*magic*/    
   return {newX:x,newY:y};
}

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

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

发布评论

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

评论(2

爱你是孤单的心事 2024-08-31 21:08:47

这应该可以做到:

function Rotate(pointX, pointY, rectWidth, rectHeight, angle) {
  // convert angle to radians
  angle = angle * Math.PI / 180.0
  // calculate center of rectangle
  var centerX = rectWidth / 2.0;
  var centerY = rectHeight / 2.0;
  // get coordinates relative to center
  var dx = pointX - centerX;
  var dy = pointY - centerY;
  // calculate angle and distance
  var a = Math.atan2(dy, dx);
  var dist = Math.sqrt(dx * dx + dy * dy);
  // calculate new angle
  var a2 = a + angle;
  // calculate new coordinates
  var dx2 = Math.cos(a2) * dist;
  var dy2 = Math.sin(a2) * dist;
  // return coordinates relative to top left corner
  return { newX: dx2 + centerX, newY: dy2 + centerY };
}

This should do it:

function Rotate(pointX, pointY, rectWidth, rectHeight, angle) {
  // convert angle to radians
  angle = angle * Math.PI / 180.0
  // calculate center of rectangle
  var centerX = rectWidth / 2.0;
  var centerY = rectHeight / 2.0;
  // get coordinates relative to center
  var dx = pointX - centerX;
  var dy = pointY - centerY;
  // calculate angle and distance
  var a = Math.atan2(dy, dx);
  var dist = Math.sqrt(dx * dx + dy * dy);
  // calculate new angle
  var a2 = a + angle;
  // calculate new coordinates
  var dx2 = Math.cos(a2) * dist;
  var dy2 = Math.sin(a2) * dist;
  // return coordinates relative to top left corner
  return { newX: dx2 + centerX, newY: dy2 + centerY };
}
酒几许 2024-08-31 21:08:47
newX = Math.cos(angle) * pointX - Math.sin(angle) * pointY;
newY = Math.sin(angle) * pointX + Math.cos(angle) * pointY;

确保指定相对于旋转原点的坐标!

(尚未准确检查语法,但数学基于旋转矩阵)

newX = Math.cos(angle) * pointX - Math.sin(angle) * pointY;
newY = Math.sin(angle) * pointX + Math.cos(angle) * pointY;

Make sure to specify the coordinates relative to the rotation origin!

(Haven't checked the syntax exactly, but the math is based on a rotation matrix)

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