|
![](/i/fill.gif) |
Chris Johnson nous apporta ses lumieres ainsi en ce 2004/05/03 15:44... :
>I haven't seen an algorithm for it specifically, though an example was on
>p.binaries.animations a while ago.
>
>Heres some C code - I've typed this straight into the post so there could
>well be typos, but I think the overall idea is right:
>
>typedef struct
>{
> float x,y,z;
>} point;
>
>int maxPoints=60000;
>point *points;
>====> points=(point *)malloc(maxPoints*sizeof(point));
>float repulseCoeff = 0.1;
>int maxIterations = 50;
>float dd;
>
>// Initially distribute points randomly on sphere
>for (int n=0; n<maxPoints; n++)
>{
> do
> {
> points[n].x = frnd(2)-1; // frnd(float max) generates random
>number between 0 and max
> points[n].y = frnd(2)-1;
> points[n].z = frnd(2)-1;
> }
> while (sq(points[n].x)+sq(points[n].y)+sq(points[n].z)>1)
>}
>
>for (int i=0; i<maxIterations; i++) // Iterate this bit a few times
>{
> for (int n=0; n<maxPoints; n++)
> {
> for (int m=n+1; m<maxPoints; m++) // for each pair of points...
> {
> // make them repulse each other, without a restriction that they
>stay on the sphere
>
>dd=sq(points[n].x-points[m].x)+sq(points[n].y-points[m].y)+sq(points[n].z-po
>ints[m].z);
> points[n].x += (points[n].x-points[m].x)*repulseCoeff/dd;
> points[n].y += (points[n].y-points[m].y)*repulseCoeff/dd;
> points[n].z += (points[n].z-points[m].z)*repulseCoeff/dd;
> points[m].x -= (points[n].x-points[m].x)*repulseCoeff/dd;
> points[m].y -= (points[n].y-points[m].y)*repulseCoeff/dd;
> points[m].z -= (points[n].z-points[m].z)*repulseCoeff/dd;
> }
> }
>
> for (int n=0; n<maxPoints; n++) // for each point
> {
> // stick it back onto the sphere
> dd = sqrt(sq(points[n].x)+sq(points[n].y)+sq(points[n].z));
> points[n].x/=dd;
> points[n].y/=dd;
> points[n].z/=dd;
> }
>}
>
>-Chris
>
>
>
>
There is a bug, using "malloc" and never freeing the allocated memory =
memory leak.
Post a reply to this message
|
![](/i/fill.gif) |