Scaling x,y coordinates in a picture box
Scaling x,y coordinates in a picture box
Scaling x,y coordinates in a picture box
Scaling x,y coordinates in a picture box
Scaling x,y coordinates in a picture box
Scaling x,y coordinates in a picture box Scaling x,y coordinates in a picture box Scaling x,y coordinates in a picture box Scaling x,y coordinates in a picture box Scaling x,y coordinates in a picture box Scaling x,y coordinates in a picture box Scaling x,y coordinates in a picture box Scaling x,y coordinates in a picture box
Scaling x,y coordinates in a picture box Scaling x,y coordinates in a picture box
Scaling x,y coordinates in a picture box
Go Back  Xtreme Visual Basic Talk > > > Scaling x,y coordinates in a picture box


Reply
 
Thread Tools Display Modes
  #1  
Old 05-04-2013, 07:02 AM
supercrewed supercrewed is offline
Centurion
 
Join Date: Feb 2004
Posts: 152
Default Scaling x,y coordinates in a picture box


Good day all, I am importing a series of X, Y coordinates, with an "R" for radius, and attempting to draw circles in a picturebox, using number like the ones below;
I'm totally lost on this one...

X0.00000 Y2.36220 R0.28150
X0.00000 Y0.78740 R0.17205
X0.00000 Y1.77165 R0.10433
X0.00000 Y1.77165 R0.08268
X0.00000 Y1.77165 R0.09843
X0.00000 Y1.77165 R0.22047
X0.50620 Y2.17323 R0.23622
X0.63879 Y0.07633 R0.25000
X0.66798 Y1.38583 R0.19685
X1.18898 Y1.38583 R0.19685
X1.18898 Y2.17323 R0.31496
X1.77165 Y2.36220 R0.15748
X1.77165 Y0.00000 R0.28150
X1.77165 Y0.59055 R0.22047
X1.77165 Y0.59055 R0.10433
X1.77165 Y0.59055 R0.08268
X1.77165 Y0.59055 R0.09843
Reply With Quote
  #2  
Old 05-04-2013, 09:24 AM
Gruff's Avatar
GruffScaling x,y coordinates in a picture box Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

Good day SC,

The easiest way to display graphics in a VB.Net Winforms project is to place the code
that does the drawing inside your into the control's 'Paint' event then inform
Windows that the control is dirty. Windows responds by doing whatever it takes to redraw the control.

This is true for most controls and forms.

While it is the most efficient method for Windows I find it counter-intuitive,
but that is the way it works.

Drawing Circles is also IMHO not as straight forward as one would hope as
the drawing tool requries the boundary rectangle of the circle, not the center pont and radius.
We will get around that by creating a translator function to do the job.

So...

Step 1) Besides importing the 'System.Drawing' namespace you are going to need the 'System .IO' namespace if your data is in a text file.

Code:
Imports System Imports System.Drawing Imports System.IO Public Class Form1 '... End Class
Step 2) You are also going to need some sort of numerical structure to hold each circle
You could do it with a 2D array, but let's keep more current and use a list of a class.
This also allows us to add other circle properties later like color, line style or visibility.
Code:
Public Class Circle2D ' Holds components that define a circle. Public Property Pt as PointF = Nothing Public Property Radius as Single = Nothing Public ReadOnly Property Rect as RectangleF Dim Lt as single = Pt.X - Radius Dim Tp as single = Pt.Y - Radius Dim Wt as single = Radius * 2 Dim Ht as single = Radius * 2 Return new RectangleF(Lt,Tp,Wt,HT) End Property Public Sub New(Byval X as single, Byval Y as single, Byval R as single) ' Constructor method Pt = new PointF(X,Y) Radius = R End Sub End Class
Step 3) A parser to convert your lines of text into Circle objects and a list variable to hold
your circles and finally a form level variable that tells your paint routine to display the list.
Code:
Imports System Imports System.Drawing Imports System.IO Public Class Form1 Dim Circles as New list(of Circle2D) Dim UpdatePic as boolean = false Private Function TextToCircle(Byval Txt as string) as Shape_Circle Dim Segments() as string = Txt.Trim.Split(" "c) Dim X as single = Segments(0).Trim.SubString(1) 'Crop 'X' from string Dim Y as single = Segments(1).Trim.SubString(1) 'Crop 'Y' from string Dim R as single = Segments(2).Trim.SubString(1) 'Crop 'R' from string Dim Circ as Circle2D = New Circle2D(X,Y,R) End Function ' Read text file, Translate, and Fill list Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim sLines() as string = File.ReadAllLines(<FilePath and Name>) For each sLine as string in sLines If sline.Startswith("X") then Circles.Add(TextToCircle(sLine)) End if Next sLine UpdatePic = True Picturebox1.Invalidate '<-- Tell Windows to redraw the picturebox. End Sub End Class
Step 4) Write Code for your paint event
Code:
'... Public Class Form1 '... Private Sub DrawCircles(ByRef g as Graphics) with g For each C as Circle2D in Circles .DrawEllipse(Pen.Black,C.Rect) Next End with End Sub Private Sub PictureBox1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint If UpdatePic Then DrawCircles(e.Graphics) End If End Sub End Class '...

UP NEXT: Controlling the graphics scale and origin
__________________
Burn the land and boil the sea
You can't take the sky from me


~T

Last edited by Gruff; 05-06-2013 at 04:59 AM.
Reply With Quote
  #3  
Old 05-04-2013, 12:14 PM
Gruff's Avatar
GruffScaling x,y coordinates in a picture box Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

Scaling in Winforms graphics is handled by matrix manipulation in your drawing sub routine.

The drawing matrix object has properties and methods to help you get the desired results.


Code:
With g ' if your graphics represent something in the real world ' then you probably want to ues Inch units. .PageUnit = GraphicsUnit.Inch ' The matrix defines the coordinate system to use ' in this case 1,0,0,-1 means a standard cartesian coordinates system ' where positive X is to the right and positive Y is Up. ' This differs from standard microsoft windows coordinates where ' positive X is to the Right and positive Y is Down. ' (Add 20 to a button.Top property and the button moves down.) ' The last two arguments may be used for a zoom about center if you want to zoom. Dim oMatrix As Matrix = New Matrix(1, 0, 0, -1, 0, 0) ' Use the scale method to adjust both x and y scale. ' Use variables if you want to zoom oMatrix.Scale(0.5,0.5) ' <-- Double Scale I think ' Use the translate method to move the origin ' Use variables to Pan oMatrix.Translate(o.0, -(0.0)) ' We reset the graphics matrix to default settings before we apply ' our new matrix. Remember this whole routne occurs everytime windows ' refreshes the graphics. If you are panning and zooming this is important. .Transform.Reset() ' Apply your custom matrix .Transform = oMatrix ' Get rid of jaggy graphics .SmoothingMode = SmoothingMode.HighQuality End With '...

If you want to automatically adust your scale to fix whatever circles may be drawn then

You will need to find the centroid of your circle list data and the delta of it's extents.

Loop through the circle list and using your circle.Rect property find the minimum and maximum x and y.

zoom center origin X = (max_x + min_x) / 2
zoom center origin Y = (max_Y + min_y) / 2

delta_x = max_x - min_x
dekta_y = max_y - min_y

Use whichever delta is larger for the scale.


To keep things simple use a square picturebox.
__________________
Burn the land and boil the sea
You can't take the sky from me


~T

Last edited by Gruff; 05-04-2013 at 01:03 PM.
Reply With Quote
  #4  
Old 05-04-2013, 07:05 PM
supercrewed supercrewed is offline
Centurion
 
Join Date: Feb 2004
Posts: 152
Default

Thanks for your, I'm unable to find much out there on this, and I have never worked with graphics, I played with it, and everything is outside the picture box..
My full code is below, I'm still confused on how to scale everything to fit, and be proportional at the same time,

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

fileReader = My.Computer.FileSystem.OpenTextFileReader(AppPath() & "3313-3682.dxf")

While Not fileReader.EndOfStream
DXFData = DXFData & fileReader.ReadLine() & vbCrLf
End While
DXFArray = Split(DXFData, vbCrLf)

For i = 0 To UBound(DXFArray) - 1
If InStr(DXFArray(i), "AcDbCircle") > 0 And Len(DXFArray(i)) = Len("AcDbCircle") Then
TextBox1.Text = TextBox1.Text & ("X" & DXFArray(i + 2) & " Y" & DXFArray(i + 4)) & " R" & DXFArray(i + 8) & vbCrLf

Dim g As Graphics = PictureBox1.CreateGraphics
Dim myPen As New Pen(Color.Green)
myPen.Width = 1

Dim oMatrix As Matrix = New Matrix(1, 0, 0, -1, 0, 0)
oMatrix.Scale(0.00001, 0.00001) ' <-- Double Scale I think
oMatrix.Translate(0.0, -(0.0))


g.PageUnit = GraphicsUnit.Inch
g.Transform.Reset()
g.Transform = oMatrix
'Get rid of jaggy graphics
g.SmoothingMode = SmoothingMode.HighQuality

g.DrawLine(myPen, 0, 0, 10, 10)

g.DrawEllipse(myPen, New Rectangle(DXFArray(i + 2), DXFArray(i + 4) , DXFArray(i + 8) , DXFArray(i + 8) ))

End If


Next



End Sub

Last edited by supercrewed; 05-04-2013 at 07:44 PM. Reason: fix error in code
Reply With Quote
  #5  
Old 05-06-2013, 10:12 AM
Gruff's Avatar
GruffScaling x,y coordinates in a picture box Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

Okay, Answer some questions before I waste another four hours of my time.

Do you understand what I mean by a virtual boundary rectangle around your circles?

Imagine you draw 30 random circles on a sheet of paper. Now find the furthest tangent vertical line to the left. Do the same to the right, then top and bottom. This is the exact extents of the area needed to display your circles. The corners are Min and Max x and y.

What you want is the Size of the rectangle. i.e. the delta of the min and max. (max - min).

So now you have a width and height of a virtual rectangle.

But you are not done. What you need to take into account is that your virtual rectangle is almost never square. You have to find out which side is greater and use that to calculate your scale , otherwise your scale will crop off some of the outer geometry.

You have do do the same thing with your picturebox except you want the smallest side. (unless it is square.) Convert its units to inches. Use the
Graphics .DpiX or .DpiY conversion values.

You now have two numbers. virtual rectangle largest side and picturebox smallest side.

Now to fit the virtual rectangle inside the picturebox rectangle you will need a scale factor.

Scale can also be thought of as a ratio. n1 / n2.
Which number comes first is important.

To be on the safe side you will want to adjust your scale by 2 to 5 percent just in case your circles abut the picturebox too closely.

do the math and use the result for both arguments in your matrix.scale() method.
(If your scale arguments are different it will distort your geometry. circles will show as ellipses.)

The last thing you need to do is calculate the center of the virtual rectangle.
This time though you want the actual coordinates not a delta.

This number you plug into the last two arguments of your New Matrix statement.
It will center your virtual rectangle in your picture box.

If it the above is not clear do what I did the first time I tried to figure out autoscaling.
Cut out a small and large piece of paper. Draw some circles on the larger.
Walk through this post with those in hand.

Finally. Another good idea is to make a small test app that does not read values from your DXF. Instead draw a circle and a square with known values.
Say Three inches Diameter. Draw them at 0,0. Make sure your picture box is greater than 3 inches in size. Now play with the Matrix values and scales. A scale of 1.0 should display the circle.
__________________
Burn the land and boil the sea
You can't take the sky from me


~T

Last edited by Gruff; 05-06-2013 at 10:41 AM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Scaling x,y coordinates in a picture box
Scaling x,y coordinates in a picture box
Scaling x,y coordinates in a picture box Scaling x,y coordinates in a picture box
Scaling x,y coordinates in a picture box
Scaling x,y coordinates in a picture box
Scaling x,y coordinates in a picture box Scaling x,y coordinates in a picture box Scaling x,y coordinates in a picture box Scaling x,y coordinates in a picture box Scaling x,y coordinates in a picture box Scaling x,y coordinates in a picture box Scaling x,y coordinates in a picture box
Scaling x,y coordinates in a picture box
Scaling x,y coordinates in a picture box
 
Scaling x,y coordinates in a picture box
Scaling x,y coordinates in a picture box
 
-->