65.9K
CodeProject 正在变化。 阅读更多。
Home

2D 多边形绘图仪-动画制作器

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.57/5 (20投票s)

2004 年 7 月 15 日

CPOL

2分钟阅读

viewsIcon

107352

downloadIcon

4730

绘制多边形、操作它们并制作动画。

Sample Image - 2D_Poligon_Drawer-Animato.jpg

引言

我开始思考如何构建一个 2D 多边形动画制作器。首先,我需要一个好的方法来记忆我的图形。这篇文章基于我记忆 2D 图形的方式。我选择了一种非位图的方式。这是因为,我想要一种快速而简单的方法来动画我的图形。因此,我选择了一种 2D 矢量方法。在本文中,我提出了我选择的(简单)结构来表示我的图形并对其进行动画处理。我还提出一个图形编辑器来生成 2D 图形,以及一个从图形工具创建动画的工具。提供的下载中包含建议的 exe 和源代码。

想法

在位图中,我们有一组像素(每个像素引用一个调色板颜色),这些像素定义了一个图像。在我的方法中,我将图像定义为一组“多边形”。每个多边形由一组点组成(如 Graphics 类中的多边形)。每个多边形还具有“中心颜色”、“颜色”和“中心点”,笔刷颜色梯度从该点扩散。以下是使用的类

Sample screenshot

使用代码

请记住,GrpLst 包含对 GrpObj 的引用列表,而 GrpObj 包含对 Point 的引用列表。以下代码片段显示了如何使用这些类

// 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);

要渲染场景,我们将 ExtGrpLst 转换为 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();
   }

请参阅 canvas.redraw() 方法。

使用该工具

首先,通过“框架”->“新建”激活一个画布。然后,您可以绘制任何您想要的内容并尝试所有功能。您可以激活“动画”->“新建”。画布与“框架”相同,但具有存储帧和重新着色的功能。单击“动画”以开始您的动画。如果您加载(“动画”->“加载”)文件 codeP_anim2.anim,您可以通过单击“动画”来测试它。

Sample screenshot

常见问题解答

在绘制时关闭多边形,请单击您开始绘制的第一个节点(点)。

在此版本的工具中,您无法绘制线条和椭圆。

注释

我没有为该工具编写帮助文件,也许将来会编写。我很高兴帮助/解释任何问题。如果任何优秀的设计师/动画师使用该工具创建了一些很酷的东西,我很高兴看到他的作品。

更新日期:2004/07/26

添加了以下功能。添加/删除所选多边形的点。所选多边形的 X 和 Y 镜像。撤消功能。将场景保存为 JPG/BMP。加载背景图像。清除了一些错误。

Update Sample screenshot

© . All rights reserved.