Draw Hexagon : Hexagon « 2D Graphics « VB.Net Tutorial






Draw Hexagon
'Visual Basic.Net JingCai Programming 100 Examples
'Author: Yong Zhang
'Publisher: Water Publisher China
'ISBN: 750841156


Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

public class DrawHexagon
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class


Public Class Form1
    Inherits System.Windows.Forms.Form

    Public Sub New()
        MyBase.New()

        InitializeComponent()
    End Sub

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    Private components As System.ComponentModel.IContainer

    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.PictureBox1 = New System.Windows.Forms.PictureBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'PictureBox1
        '
        Me.PictureBox1.BackColor = System.Drawing.SystemColors.Window
        Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.PictureBox1.Dock = System.Windows.Forms.DockStyle.Top
        Me.PictureBox1.Location = New System.Drawing.Point(0, 0)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(504, 304)
        Me.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize
        Me.PictureBox1.TabIndex = 0
        Me.PictureBox1.TabStop = False
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(160, 320)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(80, 32)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Hexagon"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(296, 320)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(80, 32)
        Me.Button2.TabIndex = 2
        Me.Button2.Text = "Circle"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(504, 366)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.PictureBox1)
        Me.ResumeLayout(False)

    End Sub

    Dim pen1 As New System.Drawing.Pen(Color.Green, 0.4)
    Dim g As System.Drawing.Graphics
    Const Max = 20
    Const pi = 3.1415926
    Dim dx(Max), dy(Max) As Double
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        g = PictureBox1.CreateGraphics
        PictureBox1.Refresh()
        Dim i As Integer
        Dim s, r As Double
        i = 6
        s = 80
        r = 0.34
        setdate(6)
        snow(200.0#, 180.0#, 6, i, s, r)
    End Sub

    Private Sub Sixth(ByVal x, ByVal y, ByVal n, ByVal s)
        Dim i, j, x1, x2, y1, y2 As Integer
        For i = 0 To n
            If i = n Then
                j = 1
            Else
                j = i + 1
            End If
            x1 = Int(x + dx(i) * s) - 50
            y1 = Int(y + dy(i) * s) - 55
            x2 = Int(x + dx(j) * s) - 50
            y2 = Int(y + dy(j) * s) - 55
            g.DrawLine(pen1, x1, y1, x2, y2)
        Next
    End Sub

    Private Sub snow(ByVal x, ByVal y, ByVal n, ByVal i, ByVal s, ByVal r)
        Dim t, t1 As Integer
        Dim xx, yy, s1 As Double
        If i > 0 Then
            For t = 0 To n
                xx = x + dx(t) * s
                yy = y + dy(t) * s
                t1 = i - 1
                s1 = s * r
                snow(xx, yy, n, t1, s1, r)
            Next
        End If
        Sixth(x, y, n, s)
    End Sub

    Private Sub setdate(ByVal n)
        Dim i As Integer
        Dim theta As Double
        dx(i) = 0.0#
        dy(i) = 0.0#
        theta = 2.0# * pi / n
        For i = 1 To n
            dx(i) = Math.Sin(i * theta)
            dy(i) = Math.Cos(i * theta)
        Next
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        PictureBox1.Refresh()
        g = PictureBox1.CreateGraphics
        Dim n, i As Integer
        Dim r, r1, c As Single
        Dim p As Double = 0.8
        Dim f As Double = 0.2
        c = 40.0#
        n = 6
        r1 = 0.5 * 1000 * (1 - f) / (1 - p)
        r = r1 / c
        g.DrawEllipse(pen1, 200 - r, 150 - r, 2 * r, 2 * r)
        circle(200, 150, r, n + 1)
    End Sub
    Private Sub circle(ByVal x, ByVal y, ByVal r, ByVal n)
        Dim tcos(100), tsin(100) As Single
        Dim i, ns As Integer
        Dim theta As Double
        Dim f As Single = 0.3!
        Dim c As Double = 2
        ns = 10
        theta = 2 * pi / ns
        Dim n1, f1 As Single
        n1 = n - 1
        f1 = f * r
        If n1 > 1 Then
            For i = 1 To ns
                tcos(i) = c * Math.Cos(i * theta)
                tsin(i) = c * Math.Sin(i * theta)
                g.DrawEllipse(pen1, x + r * tcos(i) - f1, y + r * tsin(i) - f1, 2 * f1, 2 * f1)
                circle(x + r * tcos(i), y + r * tsin(i), f1, n1)
            Next
        End If
    End Sub
End Class








17.9.Hexagon
17.9.1.Draw HexagonDraw Hexagon