Results 1 to 4 of 4

Thread: ScalingnMatrix is messing with cursor position!

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2016
    Posts
    2

    ScalingnMatrix is messing with cursor position!

    Hello! I am new here and I am stumped.

    I have a matrix that I am scaling like this:

    Code:
      CursorMatrix.Scale(Single.Parse(LabelScale.Text), Single.Parse(LabelScale.Text), MatrixOrder.Append)
    The label can be 1.0 to 2.0 in 0.1 steps. 1 is original Size and 2 would be 2x the original.

    I am applying this to graphics. However, I shift the matrix depending on the scaling, as I want it centered to the screen. With this:

    Code:
     Dim TranRepairX As Decimal = CenterOfRotation.X - ((LabelScale) * CenterOfRotation.X)
                        Dim TranRepairY As Decimal = CenterOfRotation.Y - ((2 - LabelScale) * CenterOfRotation.Y)
                        CursorMatrix.Translate(TranRepairX, TranRepairY, MatrixOrder.Append)
    This works great. The image is enlarged, and as it enlarges down and to the right in the coordinates, the above code moves it up and left so its always centered.

    Here is where my problem is. When I draw on the image, the graphics are not going where the cursor is! I understand that this is because of the scaling. However, I cant figure out for the life of me how to get the graphics to go where the cursor is.

    I did figure out that this would do it, but only when the scaling = 2:

    Code:
     NewPoint.X = e.X - (e.X - CenterOfRotation.X) / 2
                       NewPoint.Y = e.Y - (e.Y - CenterOfRotation.Y) / 2
    But, it doesnt work with any other scaling value. Also, I have tried to transform the points like this:

    Code:
              Dim NewCursorPoints As Point() = {New Point(e.X, e.Y)}
                        CursorMatrix.TransformPoints(NewCursorPoints )
                       NewPoint.X = e.X - NewCursorPoints (0).X
                       NewPoint.Y = e.Y - NewCursorPoints (0).Y
    I thought this would transform the points but they always come out to e.x,e.y

    Any ideas!?

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: ScalingnMatrix is messing with cursor position!

    Welcome to VBForums

    Thread moved from the 'VB6 and Earlier' forum to the 'VB.Net' (VB2002 and later) forum

  3. #3
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: ScalingnMatrix is messing with cursor position!

    Did you try not subtracting the transformed points from e.x and e.y.
    The point was to transform e.x and e.y (device coordinates) into world coordinates, so once you have world coordinates it doesn't make sense to subtract them from the device coordinates, they aren't related.
    Code:
            Dim NewCursorPoints As Point() =  {e.Location}
            CursorMatrix.TransformPoints(NewCursorPoints )
            NewPoint = newCursorPoints(0)
    You may have to use an inverse matrix. In fact I'm pretty sure of it, since you're using the CursorMatrix to convert points (world coordinates) to points on the screen (device coordinates) for drawing.
    Now you have screen coordinates (the mouse position), which you want to convert to your drawing coordinates (world coordinates), so that is the reverse direction.

    I would have a form class scope matrix declared and at the point in your drawing where your matrix is in the state that matches your drawing window, save an inverted copy of that matrix in your class scope matrix.
    Then use that matrix to do the TransformPoints call (will convert Mouse X,Y into World Coordinates).

    I figure I have an example of that somewhere, but can't find it offhand.

    Another method is instead of using the TransformPoints method of a matrix, use the TransformPoints method of a Graphics object.
    An example of that I can find. It is a somewhat extensive example of using a Graphics object to transform the Mouse coordinates.

    The example has a number of scrollbars that can scale, rotate and pan horizontally and vertically a drawing. There are some other controls as well, but I would have to look at the code again as it has been a while. Probably not the most complete from a user friendly GUI perspective.

    At any point, you can use the mouse to draw on that drawing as an example of converting the mouse coordinates to the world coordinates of the drawing regardless of pan, scale or rotation.
    It doesn't look like I've ever posted it on this forum before, so I'll post it here.
    Attached Files Attached Files
    Last edited by passel; Jul 9th, 2020 at 12:51 PM.

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2016
    Posts
    2

    Re: ScalingnMatrix is messing with cursor position!

    That worked great! Thanks!

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