Results 1 to 7 of 7

Thread: [RESOLVED] Find location of points after ResetTransform

  1. #1

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,561

    Resolved [RESOLVED] Find location of points after ResetTransform

    I'm writing a small app that draws Japanese multiplications, based on values in two NumericUpDown controls...

    Name:  10-10-2020_03.42.28.jpg
Views: 58
Size:  20.7 KB

    The problem i'm trying to solve is how to get the location of the red dots in the image. The location of these points varies depending on the NumericUpDown control values.
    The drawing is all done in the Form_Paint event...

    Code:
    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim d1 As Integer = CInt(NumericUpDown1.Value) \ 10
        Dim d2 As Integer = CInt(NumericUpDown1.Value) Mod 10
        Dim d3 As Integer = CInt(NumericUpDown2.Value) \ 10
        Dim d4 As Integer = CInt(NumericUpDown2.Value) Mod 10
    
        Dim w As Integer = 50 + ((d3 - 1) * 12) + 150 + ((d4 - 1) * 12) + 50
        Dim h As Integer = 50 + ((d1 - 1) * 12) + 150 + ((d2 - 1) * 12) + 50
    
        Dim x As Integer = -(w \ 2)
        Dim y As Integer = -(h \ 2)
    
        e.Graphics.TranslateTransform(300, 300)
        e.Graphics.RotateTransform(-45)
    
        Dim current_Y As Integer
        For i As Integer = 0 To d1 - 1
            current_Y = y + 50 + i * 12
            e.Graphics.DrawLine(Pens.SteelBlue, x, current_Y, x + w, current_Y)
        Next
    
        For i As Integer = 0 To d2 - 1
            current_Y = y + 50 + ((d1 - 1) * 12) + 150 + (i * 12)
            e.Graphics.DrawLine(Pens.SteelBlue, x, current_Y, x + w, current_Y)
        Next
    
        Dim current_X As Integer
        For i As Integer = 0 To d3 - 1
            current_X = x + 50 + (i * 12)
            e.Graphics.DrawLine(Pens.LightSeaGreen, current_X, y, current_X, y + h)
        Next
    
        For i As Integer = 0 To d4 - 1
            current_X = x + 50 + ((d3 - 1) * 12) + 150 + (i * 12)
            e.Graphics.DrawLine(Pens.LightSeaGreen, current_X, y, current_X, y + h)
        Next
    
        e.Graphics.ResetTransform()
    
    End Sub

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,733

    Re: Find location of points after ResetTransform

    I already answered this question in the VB.NET forum. I've asked the mods to delete this thread as a duplicate. If you'd prefer that thread moved to the Maths forum then you ought to report it but, as my post there indicates, it really is a VB.NET question because you don't need to perform the matrix mathematics yourself.

  3. #3

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,561

    Re: Find location of points after ResetTransform

    Ok thanks for answering. Iíll try your matrix idea again later. The problem is that I need to draw part of the diagram with a transform and part without a transform. After drawing the lines, I need to reset the transform, then draw 2 arcs and some text.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,733

    Re: Find location of points after ResetTransform

    Quote Originally Posted by .paul. View Post
    The problem is that I need to draw part of the diagram with a transform and part without a transform. After drawing the lines, I need to reset the transform, then draw 2 arcs and some text.
    I don't think that that's an issue. I haven't tested this but here's what I think you need to do:
    vb.net Code:
    1. Private Function GetTransformationMatrix() As Matrix
    2.     Dim m As New Matrix
    3.  
    4.     m.Translate(300, 300)
    5.     m.Rotate(-45)
    6.  
    7.     Return m
    8. End Function
    9.  
    10. Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    11.     Dim d1 As Integer = CInt(NumericUpDown1.Value) \ 10
    12.     Dim d2 As Integer = CInt(NumericUpDown1.Value) Mod 10
    13.     Dim d3 As Integer = CInt(NumericUpDown2.Value) \ 10
    14.     Dim d4 As Integer = CInt(NumericUpDown2.Value) Mod 10
    15.  
    16.     Dim w As Integer = 50 + ((d3 - 1) * 12) + 150 + ((d4 - 1) * 12) + 50
    17.     Dim h As Integer = 50 + ((d1 - 1) * 12) + 150 + ((d2 - 1) * 12) + 50
    18.  
    19.     Dim x As Integer = -(w \ 2)
    20.     Dim y As Integer = -(h \ 2)
    21.  
    22.     e.Graphics.Transform = GetTransformationMatrix()
    23.  
    24.     Dim current_Y As Integer
    25.  
    26.     For i As Integer = 0 To d1 - 1
    27.         current_Y = y + 50 + i * 12
    28.         e.Graphics.DrawLine(Pens.SteelBlue, x, current_Y, x + w, current_Y)
    29.     Next
    30.  
    31.     For i As Integer = 0 To d2 - 1
    32.         current_Y = y + 50 + ((d1 - 1) * 12) + 150 + (i * 12)
    33.         e.Graphics.DrawLine(Pens.SteelBlue, x, current_Y, x + w, current_Y)
    34.     Next
    35.  
    36.     Dim current_X As Integer
    37.  
    38.     For i As Integer = 0 To d3 - 1
    39.         current_X = x + 50 + (i * 12)
    40.         e.Graphics.DrawLine(Pens.LightSeaGreen, current_X, y, current_X, y + h)
    41.     Next
    42.  
    43.     For i As Integer = 0 To d4 - 1
    44.         current_X = x + 50 + ((d3 - 1) * 12) + 150 + (i * 12)
    45.         e.Graphics.DrawLine(Pens.LightSeaGreen, current_X, y, current_X, y + h)
    46.     Next
    47.  
    48.     e.Graphics.Transform = New Matrix()
    49. End Sub
    You can call GetTransformationMatrix any time to get the same Matrix used to transform your drawing.

  5. #5

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,561

    Re: Find location of points after ResetTransform

    Iím fairly sure youíre right about that part of it, but I still need those points, and Iím not experienced with matrixís and Iíve been unable to rotate those points to the correct location after resetting the transform.

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,733

    Re: Find location of points after ResetTransform

    If I'm understanding you correctly, you can use this method:
    vb.net Code:
    1. Private Function GetTransformedPoints(ParamArray points As Point()) As Point()
    2.     Dim m = GetTransformationMatrix()
    3.  
    4.     m.TransformPoints(points)
    5.  
    6.     Return points
    7. End Function
    You feed that the original points and it will output the locations that your transformed Graphics object would draw those points relative to the controls standard coordinates. That's what you want, right?

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,733

    Re: Find location of points after ResetTransform

    I just realised that a Matrix is an IDisposable, so you ought to use a Using statement every time you call GetTransformationMatrix.
    vb.net Code:
    1. Private Function GetTransformedPoints(ParamArray points As Point()) As Point()
    2.     Using m = GetTransformationMatrix()
    3.         m.TransformPoints(points)
    4.     End Using
    5.  
    6.     Return points
    7. End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width