Answers:
X = x*cos(θ) - y*sin(θ)
Y = x*sin(θ) + y*cos(θ)
这将为您提供绕原点旋转θ度的点的位置。由于正方形的角围绕正方形的中心而不是原点旋转,因此需要添加几个步骤才能使用此公式。首先,您需要设置相对于原点的点。然后,您可以使用旋转公式。旋转后,您需要将其相对于正方形的中心向后移动。
// cx, cy - center of square coordinates
// x, y - coordinates of a corner point of the square
// theta is the angle of rotation
// translate point to origin
float tempX = x - cx;
float tempY = y - cy;
// now apply rotation
float rotatedX = tempX*cos(theta) - tempY*sin(theta);
float rotatedY = tempX*sin(theta) + tempY*cos(theta);
// translate back
x = rotatedX + cx;
y = rotatedY + cy;
将其应用到所有4个角,就完成了!
通过平移到以枢轴为原点的坐标系,然后绕此原点旋转,然后平移回世界坐标,来绕枢轴旋转点是一种常见的技术。(可汗学院提供了这种方法的很好的解释)
但是,您并未将矩形角存储在世界坐标中,因此我们可以定制一种方法来适合您现有的数据。
Cx, Cy // the coordinates of your center point in world coordinates
W // the width of your rectangle
H // the height of your rectangle
θ // the angle you wish to rotate
//The offset of a corner in local coordinates (i.e. relative to the pivot point)
//(which corner will depend on the coordinate reference system used in your environment)
Ox = W / 2
Oy = H / 2
//The rotated position of this corner in world coordinates
Rx = Cx + (Ox * cos(θ)) - (Oy * sin(θ))
Ry = Cy + (Ox * sin(θ)) + (Oy * cos(θ))
然后可以轻松地将此方法应用于其他三个角落。
基于其他答案,并对其进行补充,我设法在此处使用P5创建了一个示例。
这是代码,以防您要直接访问它:
function setup() {
createCanvas(400, 400);
}
var count = 0;
function draw() {
background(250);
rectMode(CENTER);
stroke(0,0,255);
fill(0,0,255);
count += 1;
var box1X = 100;
var box1Y = 100;
var box2X = 160;
var box2Y = 100;
var box1R = count;
var box2R = -60-count;
var box1W = 50;
var box1H = 50;
var box2W = 50;
var box2H = 50;
translate(box1X, box1Y);
rotate(radians(box1R));
rect(0, 0, box1W, box1H);
rotate(radians(-box1R));
translate(-box1X, -box1Y);
translate(box2X, box2Y);
rotate(radians(box2R));
rect(0, 0, box2W, box2H);
rotate(radians(-box2R));
translate(-box2X, -box2Y);
stroke(255,0,0);
fill(255,0,0);
var pointRotated = [];
pointRotated.push(GetPointRotated(box1X, box1Y, box1R, -box1W/2, box1H/2)); // Dot1
pointRotated.push(GetPointRotated(box1X, box1Y, box1R, box1W/2, box1H/2)); // Dot2
pointRotated.push(GetPointRotated(box1X, box1Y, box1R, -box1W/2, -box1H/2)); // Dot3
pointRotated.push(GetPointRotated(box1X, box1Y, box1R, box1W/2, -box1H/2)); // Dot4
pointRotated.push(createVector(box1X, box1Y)); // Dot5
for (var i=0;i<pointRotated.length;i++){
ellipse(pointRotated[i].x,pointRotated[i].y,3,3);
}
}
function GetPointRotated(X, Y, R, Xos, Yos){
// Xos, Yos // the coordinates of your center point of rect
// R // the angle you wish to rotate
//The rotated position of this corner in world coordinates
var rotatedX = X + (Xos * cos(radians(R))) - (Yos * sin(radians(R)))
var rotatedY = Y + (Xos * sin(radians(R))) + (Yos * cos(radians(R)))
return createVector(rotatedX, rotatedY)
}
<script src="//cdnjs.cloudflare.com/ajax/libs/p5.js/0.3.3/p5.min.js"></script>
重构上面的代码会得到一个清理的表格,该表格还强调了一个简单的事实,即每个角基本上是,每个角都有center + height/2 + width/2
适当的符号。如果将height/2
和width/2
视为旋转向量,这也适用。
相信解释器可以内联帮助者,这应该非常有效,如果我们尝试以此为基准。
function addPoints(p1, p2) {
return { x: p1.x + p2.x, y: p1.y + p2.y }
}
function subPoints(p1, p2 ) {
return { x: p1.x - p2.x, y: p1.y - p2.y }
}
function multPoints(p1, p2 ) {
return { x: p1.x * p2.x, y: p1.y * p2.y }
}
function getRulerCorners() {
const sin = Math.sin(ruler.angle);
const cos = Math.cos(ruler.angle);
const height = { x: sin * ruler.height/2, y: cos * ruler.height/2 };
const heightUp = addPoints(ruler, multPoints({x: 1, y :-1}, height));
const heightDown = addPoints(ruler, multPoints({x: -1, y: 1}, height));
const width = { x: cos * ruler.width/2, y: sin * ruler.width/2 };
ruler.nw = subPoints(heightUp, width);
ruler.ne = addPoints(heightUp, width );
ruler.sw = subPoints(heightDown, width);
ruler.se = addPoints(heightDown, width);
}
请参阅有关旋转的Wikipedia文章。本质是这样的:
(1)如果c为中心点,则角为c +(L / 2,W / 2),+/-等,其中L和W是矩形的长度和宽度。
(2)通过从所有四个角中减去c来平移矩形,以使中心c为原点。
(3)通过引用的三角公式将矩形旋转40度。
(4)通过在每个坐标上加上c来回移。
可以通过将问题分成两个来进行一些优化:
下面的代码,此处的矩形称为标尺。Ruler.x,Ruler,y是矩形的中心。
/** Middle point on rulers's top side. */
function getRulerTopMiddle(cos, sin) {
return {
x : ruler.x + sin * ruler.height/2,
y : ruler.y - cos * ruler.height/2
}
}
/** Middle point on rulers's bottom side. */
function getRulerBottomMiddle(cos, sin) {
return {
x : ruler.x - sin * ruler.height/2,
y : ruler.y + cos * ruler.height/2
}
}
/** Update ruler's four corner coordinates. */
function getRulerCorners() {
const sin = Math.sin(ruler.angle);
const cos = Math.cos(ruler.angle);
const topMiddle = getRulerTopMiddle(cos, sin);
const bottomMiddle = getRulerBottomMiddle(cos, sin);
ruler.nw = {
x: topMiddle.x - (cos * ruler.width/2),
y: topMiddle.y - (sin * ruler.width/2)
}
ruler.ne = {
x: topMiddle.x + (cos * ruler.width/2),
y: topMiddle.y + (sin * ruler.width/2)
}
ruler.sw = {
x: bottomMiddle.x - (cos * ruler.width/2),
y: bottomMiddle.y - (sin * ruler.width/2)
}
ruler.se = {
x: bottomMiddle.x + (cos * ruler.width/2),
y: bottomMiddle.y + (sin * ruler.width/2)
}
}
有点晚了,但这是我使用的紧凑函数。它计算顶部和左侧的点,然后将它们翻转到相对的角。
rotatedRect(float x, float y, float halfWidth, float halfHeight, float angle)
{
float c = cos(angle);
float s = sin(angle);
float r1x = -halfWidth * c - halfHeight * s;
float r1y = -halfWidth * s + halfHeight * c;
float r2x = halfWidth * c - halfHeight * s;
float r2y = halfWidth * s + halfHeight * c;
// Returns four points in clockwise order starting from the top left.
return
(x + r1x, y + r1y),
(x + r2x, y + r2y),
(x - r1x, y - r1y),
(x - r2x, y - r2y);
}
旧帖子,但这是另一种方法:
public static Point[] GetRotatedCorners(Rectangle rectangleToRotate, float angle)
{
// Calculate the center of rectangle.
Point center = new Point(rectangleToRotate.Left + (rectangleToRotate.Left + rectangleToRotate.Right) / 2, rectangleToRotate.Top + (rectangleToRotate.Top + rectangleToRotate.Bottom) / 2);
Matrix m = new Matrix();
// Rotate the center.
m.RotateAt(360.0f - angle, center);
// Create an array with rectangle's corners, starting with top-left corner and going clock-wise.
Point[] corners = new Point[]
{
new Point(rectangleToRotate.Left, rectangleToRotate.Top), // Top-left corner.
new Point(rectangleToRotate.Right, rectangleToRotate.Top), // Top-right corner.
new Point(rectangleToRotate.Right, rectangleToRotate.Bottom), // Bottom-right corner.
new Point(rectangleToRotate.Left, rectangleToRotate.Bottom), // Botton-left corner
};
// Now apply the matrix to every corner of the rectangle.
m.TransformPoints(corners);
// Return the corners of rectangle rotated by the provided angle.
return corners;
}
希望能帮助到你!