1.

Solve : How to draw Isometric Grid Visual Basic .Net?

Answer»

This isn't exactly a question, more of a statement

On other websites, which i'm not a USER on, such as experts-exchange (i think? Or other ones anyway), people write up how to do things so others don't have to ask, and I don't know if anyone will ask this, but incase they do heres some visual basic.net code (vs 2003), a class, which makes an ISOMETRIC grid using GDI+ when provided with four corners:

Code: [Select]Public Class IsometricBase 'written by Hammond Pearce
'this class creats a set of points to make an isometric grid like so: (ASCII ART FOLLOWS - MONOSPACE FONT REQUIRED)
' 3________C__________4
' / /
' / /
' B/ /D
' / /
' /__________________ /
' 1 A 2

Private sideApoints As Point() 'all the points along side A
Private sideBpoints As Point() 'all the points along side B
Private sideCpoints As Point() 'all the points along side C
Private sideDpoints As Point() 'all the points along side D
Private lastgridlines As Integer 'last gridlines used in calculatepoints, used in draw_all_points
Private Stored_Corners As Point() 'last corners used in calculatepoints, used in draw_all_points
Public ReadOnly Property SideA_Points() As Point()
Get
Return sideApoints
End Get
End Property
Public ReadOnly Property sideB_points() As Point()
Get
Return sideBpoints
End Get
End Property
Public ReadOnly Property sideC_points() As Point()
Get
Return sideCpoints
End Get
End Property
Public ReadOnly Property sideD_points() As Point()
Get
Return sideDpoints
End Get
End Property

Public Sub Calculatepoints(ByVal Corner1 As Point, ByVal Corner2 As Point, ByVal Corner3 As Point, ByVal Corner4 As Point, ByVal NumberOfGridlines As Integer)
'set up all variables
Stored_Corners = New Point(4) {New Point(0, 0), Corner1, Corner2, Corner3, Corner4} 'store the last points with an offset (the newpoint) so that its more userfriendly for me! :D
lastgridlines = NumberOfGridlines 'store the number of gridlines
Dim count As Integer 'used to put data in correct pocket in array
sideApoints = New Point(NumberOfGridlines) {} 'reset arrays ready to store
sideBpoints = New Point(NumberOfGridlines) {}
sideCpoints = New Point(NumberOfGridlines) {}
sideDpoints = New Point(NumberOfGridlines) {}
Dim temppoint As New Point 'used in do... loop as a temporary point to store values in

'side A
'reset common variables
count = 0
Do Until temppoint.X = Corner2.X And temppoint.Y = Corner2.Y 'do until the temppoint = the point we are working to
count += 1 'increment pocket
If count > NumberOfGridlines Then Exit Do
temppoint.X = Corner1.X - (Corner1.X - Corner2.X) / NumberOfGridlines * count 'these two instructions calculate the points between corner1 and corner3
temppoint.Y = Corner1.Y - (Corner1.Y - Corner2.Y) / NumberOfGridlines * count
sideApoints(count) = temppoint 'store the temppoint as a real point
Loop
'side C
count = 0
Do Until temppoint.X = Corner4.X And temppoint.Y = Corner4.Y 'do until the temppoint = the point we are working to
count += 1 'increment pocket
If count > NumberOfGridlines Then Exit Do
temppoint.X = Corner3.X - (Corner3.X - Corner4.X) / NumberOfGridlines * count 'these two instructions calculate the points between corner1 and corner3
temppoint.Y = Corner3.Y - (Corner3.Y - Corner4.Y) / NumberOfGridlines * count
sideCpoints(count) = temppoint 'store the temppoint as a real point
Loop
'sideB
count = 0
Do Until temppoint.X = Corner3.X And temppoint.Y = Corner3.Y 'do until the temppoint = the point we are working to
count += 1 'increment pocket
If count > NumberOfGridlines Then Exit Do
temppoint.X = Corner1.X + (Corner3.X - Corner1.X) / NumberOfGridlines * count 'these two instructions calculate the points between corner1 and corner3
temppoint.Y = Corner1.Y + (Corner3.Y - Corner1.Y) / NumberOfGridlines * count
sideBpoints(count) = temppoint 'store the temppoint as a real point
Loop
'sideD
count = 0
Do Until temppoint.X = Corner4.X And temppoint.Y = Corner4.Y
count += 1
If count > NumberOfGridlines Then Exit Do
temppoint.X = Corner2.X + (Corner4.X - Corner2.X) / NumberOfGridlines * count
temppoint.Y = Corner2.Y + (Corner4.Y - Corner2.Y) / NumberOfGridlines * count
sideDpoints(count) = temppoint
Loop
End Sub
Public Sub Draw_All_Points(ByVal graphicsobject As Graphics, ByVal P As PEN)
'this subroutine draws the isometric grid according to the sidepoints and the stored corners, using the lastgridlines variable to control the arrays
With graphicsobject
.DrawLine(P, Stored_Corners(1), Stored_Corners(2)) 'draw the SIDES
.DrawLine(P, Stored_Corners(1), Stored_Corners(3))
.DrawLine(P, Stored_Corners(2), Stored_Corners(4))
.DrawLine(P, Stored_Corners(4), Stored_Corners(3))
End With
Dim i As Integer
For i = 0 To lastgridlines
graphicsobject.DrawLine(P, SideA_Points(i), sideC_points(i)) 'draw the gridlines
Next
For i = 0 To lastgridlines
graphicsobject.DrawLine(P, sideB_points(i), sideD_points(i))
Next
End Sub

End Class
To use it you need to feed it four corners and the number of gridlines you want, also, if you want it to draw it for you, provide it with a graphics object and a pen to do it with. An example of this is here:
Code: [Select]Dim scr_graphics As Graphics = Me.CreateGraphics
Dim p As New Pen(Color.Green)
Dim corner1 As New Point(0, 200)
Dim corner2 As New Point(400, 400)
Dim corner3 As New Point(400, 0)
Dim corner4 As New Point(800, 200)
Dim myIB As New IsometricBase
Dim mygridlines As Integer = 10
myIB.Calculatepoints(corner1, corner2, corner3, corner4, mygridlines)
myIB.Draw_All_Points(scr_graphics, p)
Which results in:

All comments welcome! (Especially if they are methods of improvement)
Thanks,
Bones92Do your points support a Z axis value?

I had a similar transposition on my TI-83 calc- I called the program ThreeD, and it graphed 3d functions.

Not SURE if you'd have any use for it- but I used a fairly simple algorithm to convert X,Y and Z coordinates to X,Y coordinates that could be drawn:


DrawX = (X/2)+(Y/3)
DrawY = (Y/3)+(Z/2)

I imagine it would go into your code as a new procedure of some sort, for converting the isometric grid coordinates into a canvas location that you can draw to.
Funnily enough I did that last night afterwards, but slightly differently - you use an X and Y and Z from the grid the program draws itselfSince my above post, and as that was version 1.0, I am now up to version 4.2, which is capable of such drawings like the following:




I'll post code on request. I'm now working on layering, so I can have moving objects.Nice work!Hello,

I would like to know if anyone can help with this sample "Make a drawing application in VB .NET" (http://www.vb-helper.com/howto_net_drawing_framework.html).

I really loved the sample project and is helping me very much, but im stuck on a need i have that is moving the objects created.

In what way should i work so that after selecting an object with the mouse i could move it arround until it stops in a position.!!!!

I would be very thankfull if you could help me with this.

best regards,


Bruno Coelho
Quote from: jbscoelho on January 26, 2009, 01:19:29 PM

Hello,
Jbscoelho, please start your own topic.
Also, dont post your email, we dont work through email, and posting your email publicly on any forum can result in SPAM.


Discussion

No Comment Found