修改了C#中旋转矩形的右端点 [英] Revised Right End Point of rotated rectangle in C#

查看:42
本文介绍了修改了C#中旋转矩形的右端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人

我在图片框中绘制一个旋转的矩形(45度角).我知道原点(左上角)是矩形的(x = 5,y = 15);我想要终点(右角).任何人都可以帮助我.

Dear All

I draw a rotated rectangle (angle 45) in a picture box. i know the origin point (Left Top Corner) is (x=5,y=15) of the rectangle; i want the the end point (Right Corner). any one help me.

推荐答案

一个矩形由4个点组成. 右下"的角度可能会随角度而变化(对于正方形旋转45°时,哪一个最右下"是模棱两可的.)

最初位于右下角原始的点的位置在本地坐标中为(w,h).局部轴变为x''=(cos a,sin a)和y''=(-sin a,cos a),因此该点的位置变为(w cos a-h sin a,w sin a + h cos a),其中(w,h)=矩形的原始大小,a =旋转矩形的角度(逆时针).

您可以对其他三个点执行相同的操作,这三个点相对于左上角的局部坐标(您声明这是原点)为(0,0),(w,0)和(0,h),这意味着它们的旋转位置是:
TL =(0,0)
TR =(w cos a,w sin a)
BL =(-h sin a,h cos a)

然后,您可以找到哪个最右下方",方法是找出哪个最大的点积与方向矢量指定右下角",即(1,1)* –只是总和坐标,但您应该了解原因.
*:实际上,标准方向向量为norm(1,1)=(0.707,0.707),但是由于我们正在相互比较值,因此在这种情况下重新缩放并不重要.
A rectangle consists of 4 points. The one which is ''bottom right'' may change depending on the angle (and at a 45° rotation for a square, it is ambiguous which one is most ''bottom right'').

The location of the point which was originally at the bottom right is (w, h) in local coordinates. The local axes become x''=(cos a, sin a) and y''=(-sin a, cos a) so the location of that point becomes (w cos a - h sin a, w sin a + h cos a), with (w,h) = the original size of the rectangle and a = the angle through which you have rotated it (anticlockwise).

You can do the same thing for the other three points, whose local coordinates relative to the top left (you state that this is the origin) are (0,0), (w,0) and (0,h), meaning that their rotated locations are:
TL = (0, 0)
TR = (w cos a, w sin a)
BL = (-h sin a, h cos a)

You can then find out which is the most ''bottom right'' by finding which has the largest dot product with the direction vector to specify ''bottom-rightness'' which is (1, 1)* – which is simply the sum of coordinates, but you should understand why.

*: Actually the standard direction vector is norm(1,1) = (0.707, 0.707), but as we are comparing values to each other, the rescaling doesn''t matter in this case.


这是一个数学问题,我喜欢:).
诀窍是找到中心与右下角之间的距离.
然后使用Cosinus和Sinus可以计算X和Y位置的变换.
窦将角度(以弧度为单位)转换为介于1(90度)和-1(270度)之间的浮点数.
余弦函数也是如此,但在0度时为1,在180度时为-1.
尽管我相信我做得不错,但可能在计算半径"时可能会犯错误.

This is a math question, I like :) .
The trick is to find the distance between center and lower right corner.
Then using Cosinus and Sinus the transformation of X and Y position can be calculated.
Sinus transforms an angle (in radians) to a floating number between 1 (90 degrees) and -1 (270 degrees).
Cosinus does the same but is 1 at 0 degrees and -1 at 180 degrees.
A possible mistake may lie in calculating ''Radius'' though I believe I did it pretty well.

//Some declarations, sort of 'default settings', these should only be calculated when the rectangle is initialised or resized.
float defaultAngle = 45; //The default Angle so calculations still point to the lower right corner when it hasn't been rotated yet.
PointF center = new PointF(width/2,height/2);
float Radius = Math.Sqrt(Math.Pow(width - Center.X,2) + Math.Pow(height - Center.Y)); // Distance between the center and the lower right corner.

//The point calculations, to be done whenever the rectangle is being rotated.
fullAngle = defaultAngle + yourAngle; //yourAngle is the rotation of the rectangle.
PointF BottomRightCorner = new PointF(0,0); //Point in which we will store the 
BottomRightCorner.X = center.X + Math.Cos(Radians(fullAngle)) * Radius; // The parameter for Math.Cos requires the angle in radians.
BottomRightCorner.Y = center.Y + Math.Sin(Radians(fullAngle)) * Radius; //Same goes for Math.Sin .
//BottomRightCorner should now contain the position of the bottom right corner.


-


--

float Radians(float Degrees)
{
 return Degrees * (Math.PI /180); //Simple conversion form degrees to radians.
}


这篇关于修改了C#中旋转矩形的右端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆