65.9K
CodeProject is changing. Read more.
Home

Marching Ants Revisited

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.35/5 (10 votes)

Sep 22, 2005

2 min read

viewsIcon

45369

downloadIcon

832

How to display marching ants around an array of points

Them!

Introduction

I needed to create a control to display user created 'zones' overlaid on a map of their choice. They needed to be able to manipulate these zones, so I thought it would be nice to display marching ants around the currently selected zone (in the demo, click on the picture box to start the ants).

Background

I searched the net for quite a while before I found a related article on CodeProject, namely: The Secret of Marching Ants by q123456789.

The solution presented, whilst not knocking q123456789 (otherwise I would have gotten nowhere), seemed incredibly complicated and bulky to implement so I decided to have a go myself. A comment from the article by jbialek, suggested the use of a textured brush to draw the ants and rotating the pattern. Whilst this worked, it did not give the effect I required. Another of the comments from the article (by Frank Hileman) set me on the right track. He suggested cyclically changing the DashOffset property of a dashed line to achieve the effect, something along the lines of...

    Public Sub drawAnts()
    
        'set length of dashes and spaces
        Dim dashValues As Single() = {5, 5}
        'white ants
        Dim whitePen As Pen = New Pen(System.Drawing.Color.White, Me.AntsWidth)
        whitePen.DashStyle = Drawing.Drawing2D.DashStyle.Dash
        whitePen.DashPattern = dashValues
        whitePen.DashOffset = blockpos
        'black background
        Dim blackPen As Pen = New Pen(System.Drawing.Color.Black, Me.AntsWidth)
        'draw background
        Me.Grafix.DrawPolygon(blackPen, Me.Outline)
        'draw ants
        Me.Grafix.DrawPolygon(whitePen, Me.Outline)
        'cleanup
        dashValues = Nothing
        blackPen.Dispose()
        whitePen.Dispose()
        Me.AntsImageHost.Invalidate()
        
    End Sub
    
    Private Sub Timer_tick(ByVal sender As Object, ByVal e As EventArgs)
    
        'increase the DashOffset variable and redraw the ants
        blockpos += 2
        If blockpos > 10 Then blockpos = 2
        drawAnts()
        Me.AntsImageHost.Invalidate()
        
    End Sub

Anyhow, since I was already storing my 'zones' as objects that exposed their outline as an array of points, it seemed to make sense to overdraw the outline using the array of points with a cycling DashOffset value. To this end, I created a class that accepts, as a minimum, an array of points, an image (to create the graphics object from), and the control hosting the image to Invalidate after drawing the ants.

Using the Code

  1. Add AntsObject.vb to your project.
  2. Dim a new AntsObject, passing it your points, image, and control:
        ao = New AntsObject(myPointsArray, myPicbox.Image, myPicBox)

It should be easy to overload the drawants() method in the AntsObject to draw rectangles, polygons, etc.

History

  • 22/09/2005 - Initial upload

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.