none
如何绘制出一个标准的五角星? RRS feed

  • 问题

  • 如果知道一个标准五角星的十个角的点坐标,那么画起来就很容易,但是如果不知道这些点坐标的话,要怎么样才能绘制出一个标准的五角星呀?还有像正五边形,正六边形这样的正多边形在不知道点坐标的情况下要怎么绘制呢?谢谢!
    xrhigh
    2009年4月3日 5:19

答案

  • 这个是完整的:
    要使用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
    DrawStar(ByVal g As Graphics, ByVal center As Point, ByVal radius As Integer, ByVal isSolid As Boolean)中center为五角星的中心点,radius为中心点到顶点的距离,可以成为它的半径,isSolid 指示画空心五角星还是实心五角星。
    然后我们在Button按钮的事件中加入如下调用代码:
    DrawStar(Me.CreateGraphics, New System.Drawing.Point(100, 100), 100, False)'画空心五角星
    DrawStar(Me.CreateGraphics, New System.Drawing.Point(300, 100), 100, True)'画实心五角星
    周雪峰
    2009年4月8日 10:38
    版主

全部回复

  • 你好!
         找到一段代码,希望对你有帮助:
    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
    周雪峰
    2009年4月3日 8:08
    版主
  • 请问这段代码的RotateTheta是何物啊?是一个自定义函数吗?怎么没有它的实现细节呀?
    xrhigh
    2009年4月8日 9:11
  • 这个是完整的:
    要使用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
    DrawStar(ByVal g As Graphics, ByVal center As Point, ByVal radius As Integer, ByVal isSolid As Boolean)中center为五角星的中心点,radius为中心点到顶点的距离,可以成为它的半径,isSolid 指示画空心五角星还是实心五角星。
    然后我们在Button按钮的事件中加入如下调用代码:
    DrawStar(Me.CreateGraphics, New System.Drawing.Point(100, 100), 100, False)'画空心五角星
    DrawStar(Me.CreateGraphics, New System.Drawing.Point(300, 100), 100, True)'画实心五角星
    周雪峰
    2009年4月8日 10:38
    版主