Browsing this Thread:
1 Anonymous Users
Need help with some code. 


Home away from home
Joined:
2004/4/12 5:20 From Perth,Australia
Group:
Registered Users Posts:
527

I adapted this routine from some code on a newsgroup..It is supposed to generate points on the surface of a sphere. At the moment it only generates 'Access Violation' errors
I can get it to run by setting the variable a=1,this generates the point coords(very fast too) but not correctly,They are all crowded on one side in lines. I'm no good at maths so can anyone see where the problem is. thankls for any help. heres the code // some setup stuff type vectorfloat = record x, y, z: double; end; var points : aray of vectorfloat; setlength(points,100); // generate the 3d points procedure GenerateDotBall2(var Points: array of Vectorfloat); var n, i, j, m, count: integer; beta, a, r, z, x, y, alpha: double; begin n := length(points); if n < 1 then exit; // subdivision angle beta := 0.5 * pi / n; // line segment length a := 2 * sin(beta / 2); //a := 1; // endcap count := 0; Points[count].x := 0; Points[count].y := 0; Points[count].z := 1; inc(count); Points[count].x := 0; Points[count].y := 0; Points[count].z := 1; inc(count); // rings for i := 1 to n do begin r := sin(i * beta); z := cos(i * beta); m := round(R * 2 * pi / a); for j := 0 to m  1 do begin alpha := j / m * 2 * pi; x := cos(alpha) * r; y := sin(alpha) * r; Points[count].x := x; Points[count].y := y; Points[count].z := z; if i <> N then begin Points[count].x := x; Points[count].y := y; Points[count].z := z; end; inc(count); end; end; end; Hers the original algorithm from comp.graphics.algorithm N := StrToIntDef(InputBox('Number of parts (N >= 1)', 'N = ', '1'), 0); if N < 1 then exit; // subdivision angle Beta := 0.5 * pi / N; // line segment length A := 2 * sin(beta/2); // endcap AddPoint(0, 0, 1); AddPoint(0, 0, 1); // rings for i := 1 to N do begin R := sin(i * beta); Z := cos(i * beta); M := round(R * 2 * pi / A); for j := 0 to M  1 do begin Alpha := j/M * 2 * pi; X := cos(Alpha) * R; Y := sin(Alpha) * R; AddPoint(X, Y, Z); if i <> N then AddPoint(X, Y, Z); end; end;
Posted on: 2005/10/8 6:53


_________________
Imagine 2.19 ...... 

Transfer 
Re: Need help with some code. 


Home away from home
Joined:
2004/4/12 5:20 From Perth,Australia
Group:
Registered Users Posts:
527

just realised the n variable is not a point count..set n=1 and it runs for 6 points then a 'floating point error' exception ???
n=2 for 16 points then the same error. getting a headache now
Posted on: 2005/10/8 7:35


_________________
Imagine 2.19 ...... 

Transfer 
Re: Need help with some code. 


Webmaster
Joined:
2004/4/3 15:57 From Stockholm, Sweden.
Group:
Webmasters Registered Users Posts:
2453

Running through your code I have the following comments...
What does N has to to with the points? It seems more to me to mean how many subdivisions you have of the sphere, not the amount of points on it.
Above they calculated the angle between the subdivisions (rings) of the sphere, but this means for one hemisphere only. Below they calculate the length of each segment, a or the distance betweens the points (vertices) if you want.
Below you add the polar cap points...
Here you start by going through each subdivision (ring) to add the points around it...
First they get the radius for the ring of the angle and then the height on the hemisphere.
Then the code is supposed to calculate how many points of the length you need around the ring, but to me it looks like the variable/constant R is undefined, but maybe this language r and R is the same, otherwise this might be your problem., in any way, its ment to give a number of points around the circle based on the calculated line segment length a above.
The program is then supposed to step through these points and adding them to the vector...
This adds the north hemisphere point...
Again we use an undefined variable N (you probably mean n) but I dont know if they are the same in this language, same as R above might be the same as r... In anycase this adds the south hemispshere half of the same coordinate... this means you probably should make an inc on each of those too, if i shoudl be equalt to N i.e. the equator, then we dont need to add it two times.
Not sure if this helps, but the code below seems to be right , just not sure if your code is doing what the pseudocode below is doing
Well, as I havent coded in pascal for quite some time I dont remeber if N=n in variable terms But I hope I might have pointet something out anyway. For the errors I would check the array indexing and check all divisions in a debugger or with simple printouts of the values while running with known values of N Thats probably the easiest way to find the errors.
Posted on: 2005/10/8 10:24


_________________
Johan Andersson the Impulsive Imagineer IFW 2.19.................. 

Transfer 
Re: Need help with some code. 


Home away from home
Joined:
2004/4/12 5:20 From Perth,Australia
Group:
Registered Users Posts:
527

thanks! thats made the code a lot clearer to me. FYI n=N in pascal/delphi..case does not matter in variable names.
Posted on: 2005/10/8 10:44


_________________
Imagine 2.19 ...... 

Transfer 
Re: Need help with some code. 


Just can't stay away
Joined:
2004/8/16 9:53 From Germany
Group:
Registered Users Posts:
130

I found 2 errors in your code and one in the original:
with Points[count].x := x; Points[count].y := y; Points[count].z := z; if i <> N then begin Points[count].x := x; Points[count].y := y; Points[count].z := z; you write overwrite the first cordinates (north) with the cordinates of the south. This appears, because the ocode printet points directly to the display (so it looks like to me). The secound errror is, why should the last points of the south not written to the field but the north (same error in the original code). Another remark is: did you use one begin and end too much in the last section ? @ Johan N is not a undefined variable, it sets the number of segments in horizontal and vertical alignment.
Posted on: 2005/10/8 11:03


_________________
Imagine for Windows 2.0 You know you have been raytraced too long when... ... You wonder which raytracer God used. (David Kraics) Mike...... 

Transfer 
Re: Need help with some code. 


Webmaster
Joined:
2004/4/3 15:57 From Stockholm, Sweden.
Group:
Webmasters Registered Users Posts:
2453

Quote:
If N <> n then it would have been undefined just like R, but as liontamer wrote, pascal/delphi is caseindependant on variables and as such both were defined. Being at base a C programmer I thought this might be the case but was still asking The fault you point out is the same as I do, however, in the pseudocode they assume AddPoint automatically build or adds to the vector array, something liontamers code didnt He has exactly the right amount of begins and ends
Posted on: 2005/10/8 15:21


_________________
Johan Andersson the Impulsive Imagineer IFW 2.19.................. 

Transfer 
Re: Need help with some code. 


Home away from home
Joined:
2004/4/12 5:20 From Perth,Australia
Group:
Registered Users Posts:
527

thanks guys, its working better now..I didn't realise that the number of points was set within the routine and was calling it with a too small array,hence the av's..I get a complete sphere but there is still a floating point error generated at the end of the loops for some reason but i think i can fix it.
ok. works perfectly now..its fast but somewhat limited in how many points it generates. eg values for n.. n=1..6 points n=2..22 points n=3..48 points n=4..82 points n=5..128 points n=6..184 points n=7..248 points n=8..328 points n=9..412 points n=10..510 points. I cant see a pattern there..anybody?
Posted on: 2005/10/8 21:30
Edited by liontamer on 2005/10/9 2:19:16


_________________
Imagine 2.19 ...... 

Transfer 
Re: Need help with some code. 


Just can't stay away
Joined:
2004/8/16 9:53 From Germany
Group:
Registered Users Posts:
130

Yes and no. The var M differs a little bit from the round function. So the pattern is not linear.
n=1..6 points +6 n=2..22 points +16 n=3..48 points +26 n=4..82 points +34 n=5..128 points +46 n=6..184 points +56 n=7..248 points +64 n=8..328 points +80 n=9..412 points +84 n=10..510 points. +98 so number of points should be <=n*10. Mike
Posted on: 2005/10/9 8:36


_________________
Imagine for Windows 2.0 You know you have been raytraced too long when... ... You wonder which raytracer God used. (David Kraics) Mike...... 

Transfer 
Re: Need help with some code. 


Webmaster
Joined:
2004/4/3 15:57 From Stockholm, Sweden.
Group:
Webmasters Registered Users Posts:
2453

Thats because it rounds it up, since it tries to put all points on an equal distance from each other...
Just make the array big enough, runt through the calculation on number of points BEFORE you allocate the array and you will be fine.
Posted on: 2005/10/9 11:58


_________________
Johan Andersson the Impulsive Imagineer IFW 2.19.................. 

Transfer 
You can view topic.
You cannot start a new topic.
You cannot reply to posts.
You cannot edit your posts.
You cannot delete your posts.
You cannot add new polls.
You cannot vote in polls.
You cannot attach files to posts.
You cannot post without approval.