Wpf 旋转矩形会产生非常混乱的结果
我使用此过程在wpf中将4个边界的矩形旋转为角:Wpf 旋转矩形会产生非常混乱的结果,wpf,vb.net,math,.net-4.0,rotation,Wpf,Vb.net,Math,.net 4.0,Rotation,我使用此过程在wpf中将4个边界的矩形旋转为角: 'find the center Dim center As New Point(((topRight.Margin.Left - topLeft.Margin.Left) / 2) + topLeft.Margin.Left, ((topLeft.Margin.Top - bottomLeft.Margin.Top) / 2) + bottomLeft.Margin.Top
'find the center
Dim center As New Point(((topRight.Margin.Left - topLeft.Margin.Left) / 2) + topLeft.Margin.Left,
((topLeft.Margin.Top - bottomLeft.Margin.Top) / 2) + bottomLeft.Margin.Top)
'shift the points to center and calculate the rotation
Dim tl As Point = getRotatedPoint(New Point(topLeft.Margin.Left - center.X,
topLeft.Margin.Top - center.Y), 1)
Dim tr As Point = getRotatedPoint(New Point(topRight.Margin.Left - center.X,
topRight.Margin.Top - center.Y), 1)
Dim bl As Point = getRotatedPoint(New Point(bottomLeft.Margin.Left - center.X,
bottomLeft.Margin.Top - center.Y), 1)
Dim br As Point = getRotatedPoint(New Point(bottomRight.Margin.Left - center.X,
bottomRight.Margin.Top - center.Y), 1)
'shift the points back from center and move
topLeft.Margin = New Thickness(tl.X + center.X, tl.Y + center.Y, 0, 0)
topRight.Margin = New Thickness(tr.X + center.X, tr.Y + center.Y, 0, 0)
bottomLeft.Margin = New Thickness(bl.X + center.X, bl.Y + center.Y, 0, 0)
bottomRight.Margin = New Thickness(br.X + center.X, +br.Y + center.Y, 0, 0)
getRotatedPoint函数是:
'rotating the borders
Private Function getRotatedPoint(ByVal pnt As Point, ByVal degrees As Double)
Dim rAngle As Double = degrees * (Math.PI / 180)
Dim x As Double = pnt.X * Math.Cos(rAngle) - pnt.Y * Math.Sin(rAngle)
Dim y As Double = pnt.X * Math.Sin(rAngle) + pnt.Y * Math.Cos(rAngle)
Return New Point(x, y)
End Function
但我得到的结果非常混乱,我不知道,任何想法都会受到欢迎:)
edit1:我将getRotatedPoint函数更改为Double,并添加了弧度到度的转换
edit2:校正弧度转换功能
edit3:更正了中心坐标,但仍发生了一些偏移
edit4:这里是测试人员的示例项目:如前所述,不清楚“混乱的结果”是什么意思,所以我想到了几个解释: 如果你的旋转是不正确的,那可能是因为你通过的角度以度为单位,而正弦波和余弦函数期望的是弧度。这似乎是可能的,因为您将角度作为整数而不是浮点表示进行传递。如果要将其作为度传递,请先将其转换为弧度,然后再将其传递给trig函数 其次,如果凌乱的结果意味着代码很复杂,那么您可能希望将代码表示为矩阵。对于二维情况,通过添加1作为第三个元素,将二维向量扩展到三维。您的变换是3x3矩阵,您可以将其相乘以创建旋转、缩放、倾斜、平移等序列。生成的变换可以作为向量矩阵乘法应用 更新: 此功能有一些问题:
Private Function getRotatedPoint(ByVal pnt As Point, ByVal angle As Integer)
Dim x As Integer = pnt.X * Math.Cos(angle) - pnt.Y * Math.Sin(angle)
Dim y As Integer = pnt.X * Math.Sin(angle) + pnt.Y * Math.Cos(angle)
Return New Point(x, y)
End Function
除了度/弧度混淆的问题外,整数和浮点之间的类型转换留给编译器和.Net framework,这可能与您的意图不一致。我将改写如下:
Private Function getRotatedPoint(ByVal pnt As Point, ByVal radians As Double)
Dim x As Double = CDbl(pnt.X) * Math.Cos(radians) - CDbl(pnt.Y) * Math.Sin(radians)
Dim y As Double = CDbl(pnt.X) * Math.Sin(radians) + CDbl(pnt.Y) * Math.Cos(radians)
Return New Point(CInt(x), CInt(y))
End Function
不过,我仍然建议使用矩阵库来实现这一点。它将让您专注于开发程序的核心功能,而不是调试和排除已经编写和重写了很多次的东西
更新2:
这也有问题:
Dim center As New Point((topLeft.Margin.Left + topRight.Margin.Left) / 2,
(topLeft.Margin.Left + bottomLeft.Margin.Left) / 2)
这不应该是:
Dim center As New Point((topLeft.Margin.Left + topRight.Margin.Right) / 2,
(topLeft.Margin.Top + bottomLeft.Margin.Bottom) / 2)
将中心变暗为新点((topLeft.Margin.Left+topRight.Margin.Right)/2,
(topLeft.Margin.Top+bottomLeft.Margin.Bottom)/2)
如果我错了,那么你需要详细解释你的topLeft、topRight、bottomLeft和bottomRight值所代表的内容,以便任何人都能帮助你。。。请参见Kendall Frey的评论。解释“混乱的结果”。我无法准确描述模式,但我认为它是随机的,不会响应所有角度值,即角度1除了10之外什么都不做,它只是围绕未知中心旋转,数量未知!你可以复制过去的代码,并尝试它也许你有一个更好的看法比我。我不能复制/粘贴代码,因为它不是一个可以看到我的答案下面。你假设“1”代表1度。它不是指1弧度。度到弧度的转换是rad=deg*pi/180。弧度到度的转换为deg=rad*180/pi。如果你想给它学位,使用第一个。如果您想给它弧度,请不要使用任何转换。请参见我在上面注释中的解释。@bat3a:您可能不认为它特别复杂,但使用矩阵库为您管理这些转换可以大大简化它。至于你的另一点,当你不小心在整型值和浮点值之间转换时,你可能会看到一些意想不到的行为,特别是在涉及trig函数的情况下。我已经按照你的建议更新了函数,同样的结果!关于矩阵库有什么想法吗?关于更新2,我会画一张图片,然后很快发布