如何在VB.NET中引发爆炸

时间:2012-08-22 15:41:28

标签: vb.net graphics 2d

我正试图引爆。通过绘制,我的意思是使用e.graphics函数而不是使用Picturebox(就像我上次那样)。

现在我的问题是,我到底该怎么做呢?在我看来,我正在考虑使用e.graphics.fillrectangle(x,y,w,h)并且很好......改变位置和颜色以创建类似于爆炸的合成图像。然而,这个过程似乎有点受到影响,因为我必须尝试尝试这样做 - 有没有办法更有效地做到这一点?

1 个答案:

答案 0 :(得分:2)

您可能会发现FillPolygonPoint方法更有用。

想象一下你爆炸的所有要点:

Explosion as Polygon

然后,绘制它的代码看起来像是:

Public Sub FillPolygonPoint(ByVal e As PaintEventArgs)

  ' Create solid brush. 
  Dim blueBrush As New SolidBrush(Color.Blue)

  ' Create points that define polygon. 
  Dim point1 As New Point(50, 50)
  Dim point2 As New Point(100, 25)
  Dim point3 As New Point(200, 5)
  Dim point4 As New Point(250, 50)
  Dim point5 As New Point(300, 100)
  Dim point6 As New Point(350, 200)
  Dim point7 As New Point(250, 250)
  Dim curvePoints As Point() = {point1, point2, point3, point4, _
    point5, point6, point7}

  ' Draw polygon to screen.
  e.Graphics.FillPolygon(blueBrush, curvePoints)
End Sub

作为outlined in the documentation provided by Microsoft

用于动态创建星点的算法(在C#中)可能如下所示:

using System;
using System.Drawing;
using System.Collections.Generic;

namespace Explosion
{
    class Program
    {
        static void Main(string[] args)
        {
            foreach (Point point in CreatePointsForStarShape(15, 200, 100))
            {
                Console.WriteLine(point);
            }
            Console.ReadLine();
        }

        public static IEnumerable<Point> CreatePointsForStarShape
                  (int numberOfPoints, int maxRadius, int minRadius)
        {
            List<Point> points = new List<Point>(numberOfPoints);

            for (
                 double angle = 0.0;
                 angle < 2* Math.PI;
                 angle += 2 * Math.PI / numberOfPoints
            )
            {
                // add outer point
                points.Add(CalculatePoint(angle, maxRadius));

                // add inner point
                points.Add(CalculatePoint
                    (angle + (Math.PI / numberOfPoints), minRadius));
            }

            return points;
        }

        public static Point CalculatePoint(double angle, int radius)
        {
            return new Point(
               (int)(Math.Sin(angle) * radius),
               (int)(Math.Cos(angle) * radius)
            );
        }
    }
}

这是输出......

{X=0,Y=200}
{X=20,Y=97}
{X=81,Y=182}
{X=58,Y=80}
{X=148,Y=133}
{X=86,Y=50}
{X=190,Y=61}
{X=99,Y=10}
{X=198,Y=-20}
{X=95,Y=-30}
{X=173,Y=-99}
{X=74,Y=-66}
{X=117,Y=-161}
{X=40,Y=-91}
{X=41,Y=-195}
{X=0,Y=-100}
{X=-41,Y=-195}
{X=-40,Y=-91}
{X=-117,Y=-161}
{X=-74,Y=-66}
{X=-173,Y=-99}
{X=-95,Y=-30}
{X=-198,Y=-20}
{X=-99,Y=10}
{X=-190,Y=61}
{X=-86,Y=50}
{X=-148,Y=133}
{X=-58,Y=80}
{X=-81,Y=182}
{X=-20,Y=97}
{X=0,Y=200}
{X=20,Y=97}

您必须将其转换为Visual Basic并添加一些随机化以使其具有爆炸性外观。您可以通过矩阵乘法转置(移动)和缩放点。