扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
Graphics 对象提供了 DrawPolygon 让我们绘制多边形区域的边, FillPolygon 方法填充多边形区域。
如下代码为绘制一个空心的五角星:
Sub DrawStar(ByVal g As Graphics, ByVal center As Point, ByVal radius As Integer, ByVal isSolid As Boolean)
Dim pts(9) As Point
pts(0) = New Point(center.X, center.Y - radius)
pts(1) = RotateTheta(pts(0), center, 36.0)
Dim len As Double = radius * Math.Sin((18.0 * Math.PI / 180.0)) / Math.Sin((126.0 * Math.PI / 180.0))
pts(1)。X = CInt(center.X + len * (pts(1)。X - center.X) / radius)
pts(1)。Y = CInt(center.Y + len * (pts(1)。Y - center.Y) / radius)
Dim i As Integer
For i = 1 To 4
pts((2 * i)) = RotateTheta(pts((2 * (i - 1))), center, 72.0)
pts((2 * i + 1)) = RotateTheta(pts((2 * i - 1)), center, 72.0)
Next i
If isSolid = False Then
Dim mPen As New Pen(New SolidBrush(Color.Blue))
g.DrawPolygon(mpen, pts)'画一个空心五角星
Else
Dim mBrush As New SolidBrush(Color.Blue)
g.FillPolygon(mBrush, pts)'画一个实心的五角星
End If
End Sub
'旋转
Function RotateTheta(ByVal pt As Point, ByVal center As Point, ByVal theta As Double) As Point
Dim x As Integer = CInt(center.X + (pt.X - center.X) * Math.Cos((theta * Math.PI / 180)) - (pt.Y - center.Y) * Math.Sin((theta * Math.PI / 180)))
Dim y As Integer = CInt(center.Y + (pt.X - center.X) * Math.Sin((theta * Math.PI / 180)) + (pt.Y - center.Y) * Math.Cos((theta * Math.PI / 180)))
Return New Point(x, y)
End Function
DrawStar(ByVal g As Graphics, ByVal center As Point, ByVal radius As Integer, ByVal isSolid As Boolean)中center为五角星的中心点,radius为中心点到顶点的距离,可以成为它的半径,isSolid 指示画空心五角星还是实心五角