Click here to Skip to main content
15,884,298 members
Articles / Programming Languages / XML

Marching Ants Revisited

Rate me:
Please Sign up or sign in to vote.
4.35/5 (11 votes)
21 Sep 20052 min read 44.6K   829   32   6
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...

VB
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:
    VB
    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.


Written By
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Maxwolf Goodliffe13-Dec-14 23:48
Maxwolf Goodliffe13-Dec-14 23:48 
GeneralNice!!! Pin
msalmank28-Sep-05 6:51
msalmank28-Sep-05 6:51 
GeneralRe: Nice!!! Pin
Keith Wilson29-Sep-05 3:42
Keith Wilson29-Sep-05 3:42 
GeneralNice! Pin
Frank Hileman27-Sep-05 11:38
Frank Hileman27-Sep-05 11:38 
GeneralRe: Nice! Pin
Christoph Buenger27-Sep-05 20:01
professionalChristoph Buenger27-Sep-05 20:01 
very impressive, the VG.net library! looks cool.

regards
Christoph




__________________________________________________
Christoph Buenger Software
web: www.cbuenger.com
mail: contact@cbuenger.com
__________________________________________________

NEW: mceWeather wins the "Europe xpMCE Award 2004/2005": http://www.mce-community.de/award

mceAuction won the eBay®-Award "Most innovative Buyer's Application 2005"!
Read all the details on http://www.cbuenger.com
GeneralRe: Nice! Pin
Keith Wilson29-Sep-05 3:43
Keith 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.