Click here to Skip to main content
15,064,166 members
Articles / Programming Languages / XML
Posted 21 Sep 2005


32 bookmarked

Marching Ants Revisited

Rate me:
Please Sign up or sign in to vote.
4.35/5 (11 votes)
21 Sep 20052 min read
How to display marching ants around an array of points



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).


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)
    dashValues = Nothing

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

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.


  • 22/09/2005 - Initial upload


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.


About the Author

Keith Wilson
Web Developer
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

GeneralMy vote of 5 Pin
Maxwolf Goodliffe13-Dec-14 23:48
MemberMaxwolf Goodliffe13-Dec-14 23:48 
GeneralNice!!! Pin
msalmank28-Sep-05 6:51
Membermsalmank28-Sep-05 6:51 
GeneralRe: Nice!!! Pin
Keith Wilson29-Sep-05 3:42
MemberKeith Wilson29-Sep-05 3:42 
GeneralNice! Pin
Frank Hileman27-Sep-05 11:38
MemberFrank Hileman27-Sep-05 11:38 
GeneralRe: Nice! Pin
Christoph Buenger27-Sep-05 20:01
professionalChristoph Buenger27-Sep-05 20:01 
GeneralRe: Nice! Pin
Keith Wilson29-Sep-05 3:43
MemberKeith Wilson29-Sep-05 3:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.