Tips kali ini kita akan membuat visual Grafik 3D dengan menggunakan Visual Basic 2008. Tampilannya perhatikan Gambar di bawah ini:
Ikuti langkah-langkah di bawah ini, rancang interface/antarmuka pada Form menggunakan ToolBox seperti gambar di bawah ini:
Source listing program dari perancangan/design di atas, ketikkan dengan mengklik kanan pada design dan pilih View Code.
Source Program:
Public Class Form1
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
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
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 = 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
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
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 Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim th As Single
Dim n1 As Short
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
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
End Class
Setelah Anda menuliskan listing program diatas, tekan F5. Tampilan pola grafiknya sebagai berikut akan tampil dengan menekan tombol 3D Curve, Ball dan Shape 1:
0 komentar:
Posting Komentar