Click here to Skip to main content
Click here to Skip to main content

2D Poligon Drawer-Animator

By , 1 Aug 2004
 

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
Software Developer
Italy Italy
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5membermanoj kumar choubey21 Feb '12 - 23:51 
Nice
QuestionWhat time can test the links?Can be exported or load svg files?memberhudawei12 Apr '10 - 5:39 
What time can test the links?Can be exported or load svg files?
Your product is very good, thank you!
Generalreplymemberkold1618 Feb '09 - 11:34 
Im glad to see that others have the same interest in 2d polygon editing. The user interface is quite bothersome good job though.
GeneralRe: replymemberacontoli18 Feb '09 - 21:40 
You're right. You can find a better user interface here: http://www.codeproject.com/KB/graphics/SimpleVectorShapes.aspx[^]
 
dnk

GeneralNice ArticlememberAnand Ranjan Pandey5 Feb '09 - 21:50 
Hi Andrea, Your Article Helps me a lot. Thanks.Thumbs Up | :thumbsup:
 
Cheers by,
 
Anand Ranjan
developer@esspl
www.anandranjanp.blogspot.com

Generalmy homepagememberacontoli20 Sep '07 - 2:13 
If interested you my find my work also at :
http://acontoli.googlepages.com/[^]

 
dnk

GeneralHelp in graphics programming needed !!memberLalito8019 Jun '07 - 5:02 
Hi:
 
I am Eduardo, writing from Peru (Lima) and the author of program Secciones (http://sourceforge.net/projects/secciones/) developed with .NET 2 and CSharp and i realize that you have experience with graphics management, viewpoints, viewports, etc. and I was wondering if you can help me improve my little program Secciones.
 
If you have a look on my program, you will see that the graphics are poor and i need to improve them (now i am using ZedGraph). I would like to have a graphics interface like Autocad or any CAD Tools (maybe not so advanced), with the following features:
 
* Allow me to see the Polygons and 2d figures defined by (x,y) points in their real form (Scale on X = Scale on Y).
* Zooming functions (In / Out / Rectangular area)
* Panning
* Grids with points
* Able to have "sensible" areas, like the tag <MAP> of html.
* Obviously FREE or Open Source !!!
 

Is there any way to do this? Can you help on that, giving some directions or related information?
 
pd. Sorry for my bad english.
 
Thanks in advance.
Regards

 

Saludos,
 
Eduardo Rivera Alva
Teléfono: (51-1) 226-1440 (Oficina)
Celular : (51-1) 9-506-9736 (Móvil)
Email 1 : eriveraa@gmail.com
Email 2 : eriveraa@hotmail.com
Lima - Perú
----------------------------------------------------------
QuestionSpelling?memberColin Angus Mackay2 Aug '04 - 3:12 
Isn't "poligon" correctly spelt "polygon"? Or is this another crazy difference between US English and British English?
 

"If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell
 
Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!


AnswerRe: Spelling?sussMilkybar Kid3 Aug '04 - 13:06 

Well this guy does live in Italy, and English is likely his second language. So come on cut him some slack! Smile | :)
GeneralRe: Spelling?memberColin Angus Mackay3 Aug '04 - 13:15 
The comment was made after the article has passed through a CodeProject editor. (The heading of articles are blue [unedited contribution], orange [edited contribution] and grey [in purgatory]) This article is in orange so has passed through the editing process.
 
From my previous post I accept that I was unclear - my comment was based towards an editor (or potentially the original author who may alert the editor) that an error exists. The articles used to contain a link at the bottom of the page to the editor who proof read and edited the article.
 

"If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell
 
Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!


GeneralMemory hungrymemberDoubin15 Jul '04 - 1:11 
I like the idea of the editor.
 
Only thing it is quite memory hungry OMG | :OMG: 3 animations at once is imposible.
 
Pity it was a borland project Sigh | :sigh: , had to convert it manually to VS.2003 and fix an error.
 
Thank for making it.
GeneralRe: Memory hungrymemberacontoli15 Jul '04 - 2:32 
I'll update the dpwnload src with a .csproj file.
 
For the memory usage of the anim class, initialy a thought to start from a frame and store in other frame only the points moved. Maybe in future I'll try it.
 
I tried more than 3 animation at onec with no problem (only a slowdown..;P)
 
If U make me know the bug U fixed I'll update my source.
 
Thx.
 
dnk

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 2 Aug 2004
Article Copyright 2004 by acontoli
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid