-
Transform Problem
Hi everybody,
I have a problem with world transformations. The routine attached below is supposed to draw 2 rectangles and then it attempts to do a rotation transformation and draw 2 strings and again the rectangles. The desired result is the two original rectangles and the rotated ones with the text inside having the same center. The code works only I am not able to understand where exactly must i translate (and what is the offset value) the rotation so that the center of the rectangle stays in the same place before and after the rotation. As it works now, depending on the rotation angle the rectangles and the text shift either to the left or right... My question is so, what am I doing wrong when I compute dx and dy that must be passed to TranslateTransform?
This sub is called from the OnPaint of a simple form:
Public Sub dodraw(ByVal e As PaintEventArgs)
Dim g As Graphics = e.Graphics
Dim p As Pen
p = New Pen(Color.Yellow, 1)
Dim p2 As New Pen(Color.Red, 5)
p2.Alignment = Drawing.Drawing2D.PenAlignment.Inset
Dim x1 As Integer = 10
Dim y1 As Integer = 10
Dim w1 As Integer = 100
Dim h1 As Integer = 100
Dim x2 As Integer = 100
Dim y2 As Integer = 100
Dim w2 As Integer = 200
Dim h2 As Integer = 200
g.DrawRectangle(p2, New Rectangle(x1, y1, w1, h1))
g.DrawRectangle(p2, New Rectangle(x2, y2, w2, h2))
Dim pf() As PointF = {New PointF(g.VisibleClipBounds.Location.X, g.VisibleClipBounds.Location.X)}
Dim sz() As SizeF = {New SizeF(g.VisibleClipBounds.Size.Width, g.VisibleClipBounds.Size.Height)}
Dim np() As PointF = {New PointF(pf(0).X, pf(0).Y)}
Dim mp() As PointF = {New PointF(x1 + w1 / 2, y1 + h2 / 2)}
g.TransformPoints(Drawing.Drawing2D.CoordinateSpace.World, Drawing.Drawing2D.CoordinateSpace.Page, mp)
g.TransformPoints(Drawing.Drawing2D.CoordinateSpace.World, Drawing.Drawing2D.CoordinateSpace.Page, np)
Dim dx As Integer = mp(0).X - np(0).X
Dim dy As Integer = mp(0).Y - np(0).Y
g.TranslateTransform(dx, dy)
g.RotateTransform(45)
Dim strfmt As StringFormat = New StringFormat()
strfmt.Alignment = StringAlignment.Center
strfmt.LineAlignment = StringAlignment.Center
g.DrawString("AAAAA", _
New Font("Arial", 14, FontStyle.Bold), _
New SolidBrush(Color.Yellow), _
New RectangleF(x1, y1, w1, h1), strfmt)
g.DrawRectangle(p2, New Rectangle(x1, y1, w1, h1))
g.ResetTransform()
np(0) = New PointF(pf(0).X, pf(0).Y)
mp(0) = New PointF(x2 + w2 / 2, y2 + h2 / 2)
g.TransformPoints(Drawing.Drawing2D.CoordinateSpace.World, Drawing.Drawing2D.CoordinateSpace.Page, mp)
g.TransformPoints(Drawing.Drawing2D.CoordinateSpace.World, Drawing.Drawing2D.CoordinateSpace.Page, np)
dx = mp(0).X - np(0).X
dy = mp(0).Y - np(0).Y
g.TranslateTransform(dx, dy)
g.RotateTransform(20)
g.DrawString("BBBBB", _
New Font("Arial", 14, FontStyle.Bold), _
New SolidBrush(Color.Yellow), _
New RectangleF(x2, y2, w2, h2), strfmt)
g.DrawRectangle(p2, New Rectangle(x2, y2, w2, h2))
g.ResetTransform()
End Sub
Any help would be appreciated,
thank you,
Iulian