简体   繁体   English

计算多边形旋转,使直角是直的

[英]Calculate Polygon Rotation so Right Angles are Straight

I have a polygon (RED SQUARE), for simplicity its a square 100x100 with an offset of 100 from the top left.我有一个多边形(RED SQUARE),为简单起见,它是一个 100x100 的正方形,从左上角偏移 100。 Assume the coordinate system top left is 0,0.假设坐标系左上角为 0,0。 So the coordinates for my simple square are: [x:100,y:100],[x:100,y:200],[x:200,y:200], [x:200,y: 100].所以我的简单正方形的坐标是:[x:100,y:100],[x:100,y:200],[x:200,y:200],[x:200,y:100]。

Now lets say I have another square (BLUE SQUARE), its a 100x100 square also, with the same 100 offset from the top left, but this square is rotated 45 degrees, so its cords are: (rounded) [x:150,y:79],[x:79,y:150],[x:150,y:221],[x:221,y:150].现在假设我有另一个正方形(蓝色正方形),它也是一个 100x100 的正方形,与左上角有相同的 100 偏移,但是这个正方形旋转了 45 度,所以它的绳子是:(圆形)[x:150,y :79],[x:79,y:150],[x:150,y:221],[x:221,y:150]。

How do I calculate the rotation of BLUE SQUARE (45 degrees) if I am given only the coordinates?如果只给定坐标,如何计算 BLUE SQUARE(45 度)的旋转? Assuming I want the right angles to be straight (vertical or horizontal) in this coordinate system (Like RED SQAURE).假设我希望在这个坐标系中直角(垂直或水平)(如 RED SQAURE)。

Worded another way... Given these coordinates: [x:150,y:79],[x:79,y:150],[x:150,y:221],[x:221,y:150] how do I calculate the rotation to apply to polygon so its coordinates are this: [x:100,y:100],[x:100,y:200],[x:200,y:200], [x:200,y: 100]换一种说法...给定这些坐标: [x:150,y:79],[x:79,y:150],[x:150,y:221],[x:221,y:150] 如何我是否计算要应用于多边形的旋转,所以它的坐标是这样的:[x:100,y:100],[x:100,y:200],[x:200,y:200], [x:200,是:100]

Here is a image demonstrating what I am talking about.这是一张图片,展示了我在说什么。 Image of both polygons with coordinates带坐标的两个多边形的图像

The way you do this is to你这样做的方式是

  1. calculate the angle between two adjacent points.计算两个相邻点之间的角度。 The formula for this Math.atan2(x2-x1, y2-y1);这个 Math.atan2(x2-x1, y2-y1) 的公式; This will give you the angle the quadrilateral is on.这将为您提供四边形所在的角度。
  2. Rotate the quadrilateral (from its center) by -angle (or by pi/2 - angle) and one side will be horizontal and one will be vertical将四边形(从其中心)旋转 - 角(或 pi/2 - 角),一侧将是水平的,另一侧将是垂直的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM