旋转矩形会产生非常混乱的结果

时间:2012-06-25 16:34:05

标签: 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)

    '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函数更改为双精度,并为度数转换添加了一个弧度。

edit2 :更正了弧度转换功能。

edit3 :纠正了中心坐标,但仍然发生了一些抵消。

edit4 :以下是测试人员的示例项目:http://dl.dropbox.com/u/33417300/testapp.zip

1 个答案:

答案 0 :(得分:2)

如上所述,目前还不清楚“杂乱的结果”是什么意思,所以会想到几个解释:

如果您的旋转不正确,可能是因为您以度为单位传递角度,而SIN和COS函数则需要弧度。这似乎很可能因为您将角度作为整数而不是浮点表示传递。如果要将它们作为度数传递,请在将它们传递给trig函数之前转换为弧度。

其次,如果通过混乱的结果表示代码很复杂,您可能希望将affine transformations表示为矩阵。对于2-D情况,通过添加1作为第三个元素,将2-D向量扩展为3-D。您的变换是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框架,这可能与您的意图不一致。我会改写如下:

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,topRight,bottomLeft和bottomRight值对于任何人来说都足以帮助你......请参阅Kendall Frey对SSCCE的评论。