[RESOLVED] 2D loop to plot surface of a 3D sphere?
Yes, I realize that a sphere is nothing BUT 3D... but I figured I'd toss it in there just to clarify my intents. I have two loops going, both from 0 to 359. The outer loop is defining the angle of the next circle along the X-Z plane, and the inner loop draws the circle along the Y-angle of outer loop plane. I can get the inner loop to draw just fine.. I know the Y variable is the independent variable in this situation. I know the X and Z variables need to play off of each other to figure out where the point actually is, but I'm not sure how... yes it's a program, but it's the math that hurts. Here's my loop right now:
Code:
For i As Integer = 0 To 359 Step 5
For j As Integer = 0 To 359 Step 5
Points.Add(New Pt3D(size * Sin(j / rad), size * Cos(j / rad), size * Cos(i / rad), Pens.White))
Next
Next
in this instance, rad is defined as 180/PI... a conversion factor so my angles get changed to radians before the trig functions hit them... bloody program only knows radians...
Re: 2D loop to plot surface of a 3D sphere?
Nevermind.. I figured it out.. I was thinking about it the wrong way. This method basically looks at the height as independent, but part of a triangle where the radius is the hypotenuse... a simple Pythagorean equation gives me the distance from the Y-axis that all points at the current height are, and I just ran around the axis with those.. looks cool, too. Here's my solution:
Code:
For i As Integer = 0 To 359 Step 5
For j As Integer = 0 To 359 Step 5
Dim tmp As Double = size * Cos(i / rad)
Dim dist As Double = ((size ^ 2) - (tmp ^ 2)) ^ 0.5
Points.Add(New Pt3D(dist * Cos(j / rad), tmp, dist * Sin(j / rad), Pens.White))
Next
Next
Re: 2D loop to plot surface of a 3D sphere?
Your outer loop needs to run from -90 (south pole) to 90 (north pole). The sine of this angle is your y position. The cosine of this angle is the radius of the circle in the X-Z plane.
In the inner loop, your x and z postions are the cosine and sine, respectively, multiplied by the radius calculated in the outer loop.
Code:
For i = -90 To 90 Step 5
ii = i*pi/180
py = Sin(ii)
r = Cos(ii)
For j = 0 To 359 Step 5
jj = j*pi/180
px = r * Cos(jj)
pz = r * Sin(jj)
' Assuming size is radius of sphere
Points.Add(New Pt3D(size * px, size * py, size * pz, Pens.White))
Next j
Next i