3D Ball : 3D « 2D Graphics « VB.Net Tutorial






3D Ball
'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 Draw3DBall
   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
    Friend WithEvents Button3 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.Button3 = 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(528, 312)
        Me.PictureBox1.TabIndex = 0
        Me.PictureBox1.TabStop = False
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(24, 336)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(64, 32)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Shape 1"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(120, 336)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(64, 32)
        Me.Button2.TabIndex = 2
        Me.Button2.Text = "3D Curve"
        '
        'Button3
        '
        Me.Button3.Location = New System.Drawing.Point(232, 336)
        Me.Button3.Name = "Button3"
        Me.Button3.Size = New System.Drawing.Size(72, 32)
        Me.Button3.TabIndex = 3
        Me.Button3.Text = "Ball"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(528, 382)
        Me.Controls.Add(Me.Button3)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.PictureBox1)
        Me.ResumeLayout(False)

    End Sub

    Dim g As System.Drawing.Graphics
    Dim pen1 As New System.Drawing.Pen(Color.Green, 0.5)
    Const PI = 3.14159
    Dim flag, col, r, h, hl, n, n1 As Short
    Dim x, y, z, cx, cy, cz, thx, thy, thz As Single
    Dim ed, od, eh As Short
    Dim point1, point2, point3, point4, point5 As System.Drawing.Point
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim th As Single
        Static ax(9), ay(9), az(9)
        Static bx(9), by(9), bz(9)
        h = 160 : thy = 0.2
        col = 0 : n1 = 1
        r = 100
        n = 8
        thx = 0.2
        PictureBox1.Refresh()
        ax(0) = h : ay(0) = h : az(0) = h
        n1 = 1
        For th = 0 To 2 * PI + 0.1 Step 2 * PI / n
            x = r * Math.Cos(th) : y = -h : z = r * Math.Sin(th)
            Call roty() : Call rotx()
            bx(n1) = x : by(n1) = y : bz(n1) = z
            n1 = n1 + 1
        Next th
        For n1 = 1 To n
            x = ax(n1) : y = ay(n1)
            Call linzuiti()
            x = ax(n1 + 1) : y = ay(n1 + 1)
            Call linzuiti()
            x = bx(n1 + 1) : y = by(n1 + 1)
            Call linzuiti()
            x = bx(n1) : y = by(n1)
            Call linzuiti()
        Next n1
    End Sub
    Public Sub linzuiti()
        Dim gx, gy As Single
        gx = 250 + x
        gy = 180 + y
        If flag = 0 Then point1.X = gx : point1.Y = gy
        flag = 1
        point2.X = gx : point2.Y = gy
        g = PictureBox1.CreateGraphics
        g.DrawLine(pen1, point1, point2)
        point1 = point2
    End Sub
    Private Sub rotx()
        Dim yw, zw As Single
        yw = y : zw = z
        y = yw * Math.Cos(thx) - zw * Math.Sin(thx)
        z = yw * Math.Sin(thx) + zw * Math.Cos(thx)
    End Sub

    Private Sub roty()
        Dim zw, xw As Single
        zw = z : xw = x
        x = zw * Math.Cos(thy) - xw * Math.Sin(thy)
        z = zw * Math.Sin(thy) + xw * Math.Cos(thy)
    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 x, y, s, cx, cy, zz, xx, yy As Single
        Dim h, a, b, sx, sx1, sx2, sy, sy1, sy2 As Integer
        Dim x1, x2, y1, y2, xw, yw As Integer
        s = pi / 16
        x1 = -3
        x2 = 3
        xw = x2 - x1
        y1 = -9 : y2 = 9 : yw = y2 - y1
        sx1 = 140 : sx2 = 600
        sy1 = 360 : sy2 = 100
        cx = (sx2 - sx1) / xw
        cy = (sy1 - sy2) / yw
        h = 170 : a = 6 : b = 6
        For y = y1 + 6 To y2 - 6 Step 1 / cy
            For x = x1 To x2 Step 1 / cx * 2
                zz = h * Math.Exp(-x * x / a * a - y * y / b * b)
                xx = x * cx + sx1 + 160
                yy = sy1 - y * cy
                sx = Int(xx + y * cy * Math.Cos(s))
                sy = yy - zz
                sy = sy - 100
                If x = -3 Then
                    point1.X = sx - 60 : point1.Y = sy - 20
                Else : point2.X = sx - 60 : point2.Y = sy - 20
                    g.DrawLine(pen1, point1, point2)
                    point1 = point2
                End If
            Next x
        Next y
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        PictureBox1.Refresh()
        g = PictureBox1.CreateGraphics
        Dim i, r As Integer
        Dim x, y, z, s, q, p, px, py As Single
        Dim mx(600), my(600) As Integer
        s = pi / 4
        r = 100
        i = 1
        q = -pi / 2 + 0.1
        For p = 0 To 2 * pi Step 0.2
            x = r * Math.Cos(q) * Math.Sin(p)
            y = r * Math.Sin(q)
            z = r * Math.Cos(q) * Math.Cos(p)
            px = x * 1.2
            py = y - z * Math.Sin(s)
            mx(i) = px + 200
            my(i) = 150 - py
            i = i + 1
        Next p
        point1.X = mx(1) : point1.Y = my(1)
        For q = -pi / 2 + 0.2 To pi / 2 Step 0.2
            i = 1
            For p = 0 To 2 * pi Step 0.2
                x = r * Math.Cos(q) * Math.Sin(p)
                y = r * Math.Sin(q)
                z = r * Math.Cos(q) * Math.Cos(p)
                px = x * 1.2
                py = y - z * Math.Sin(s)

                point2.X = px + 200 : point2.Y = 150 - py
                g.DrawLine(pen1, point1, point2)

                point1 = point2

                point3.X = mx(i) : point3.Y = my(i)
                point4.X = px + 200 : point4.Y = 150 - py
                g.DrawLine(pen1, point3, point4)
                point3 = point4
                mx(i) = px + 200
                my(i) = 150 - py
                i = i + 1
            Next p
            p = 0
            x = r * Math.Cos(q) * Math.Sin(p)
            y = r * Math.Sin(q)
            z = r * Math.Cos(q) * Math.Cos(p)
            px = x * 1.2
            py = y - z * Math.Sin(s)

            point5.X = px + 200 : point5.Y = 150 - py
            g.DrawLine(pen1, point4, point5)
            point4 = point5
        Next q
    End Sub
End Class








17.89.3D
17.89.1.3D shape3D shape
17.89.2.3D Curve
17.89.3.3D Ball3D Ball