- 关 键 词:
- vb.net
要使用vb.net中的 Graphics 对象来绘制一个五角星,最重要的是需要获取五角星的10个点的坐标(5个顶点和5个凹点),这个需要通过数学公式来计算,但是我们这篇文章就不针对数学公式进行深入讨论,只来了解下 Graphics 对象绘制多边形的知识。
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
正文:http://www.qqread.com/book/myvbnet/200712213.html相关专题
- .NET移动与嵌入式技术 (5974篇文章)
- .NET开发手册 (5673篇文章)
- vb.net GDI+入门——使用DrawString显示文本 (0次浏览)
- VB2005中开发新一代控制台应用程序(1) (0次浏览)
- VB2005中开发新一代控制台应用程序(2) (0次浏览)
- VB2005中开发新一代控制台应用程序(3) (0次浏览)
- vb.net使用ListView 控件显示系统驱动器 (0次浏览)
- vb.net入门——ListView 控件的使用 (0次浏览)
- vb.net GDI+入门——使用Graphics对象填充图形 (0次浏览)
- .Net中ListView控件多选时复选框自动选择问题 (0次浏览)
- vb.net GDI+入门——使用Graphics对象绘制线图 (0次浏览)
- vb.net GDI+入门——了解Font类 (0次浏览)



