-
Oct 9th, 2020, 10:42 PM
#1
[RESOLVED] Find location of points after ResetTransform
I'm writing a small app that draws Japanese multiplications, based on values in two NumericUpDown controls...
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 10th, 2020, 12:01 AM
#2
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.
-
Oct 10th, 2020, 12:09 AM
#3
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.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 10th, 2020, 12:18 AM
#4
Re: Find location of points after ResetTransform
Originally Posted by .paul.
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:
Private Function GetTransformationMatrix() As Matrix
Dim m As New Matrix
m.Translate(300, 300)
m.Rotate(-45)
Return m
End Function
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.Transform = GetTransformationMatrix()
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.Transform = New Matrix()
End Sub
You can call GetTransformationMatrix any time to get the same Matrix used to transform your drawing.
-
Oct 10th, 2020, 12:41 AM
#5
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.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 10th, 2020, 03:10 AM
#6
Re: Find location of points after ResetTransform
If I'm understanding you correctly, you can use this method:
vb.net Code:
Private Function GetTransformedPoints(ParamArray points As Point()) As Point()
Dim m = GetTransformationMatrix()
m.TransformPoints(points)
Return points
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?
-
Oct 10th, 2020, 03:15 AM
#7
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:
Private Function GetTransformedPoints(ParamArray points As Point()) As Point()
Using m = GetTransformationMatrix()
m.TransformPoints(points)
End Using
Return points
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|