Click here to Skip to main content
12,078,090 members (47,118 online)
Click here to Skip to main content
Add your own
alternative version

Stats

35.6K views
572 downloads
31 bookmarked
Posted

Marching Ants Revisited

, 22 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

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
Maxwolf Goodliffe14-Dec-14 0:48
memberMaxwolf Goodliffe14-Dec-14 0:48 
GeneralNice!!! Pin
msalmank28-Sep-05 7:51
membermsalmank28-Sep-05 7:51 
GeneralRe: Nice!!! Pin
Keith Wilson29-Sep-05 4:42
memberKeith Wilson29-Sep-05 4:42 
GeneralNice! Pin
Frank Hileman27-Sep-05 12:38
memberFrank Hileman27-Sep-05 12:38 
GeneralRe: Nice! Pin
Christoph Buenger27-Sep-05 21:01
memberChristoph Buenger27-Sep-05 21:01 
GeneralRe: Nice! Pin
Keith Wilson29-Sep-05 4:43
memberKeith Wilson29-Sep-05 4: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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.160212.1 | Last Updated 22 Sep 2005
Article Copyright 2005 by Keith Wilson
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid