我正在尝试绘制爆炸。通过draw,我的意思是使用e.graphics
函数而不是使用a Picturebox
(就像我上次那样)。
现在我的问题是,我该怎么做呢?在我的脑海中,我正在考虑使用e.graphics.fillrectangle(x,y,w,h)
和...改变位置和颜色来创建类似于爆炸的合成图像。但是,这个过程似乎有点失败,因为我必须尝试尝试这样做 - 有没有办法更有效地做到这一点?
您可能会发现 FillPolygonPoint 方法更有用。
想象一下你爆炸的所有点:
然后绘制它的代码看起来类似于:
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
动态创建星点的算法(在 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 并添加一些随机化以使其具有爆炸性的外观。您可以通过矩阵乘法转置(移动)和缩放点。