3 Replies - 518 Views - Last Post: 20 January 2013 - 10:30 AM Rate Topic: -----

#1 ivanov_o  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 16-January 13

Recursion or Loop?

Posted 19 January 2013 - 11:07 AM

Hi,
I'm working on the texture tile system for a small GDI+ game
and I'm wondering what approach should I take for the sub that
draws all the texture elements on the map - loop through
entire array of textures or call each texture element using a
recursion? I tried both ways and I can't see a difference,
it looks that both methods are performing with same speed,
here's my code:

 - Loop approach:

    Public Sub TileTexCells(e As System.Windows.Forms.PaintEventArgs)
        For i = 0 To UBound(TexCells)
          e.Graphics.DrawImage(TexCells(i).bmpFace, TexCells(i).ptrLocation)
        Next
    End Sub

 - Recursive approach:

'class declaration:
Public Class TexCell

    Public bmpFace As New Bitmap(128, 128, Imaging.PixelFormat.Format32bppPArgb)
    Public intSubscript As Integer
    Public ptrLocation As Point
   
    Public Sub TileTexcells(ByVal e As System.Windows.Forms.PaintEventArgs)
        e.Graphics.DrawImage(bmpFace, ptrLocation)
        If intSubscript < UBound(TexCells) Then _
           TexCells(intSubscript + 1).TileTexcells(e)
    End Sub

End Class

'usage:
    Public Sub On_Paint(ByVal e As System.Windows.Forms.PaintEventArgs)
        TexCells(0).TileTexcells(e)
    End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: Recursion or Loop?

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,072
  • Joined: 02-July 08

Re: Recursion or Loop?

Posted 19 January 2013 - 12:44 PM

I like the class handling it's painting - especially if it has a collection. It probably does not matter if both methods can do the job - I guess it's a design question. When I make GDI+ apps I like the objects to know how to do their own painting - you just tell them when to do it.
Was This Post Helpful? 1
  • +
  • -

#3 ivanov_o  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 16-January 13

Re: Recursion or Loop?

Posted 20 January 2013 - 05:13 AM

Thank you Hawk,
I've done some google research and it seems that in the most of the cases loops are better solution than recursions, but of course everything depends on the current situation, for now I'll will stick with the loops.
Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,072
  • Joined: 02-July 08

Re: Recursion or Loop?

Posted 20 January 2013 - 10:30 AM

Actually recursion is a form a looping - it just looks for children in a hierarchical design - which a simple loop would miss.

This post has been edited by _HAWK_: 20 January 2013 - 10:30 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1