Graph of 3D Sphere

Question

• Trying to graph a 3D Sphere with radius =100.  I can't get any graphics.  Could anyone go over this.  Thanx.

Dim gphFormGraphics As Graphics = Me.CreateGraphics
Dim pt1, pt2 As Point
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
gphFormGraphics =
Me.CreateGraphics()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim recDrawingRectangle As Rectangle = New Rectangle(0, 0, 700, 700)
Dim i, j As Integer
Dim r0, x0, y0, x1, y1, x, y, z, x10, y10, z10 As Double
r0 = 100
x10 = r0 * Math.Cos(0) * Math.Sin(0)
y10 = r0 * Math.Sin(0) * Math.Sin(0)
z10 = r0 * Math.Cos(0)
x0 = 350 + x10 - y10 * Math.Cos(Math.PI / 4)
y0 = 350 - z10 + y10 * Math.Sin(Math.PI / 4)
For i = 0 To (2 * Math.PI) Step 0.2
For j = 0 To (2 * Math.PI) Step 0.2
x = r0 * Math.Cos(i) * Math.Sin(j)
y = r0 * Math.Sin(i) * Math.Sin(j)
z = r0 * Math.Cos(j)
x1 = 350 + x - y * Math.Cos(Math.PI / 4)
y1 = 350 - z + y * Math.Sin(Math.PI / 4)
pt1 =
New Point(x0, y0)
pt2 =
New Point(x1, y1)
gphFormGraphics.DrawLine(Pens.Red, pt2, pt1)
x0 = x1
y0 = y1
Next j
Next i
End Sub

Wednesday, November 05, 2008 5:44 AM

• Hi,

Here is your code changed slightly for OPTION STRICT ON
( it will also now work with OPTION STRICT OFF ) and inserted into the FORM PAINT event where it works. :-)

I have never seen a plot quite like that before, thanks for your code.

Regards,

John

________________________________________________

Option Strict On

Public
Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Me
.WindowState = FormWindowState.Maximized
Me.BackColor = Color.Black
End Sub

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

Dim r0, x0, y0, x1, y1, x, y, z, x10, y10, z10 As Double
r0 = 150
x10 = r0 * Math.Cos(0) * Math.Sin(0)
y10 = r0 * Math.Sin(0) * Math.Sin(0)
z10 = r0 * Math.Cos(0)
x0 = 350 + x10 - y10 * Math.Cos(Math.PI / 4)
y0 = 350 - z10 + y10 * Math.Sin(Math.PI / 4)

Dim
pt1, pt2 As Point

For i As Double = 0 To (2 * Math.PI) Step 0.2
For j As Double = 0 To (2 * Math.PI) Step 0.2

x = r0 * Math.Cos(i) * Math.Sin(j)
y = r0 * Math.Sin(i) * Math.Sin(j)
z = r0 * Math.Cos(j)
x1 = 350 + x - y * Math.Cos(Math.PI / 4)
y1 = 350 - z + y * Math.Sin(Math.PI / 4)
pt1 =
New Point(CInt(x0), CInt(y0))
pt2 =
New Point(CInt(x1), CInt(y1))
e.Graphics.DrawLine(Pens.Red, pt2, pt1)
x0 = x1
y0 = y1

Next
j
Next i
End Sub

End
Class

• Edited by Wednesday, November 05, 2008 7:06 AM Rectangle line of code not needed so it was removed.
• Marked as answer by Wednesday, November 05, 2008 10:54 AM
Wednesday, November 05, 2008 6:53 AM

All replies

• Hi,

Here is your code changed slightly for OPTION STRICT ON
( it will also now work with OPTION STRICT OFF ) and inserted into the FORM PAINT event where it works. :-)

I have never seen a plot quite like that before, thanks for your code.

Regards,

John

________________________________________________

Option Strict On

Public
Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Me
.WindowState = FormWindowState.Maximized
Me.BackColor = Color.Black
End Sub

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

Dim r0, x0, y0, x1, y1, x, y, z, x10, y10, z10 As Double
r0 = 150
x10 = r0 * Math.Cos(0) * Math.Sin(0)
y10 = r0 * Math.Sin(0) * Math.Sin(0)
z10 = r0 * Math.Cos(0)
x0 = 350 + x10 - y10 * Math.Cos(Math.PI / 4)
y0 = 350 - z10 + y10 * Math.Sin(Math.PI / 4)

Dim
pt1, pt2 As Point

For i As Double = 0 To (2 * Math.PI) Step 0.2
For j As Double = 0 To (2 * Math.PI) Step 0.2

x = r0 * Math.Cos(i) * Math.Sin(j)
y = r0 * Math.Sin(i) * Math.Sin(j)
z = r0 * Math.Cos(j)
x1 = 350 + x - y * Math.Cos(Math.PI / 4)
y1 = 350 - z + y * Math.Sin(Math.PI / 4)
pt1 =
New Point(CInt(x0), CInt(y0))
pt2 =
New Point(CInt(x1), CInt(y1))
e.Graphics.DrawLine(Pens.Red, pt2, pt1)
x0 = x1
y0 = y1

Next
j
Next i
End Sub

End
Class

• Edited by Wednesday, November 05, 2008 7:06 AM Rectangle line of code not needed so it was removed.
• Marked as answer by Wednesday, November 05, 2008 10:54 AM
Wednesday, November 05, 2008 6:53 AM
• Hi again,

I like it with the following changes  :-) .>>

For i As Double = 0 To (2 * Math.PI) Step (2 * Math.PI) / 50

For
j As Double = 0 To (2 * Math.PI) Step (2 * Math.PI) / 50

Regards,

John