5,448,416 members and growing! (19,439 online)
Email Password   helpLost your password?
Multimedia » General Graphics » Graphics     Intermediate License: The Code Project Open License (CPOL)

2D Poligon Drawer-Animator

By acontoli

Draw poligons, manipulate them, and animate them.
C#Windows, .NET, .NET 1.0, .NET 1.1, WinXPVS.NET2002, VS.NET2003, Visual Studio, Dev

Posted: 14 Jul 2004
Updated: 1 Aug 2004
Views: 46,056
Bookmarked: 29 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
12 votes for this Article.
Popularity: 4.32 Rating: 4.00 out of 5
0 votes, 0.0%
1
1 vote, 8.3%
2
3 votes, 25.0%
3
0 votes, 0.0%
4
8 votes, 66.7%
5

Sample Image - 2D_Poligon_Drawer-Animato.jpg

Introduction

I started thinking of the way to build a 2D polygon animator. First of all, I needed a good stuff to memorize my graphics. This article is based on the way I can memorize some 2D graphics. I choose a non bitmap way. This is because, I want a fast and simple way to animate my graphics. So, I choose a 2D vectorial approach. In this article, I propose the (simple) structures I chose to represent my graphics and to animate it. I propose also a graphic editor to generate 2D graphics, and a tool to create animation from the graphics tool. The proposed exe and the source is in the provided download.

The idea

In a bitmap, we have a set of pixels (each of them refer to a palette color) that defines an image. In my approach, I define an image as a set of "polygons". Each polygon is composed by a set of points, (like the polygons of the Graphics class). Each polygon has also a "center color", a "color", and a "center point" from which diffuses the brush color gradient. Here are the classes used:

Sample screenshot

Using the code

Remember that GrpLst contains a list of references to GrpObj, and GrpObj contains a list of references to Point. Following snippet shows how to use the classes:

// create a List of poligons

ExtGrpLst g_l = new ExtGrpLst();

// to create a Poligon

pubO = new GrpObj();
pubO.color =  button4.BackColor;
pubO.centerColor = button1.BackColor;
// add a poligon to the list

g_l.addObj(pubO);
// I can add new point to the poligon like this (I'm plaing with references)

pubO.addPoint(new Point(e.X,e.Y);
// or like this (to add a point to the last poligon of the list)

g_l.addPoint(appo);

To render the scene, we transform the ExtGrpLst into a GraphicsPath.

foreach (GrpObj r in g_l.list)
   {
    PointF[] appo = new PointF[r.pointList.Count];
    int i = 0;
    foreach (PointF p in r.pointList)
    {
      appo[i] = p;
      i++;
    }
    GraphicsPath gr_pth = new GraphicsPath();
    gr_pthGlb.AddPolygon(appo);
    gr_pth.AddPolygon(appo);
    if (filled) // draw only lines or filled poligon

    {
       GraphicsPath g_p = new GraphicsPath();
       g_p.AddPolygon(appo);
       PathGradientBrush pgbrush= new PathGradientBrush(g_p);
       pgbrush.CenterPoint=r.centerPoint;
       pgbrush.CenterColor=r.centerColor;
       pgbrush.SurroundColors=new Color[] { r.color };
       offScreenDC.FillPolygon(pgbrush, appo);
       g_p.Dispose();
    }
       Pen pen = new Pen(r.color, 1);
       offScreenDC.DrawPath(pen,gr_pth);
       gr_pth.Dispose();
   }

See the canvas.redraw() method.

Using the tool

First of all, activate a canvas with Frame->New. Then you can draw what you want and try all the functions. You can activate Animation-->New. The canvas is the same as the Frame, with the possibility to store frames and recolor them. Click on Animate to start your animation. If you load (Animation-->Load) the file codeP_anim2.anim, you can test it by clicking "Animate".

Sample screenshot

FAQ

To close a polygon while drawing, click on the first node (point) from which you started.

In this version of the tool, you cannot draw lines and ellipses.

Notes

I didn't write a help file for the tool, maybe in the future. I'll be glad to help/explain any problem. If any good designer/animator will create something cool with that tool, I'll be glad to see his work.

Update 26/07/2004

Added the following features. Add/remove points to selected poligon. X and Y mirroring of selected poligons. Undo function. Save scene as JPG/BMP. Load a background image. Some bugs cleared.

Update Sample screenshot

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

acontoli



Occupation: Software Developer
Location: Italy Italy

Other popular General Graphics articles:

  • A flexible charting library for .NET
    Looking for a way to draw 2D line graphs with C#? Here's yet another charting class library with a high degree of configurability, that is also easy to use.
  • CxImage
    CxImage is a C++ class to load, save, display, transform BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, J2K images.
  • 3D Pie Chart
    A class library for drawing 3D pie charts.
  • Really cool visual FX
    A set of classes for doing stunning visual effects, including water, plasma and fire.
  • ImageStone
    An article on a library for image manipulation.

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 7 of 7 (Total in Forum: 7) (Refresh)FirstPrevNext
Subject  Author Date 
Generalmy homepagememberacontoli3:13 20 Sep '07  
GeneralHelp in graphics programming needed !!memberLalito806:02 19 Jun '07  
GeneralSpelling?memberColin Angus Mackay4:12 2 Aug '04  
GeneralRe: Spelling?sussMilkybar Kid14:06 3 Aug '04  
GeneralRe: Spelling?memberColin Angus Mackay14:15 3 Aug '04  
GeneralMemory hungrymemberDoubin2:11 15 Jul '04  
GeneralRe: Memory hungrymemberacontoli3:32 15 Jul '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 1 Aug 2004
Editor: Nishant Sivakumar
Copyright 2004 by acontoli
Everything else Copyright © CodeProject, 1999-2008
Web17 | Advertise on the Code Project