Function which creates a simulation object simply get a point (BALLRADIUS,0) and rotate it with a specified angle ( =
part of
). It gives us a set of points which have to be connected by linear springs. It is simple procedure:
void CreateBall(void)
{
int i;
// create points
for(i=1 ; i <= NUMP ; ++i)
{
myPoints[i].x = BALLRADIUS *
sin(i * (2.0 * 3.14) / NUMP );
myPoints[i].y = BALLRADIUS *
cos(i * (2.0 * 3.14) / NUMP ) +
SCRSIZE/2;
}
// create springs
for(i=1 ; i <= NUMP ; ++i)
AddSpring(i,i,i+1);
AddSpring(i-1,i-1,1);
}
An
function use mySprings[] table (mentioned before) to store springs. It simply get index in a spring table (
) and indexes of first and second spring point (those indexes are just pointers in myPoints[] table). Function calculates length of the spring and put all informations in actual (
) place in mySprings[] table:
void AddSpring(int pi, int i, int j)
{
mySprings[pi].i = i;
mySprings[pi].j = j;
mySprings[pi].length =
sqrt(
(myPoints[ i ].x - myPoints[ j ].x) *
(myPoints[ i ].x - myPoints[ j ].x) +
(myPoints[ i ].y - myPoints[ j ].y) *
(myPoints[ i ].y - myPoints[ j ].y)
);
}
Now an object is created and we are able to start main part of algorithm.