Home / Software / Computer programming / How to draw Isometric Grid Visual Basic .Net
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] - (Bottom) Print
Author Topic: How to draw Isometric Grid Visual Basic .Net  (Read 6624 times)
Bones92
Topic Starter
Hopeful



Thanked: 3
Posts: 348

Computer: Specs
Experience: Experienced
OS: Windows 7
Website Designer and Microcontroller Programmer

PIC Programming New Zealand
« on: December 03, 2008, 10:43:00 PM »

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

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,
Bones92
IP logged

BC_Programmer
Mastermind


Thanked: 697
Posts: 15,880

Computer: Specs
Experience: Beginner
OS: Windows 7


Pinkie Pie is best pony

BC-Programming.com 1 1
« Reply #1 on: December 04, 2008, 11:07:17 AM »

Do 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.
IP logged

My Blog

BASeBlock 2.3.0 (NOW WITH MACGUFFINS!)
Bones92
Topic Starter
Hopeful



Thanked: 3
Posts: 348

Computer: Specs
Experience: Experienced
OS: Windows 7
Website Designer and Microcontroller Programmer

PIC Programming New Zealand
« Reply #2 on: December 04, 2008, 06:20:19 PM »

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 itself
IP logged

Bones92
Topic Starter
Hopeful



Thanked: 3
Posts: 348

Computer: Specs
Experience: Experienced
OS: Windows 7
Website Designer and Microcontroller Programmer

PIC Programming New Zealand
« Reply #3 on: December 28, 2008, 12:12:20 AM »

Since 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.
IP logged

BC_Programmer
Mastermind


Thanked: 697
Posts: 15,880

Computer: Specs
Experience: Beginner
OS: Windows 7


Pinkie Pie is best pony

BC-Programming.com 1 1
« Reply #4 on: December 28, 2008, 09:35:01 AM »

Nice work!
IP logged

My Blog

BASeBlock 2.3.0 (NOW WITH MACGUFFINS!)
jbscoelho
Newbie



Posts: 1


« Reply #5 on: January 26, 2009, 01:19:29 PM »

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
« Last Edit: January 26, 2009, 03:00:14 PM by Zylstra » IP logged
Zylstra
Moderator
Hacker



Thanked: 37
Posts: 5,274

Certifications: List
Experience: Guru
OS: Windows 7
The Techinator!

Technology News and Information 1
« Reply #6 on: January 26, 2009, 03:01:11 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.
IP logged

Pages: [1] - (Top) Print 
Home / Software / Computer programming / How to draw Isometric Grid Visual Basic .Net « previous next »
 


Login with username, password and session length

Old Forum Search | Forum Rules
Copyright © 2010 Computer Hope ® All rights reserved.
Powered by SMF 2.0 RC3 | SMF © 2006–2010, Simple Machines LLC
Page created in 0.09 seconds with 20 queries.