Click here to Skip to main content
Click here to Skip to main content
Go to top

Marching Ants Revisited

, 21 Sep 2005
Rate this:
Please Sign up or sign in to vote.
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 got 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

Share

About the Author

Keith Wilson
Web Developer
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
GeneralNice!!! Pinmembermsalmank28-Sep-05 6:51 
GeneralRe: Nice!!! PinmemberKeith Wilson29-Sep-05 3:42 
GeneralNice! PinmemberFrank Hileman27-Sep-05 11:38 
GeneralRe: Nice! PinmemberChristoph Buenger27-Sep-05 20:01 
GeneralRe: Nice! PinmemberKeith Wilson29-Sep-05 3:43 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 22 Sep 2005
Article Copyright 2005 by Keith Wilson
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid