locked
How do I make an infinite grid in visual studios? RRS feed

  • Question

  • I am designing a robot that can be used to map any area it is put in and I am using visual studios to create the grid. The problem I am having is that I do not know how to create a grid with infinite blocks. Can someone please help me? I will be attaching the code I used to make my grid once I am able <g class="gr_ gr_8 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-del replaceWithoutSep" data-gr-id="8" id="8">to,</g> if it is needed.

    Here are screenshot shots of the code and its results. The blue square is the robot and the black streaks shows the path the robot has taken(This was just a test run to see how it would look)

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                this.Width = 1920;
                this.Height = 1080;
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    
            private void Form1_Paint(object sender, PaintEventArgs e)
            {
                Graphics g = e.Graphics;
                Pen myPen = new Pen(Color.Black); //Draws the borders around the shape
                Brush myBrush = new SolidBrush(Color.Blue);//Draws the interior of the shape
    
                Rectangle rect = new Rectangle(900,500, 9, 9);
                g.DrawRectangle(myPen, rect); // Draws the Rectangle to the screen
                e.Graphics.FillRectangle(myBrush, rect);
    
                for (int i = 0; i < 1920; i = i + 12)
                {
                    rect = new Rectangle(0 + i, 0, 12, 12);
                    g.DrawRectangle(myPen, rect);
    
                    for (int j = 0; j < 1080; j = j + 12)
                    {
                        rect = new Rectangle(0 + i, 0 + j, 12, 12);
                        g.DrawRectangle(myPen, rect);
    
                    }
    
                }
                for (int i=0; i< 100; i++)
                {
    
                    
                    rect.X = 900; rect.Y = 500 - (i+9);
                    g.DrawRectangle(myPen, rect); // Draws the Rectangle to the screen
                    e.Graphics.FillRectangle(myBrush, rect);
                }
    
            }
    
            private void Form1_Load_1(object sender, EventArgs e)
            {
    
            }
        }
    }
    


    • Edited by gamebo5000 Thursday, January 11, 2018 5:10 PM
    Wednesday, January 10, 2018 6:46 PM

All replies

  • Hi

    There is not really an infinite size Grid available anywhere since you can't define a location for the last cell as there is no last cell.

    However, here is code that will provide 1000 x 1000 cell Grid of 20 x 20 cell size. Each cell has Properties that can be anything at all, but here, just name,location,size and a mysterious Object that is out of this world.

    There is no display of the Grid as you didn't specify that.

    ' here is  100000 cells
    ' in a 1000 x 1000 Grid
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim Grid As New List(Of Gcell)
      Class Gcell
    	Property Name As String
    	Property Size As Size
    	Property Location As Point
    	Property WhatEverNext As Object
      End Class
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    	Dim x, y As Integer
    	For i As Integer = 1 To 1000 * 1000
    	  Dim c As New Gcell
    	  With c
    		.Name = i.ToString
    		.WhatEverNext = New Object
    		.Size = New Size(20, 20)
    		.Location = New Point(x, y)
    		x += 20
    		If x > 1000 * 20 Then
    		  x = 0
    		  y += 20
    		End If
    	  End With
    	  Grid.Add(c)
    	Next
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    Wednesday, January 10, 2018 7:30 PM
  • The problem I am having is that I do not know how to create a grid with infinite blocks.
    You can't create an infinite grid.  What you can do is extend the grid into whatever new area the exploration requires.  It therefore appears infinite because wherever the robot goes there is a grid available.  How you do that depends on how you are creating the existing grid (your code hasn't appeared yet) and how the robot explores.
    Wednesday, January 10, 2018 8:28 PM
  • I tend to think of this differently than the others in that any "grid" is infinite until you define some bounds for its axes.  Obviously you can't store information about infinite points in a world-space without infinite memory storage, so I would suspect that for this problem, thinking of the world in the terms of an infinite grid of 2D space is probably not the way to go - particularly if you are interested in storing information about each location (grid cell).  Creating any kind of 2d array or list-of-lists becomes impractical quickly due to the amount of data recorded (memory consumed).  It is also rare that every location is interesting.  Typically there are only specific points that contain anything worth knowing about so you end up with a lot of potentially empty cells in your grid data.

    Instead of thinking in terms of a grid, it might be better to think in terms of areas-of-interest, or spheres-of-influence.  In other words, think in circles instead of squares.  An area of interest in 2D space can be thought of as a Circle, and a Circle in code is just a center point and a radius (three singles).  It is fast and efficient to check to see if a point is within a circle (is the distance between the two points less than the radius of the circle?) and you can store information about a large area with a single identifier.  This way you only need to create data records for things that interest you and you can avoid having data that records empty space.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, January 11, 2018 3:51 PM
  • That is what I am currently trying to do. I uploaded my code now.
    Thursday, January 11, 2018 5:11 PM
  • That is what I am currently trying to do. I uploaded my code now.

    Maybe this is what you want to do?

    Public Class Form2
        Private WithEvents Pic As New PictureBox With {.Parent = Me}
        Private GridRect As Rectangle
        Private GridSize As New Size(30, 30)   'square size in pixels
        Private GridOrigin As Point             'upper left corner
        Private BackgroundBmp As New Bitmap("c:\bitmaps\bg1.png")
        Private S1Bmp As New Bitmap("c:\bitmaps\spaceship1.png")
        Private S1Loc As Point
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles Me.Load
            DoubleBuffered = True
            BackColor = Color.DimGray
            KeyPreview = True
            ClientSize = New Size(500, 300)
            Form2_Resize(0, Nothing)
        End Sub
    
        Private Sub Form2_Resize(sender As Object, e As EventArgs) Handles Me.Resize
            Dim border As Integer = 10
            Dim header As Integer = 20
            Pic.Location = New Point(border, border + header)
            Pic.Size = New Size(ClientSize.Width - (2 * border), ClientSize.Height - (header + (2 * border)))
            Pic.Invalidate()
        End Sub
    
        Private Sub Form2_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
    
            Select Case e.KeyCode
                Case Keys.Left
                    S1Loc.X -= GridSize.Width
                    If S1Loc.X < GridOrigin.X Then GridOrigin.X -= GridSize.Width
                Case Keys.Right
                    S1Loc.X += GridSize.Width
                    If S1Loc.X > GridOrigin.X + Pic.ClientSize.Width - GridSize.Width Then GridOrigin.X += GridSize.Width
                Case Keys.Up
                    S1Loc.Y -= GridSize.Height
                    If S1Loc.Y < GridOrigin.Y Then GridOrigin.Y -= GridSize.Height
                Case Keys.Down
                    S1Loc.Y += GridSize.Height
                    If S1Loc.Y > GridOrigin.Y + Pic.ClientSize.Height - GridSize.Height Then GridOrigin.Y += GridSize.Height
                Case Else
                    Exit Sub
            End Select
    
            Pic.Invalidate()
        End Sub
    
        Private Sub Pic_Paint(sender As Object, e As PaintEventArgs) Handles Pic.Paint
            Dim gWidth As Single = CSng(GridSize.Width * Math.Ceiling(Pic.ClientSize.Width / GridSize.Width))
            Dim gHeight As Single = CSng(GridSize.Height * Math.Ceiling(Pic.ClientSize.Width / GridSize.Height))
            Dim x1 As Single = GridOrigin.X
            Dim y1 As Single = GridOrigin.Y
    
            With e.Graphics
                .Clear(Color.Black)
                .TranslateTransform(-GridOrigin.X, -GridOrigin.Y)       'move the drawing origin to the grid corner gx, gy
    
                Using p As New Pen(Color.White, 1),
                        pRect As New Pen(Color.Red, 1),
                        br As New SolidBrush(Color.Blue),
                        f As New Font("arial", 10)
    
                    'draw background
                    Dim bgRect As New Rectangle(3, -(25 + GridSize.Height), 2 * BackgroundBmp.Width, 2 * BackgroundBmp.Height)
                    .DrawImage(BackgroundBmp, bgRect)
    
                    'draw grid
                    p.DashStyle = Drawing2D.DashStyle.Dot
    
                    For x As Single = x1 To x1 + gWidth Step GridSize.Width
                        .DrawLine(p, x, GridOrigin.Y, x, GridOrigin.Y + gHeight)
                        .DrawString(x.ToString("f0"), f, br, x, y1)
                    Next
    
                    For y As Single = y1 To y1 + gHeight Step GridSize.Height
                        .DrawLine(p, GridOrigin.X, y, GridOrigin.X + gWidth, y)
                        If y <> y1 Then .DrawString(y.ToString("f0"), f, br, x1, y)
                    Next
    
                    'draw sprite
                    Dim destRect As New Rectangle(S1Loc.X, S1Loc.Y, GridSize.Width, GridSize.Height)
                    .DrawImage(S1Bmp, destRect)
                End Using
            End With
        End Sub
    End Class


    Thursday, January 11, 2018 6:44 PM
  • Thank you, this is extremely helpful. Is there a tutorial on how to work with this? <g class="gr_ gr_6 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="6" id="6">Im</g> still learning how to use visual studios. Specifically replacing the keys part of the code. With something more autonomous. 
    • Edited by gamebo5000 Thursday, January 11, 2018 8:23 PM
    Thursday, January 11, 2018 8:09 PM
  • Thank you, this is extremely helpful. Is there a tutorial on how to work with this? <g class="gr_ gr_6 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="6" id="6">Im</g> still learning how to use visual studios. Specifically replacing the keys part of the code. With something more autonomous. 

    No there is not single thing.

    When the app is not running, ie design mode, click the mouse pointer on a word you don't understand like keycode, then press the F1 (help) key on the keyboard. Your browser will open and show the msdn docs for the word. Sometimes you have to do it twice the first one does not go.

    Now you see the code words you can search those words like vb.net how to make a game.

    Please note this is visual basic code forum and the example is visual basic. However c# is very similar to vb and there are code converters like telerick? you can use to convert the example search for vb.net to c#.

    And finally if you want to use c# (I assume that is what you posted) then I guess you have to go to that forum but we like this here too.


    PS the important thing is this is an infinite grid and one way to do it. If you want to use the mouse and timer this is the basis of it. Then you add the things Reed and the others talk about.
    Thursday, January 11, 2018 8:34 PM
  • Is there a tutorial on how to work with this? 

    The code you have posted is C#.  You need to convert it to VB and update the post.

    The part of your code that is required for implementing an 'infinite' grid is where the point of interest moves off the grid as drawn.   It appears from your code that in that case you won't extend the grid in that direction, but instead will shift whatever already exists in the grid (path. obstacles etc.) off in the other direction, so that the grid appears to have been extended and then moved to accommodate the new point of interest.   So you need to show the code that draws the items onto the grid, and the code that moves the point of interest to the grid boundary, creating the need to move everything.

    Thursday, January 11, 2018 8:37 PM
  • Thank you, this is extremely helpful. Is there a tutorial on how to work with this? <g class="gr_ gr_6 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="6" id="6">Im</g> still learning how to use visual studios. Specifically replacing the keys part of the code. With something more autonomous. 

    PS Here I added a mouse joystick control panel. Click for the direction the sprite moves.

    You have to learn one thing at a time and debug it and get it working. Do too much you cant run it. You cant debug it. You don't understand it. You get aggrevated. 

    So, see how you add each thing one at a time, try to make it object oriented etc.

    We don't  know exactly what your skill level is. Maybe you know all this.   :)

    'infinite drawing grid v2 with mouse joystick control
    Public Class Form2
        Private WithEvents Pic As New PictureBox With {.Parent = Me}    'add picturebox to the form
        Private GridSize As New Size(30, 30)    'square size in pixels
        Private GridOrigin As Point             'upper left corner
        Private BackgroundBmp As New Bitmap("c:\bitmaps\bg1.png")
        Private S1Bmp As New Bitmap("c:\bitmaps\spaceship1.png")
        Private S1Loc As Point                  'sprite location
        Private S1CtrlRect As Rectangle         'control panel size and location
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles Me.Load
            DoubleBuffered = True
            BackColor = Color.DimGray
            KeyPreview = True
            ClientSize = New Size(500, 300)
            Form2_Resize(0, Nothing)
        End Sub
    
        Private Sub Form2_Resize(sender As Object, e As EventArgs) Handles Me.Resize
            Dim border As Integer = 10
            Dim header As Integer = 20
            Pic.Location = New Point(border, border + header)
            Pic.Size = New Size(ClientSize.Width - (2 * border), ClientSize.Height - (header + (2 * border)))
    
            'control panel location
            S1CtrlRect = New Rectangle(Pic.ClientSize.Width - (3 * GridSize.Width),
                                     Pic.ClientSize.Height - (3 * GridSize.Height),
                                     3 * GridSize.Width, 3 * GridSize.Height)
    
            Pic.Invalidate()
        End Sub
    
        Private Sub Form2_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
    
            Select Case e.KeyCode
                Case Keys.Left
                    S1Loc.X -= GridSize.Width
                    If S1Loc.X < GridOrigin.X Then GridOrigin.X -= GridSize.Width
                Case Keys.Right
                    S1Loc.X += GridSize.Width
                    If S1Loc.X > GridOrigin.X + Pic.ClientSize.Width - GridSize.Width Then GridOrigin.X += GridSize.Width
                Case Keys.Up
                    S1Loc.Y -= GridSize.Height
                    If S1Loc.Y < GridOrigin.Y Then GridOrigin.Y -= GridSize.Height
                Case Keys.Down
                    S1Loc.Y += GridSize.Height
                    If S1Loc.Y > GridOrigin.Y + Pic.ClientSize.Height - GridSize.Height Then GridOrigin.Y += GridSize.Height
                Case Else
                    Exit Sub
            End Select
    
            Pic.Invalidate()
        End Sub
    
        Private Sub Pic_Paint(sender As Object, e As PaintEventArgs) Handles Pic.Paint
            Dim gWidth As Single = CSng(GridSize.Width * Math.Ceiling(Pic.ClientSize.Width / GridSize.Width))
            Dim gHeight As Single = CSng(GridSize.Height * Math.Ceiling(Pic.ClientSize.Width / GridSize.Height))
    
            With e.Graphics
                .Clear(Color.Black)
                .TranslateTransform(-GridOrigin.X, -GridOrigin.Y)       'move the drawing origin to the grid corner gx, gy
    
                Using p As New Pen(Color.White, 1),
                        br As New SolidBrush(Color.Blue),
                        f As New Font("arial", 10)
    
                    'draw background
                    Dim bgRect As New Rectangle(3, -(25 + GridSize.Height), 2 * BackgroundBmp.Width, 2 * BackgroundBmp.Height)
                    .DrawImage(BackgroundBmp, bgRect)
    
                    'draw grid
                    p.DashStyle = Drawing2D.DashStyle.Dot
    
                    For x As Single = GridOrigin.X To GridOrigin.X + gWidth Step GridSize.Width
                        .DrawLine(p, x, GridOrigin.Y, x, GridOrigin.Y + gHeight)
                        .DrawString(x.ToString("f0"), f, br, x, GridOrigin.Y)
                    Next
    
                    For y As Single = GridOrigin.Y To GridOrigin.Y + gHeight Step GridSize.Height
                        .DrawLine(p, GridOrigin.X, y, GridOrigin.X + gWidth, y)
                        If y <> GridOrigin.Y Then .DrawString(y.ToString("f0"), f, br, GridOrigin.X, y)
                    Next
    
                    'draw sprite
                    Dim destRect As New Rectangle(S1Loc.X, S1Loc.Y, GridSize.Width, GridSize.Height)
                    .DrawImage(S1Bmp, destRect)
    
                    'draw control panel
                    DrawControls(e.Graphics, S1CtrlRect)
    
                End Using
            End With
        End Sub
    
        Private Sub DrawControls(g As Graphics, rect As Rectangle)
            With g
                .ResetTransform()
                .SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
    
                Using p As New Pen(Color.Maroon, 7),
                        pCtrl As New Pen(Color.Maroon, 2),
                        brCtrl As New SolidBrush(Color.BlanchedAlmond)
                    p.EndCap = Drawing2D.LineCap.ArrowAnchor
    
                    .FillRectangle(brCtrl, rect)
                    .DrawRectangle(pCtrl, rect)
    
                    .TranslateTransform(rect.X + CSng(rect.Width / 2), rect.Y + CSng(rect.Height / 2))
    
                    For a As Integer = 0 To 270 Step 90
                        .RotateTransform(a)
                        .DrawLine(p, 5, 0, CInt(0.45 * rect.Width), 0)
                    Next
    
                End Using
                .ResetTransform()
            End With
        End Sub
    
        Private Sub Pic_MouseDown(sender As Object, e As MouseEventArgs) Handles Pic.MouseDown
            If S1CtrlRect.Contains(e.Location) Then
                'determine the direction using the angle to the click point
                'from the center of the control panel
                Dim w2 As Single = CSng(S1CtrlRect.Width / 2)
                Dim h2 As Single = CSng(S1CtrlRect.Height / 2)
                Dim rx As Single = Pic.ClientSize.Width - w2
                Dim ry As Single = Pic.ClientSize.Height - h2
                Dim dx As Single = e.X - rx
                Dim dy As Single = e.Y - ry
                Dim angle As Double = Math.Atan2(dy, dx) * 57.8
                If angle < 0 Then angle += 360
                If angle > 360 Then angle -= 360
    
                Select Case angle
                    Case < 45, > 315     'Right 
                        S1Loc.X += GridSize.Width
                        If S1Loc.X > GridOrigin.X + Pic.ClientSize.Width - GridSize.Width Then GridOrigin.X += GridSize.Width
                    Case < 135          'Down
                        S1Loc.Y += GridSize.Height
                        If S1Loc.Y > GridOrigin.Y + Pic.ClientSize.Height - GridSize.Height Then GridOrigin.Y += GridSize.Height
                    Case < 225          'left
                        S1Loc.X -= GridSize.Width
                        If S1Loc.X < GridOrigin.X Then GridOrigin.X -= GridSize.Width
                    Case Else  '< 315   'up
                        S1Loc.Y -= GridSize.Height
                        If S1Loc.Y < GridOrigin.Y Then GridOrigin.Y -= GridSize.Height
                End Select
    
                Pic.Invalidate()
            End If
        End Sub
    End Class

    Friday, January 12, 2018 12:38 AM
  •  OT - Cool Tom,  this kind of reminds me of the Defender game i use to play on my Atari 2600 back in the day.  8)

    If you say it can`t be done then i`ll try it

    Friday, January 12, 2018 12:48 AM
  •  OT - Cool Tom,  this kind of reminds me of the Defender game i use to play on my Atari 2600 back in the day.  8)

    If you say it can`t be done then i`ll try it

    Heh, heh, yeah, Thanks.

    I never had Defender. This could be like my lunar lander game on the Commador 64. Of course this is a much improved version. Thanks Microsoft et al!

    Friday, January 12, 2018 1:33 AM
  • Thanks for being so helpful. This will help me complete my senior project, You're a life saver.
    Sunday, January 14, 2018 11:34 PM
  • Thanks for being so helpful. This will help me complete my senior project, You're a life saver.

    You are welcome.

    Its a learning example then not a copying example. You still have much to do to complete what you described. :)

    To close the question please mark the post(s) that helped as the answer using the link at the bottom of the post.

    Monday, January 15, 2018 1:33 AM
  • Is this the full code or partial?
    Wednesday, January 24, 2018 3:13 PM