code question / connect points

Posted By: ello

code question / connect points - 03/14/10 19:25

Hi, I have a small issue trying to connect all points of a mesh with lines.

i have a code like:
Code:
for (i=0;i<pointnumber;i++)
{
 for (j=0;j<pointnumber;j++)
 {
  if (i!=j) line(point[i],point[j]);
 }
}



mainly my question is how can i prevent that when there already was for example a line from point[2] to point[5] another line points from point[5] to point[2]

thanks and cheers,
ello
Posted By: MichaelGale

Re: code question / connect points - 03/14/10 19:33

The simplest way to do that is probably to create a list of all lines you need to draw and then iterate through them (or mark them as drawn using a boolean once you have drawn them but that isn't really necessary here). This shouldn't be much of an issue if your points don't or rarely change as you can maintain the list structure in memory.

Also, if you are simply drawing straight lines then there isn't much of point in trying to avoid drawing the same line twice because it will most likely be slower to prevent that from happening than it would be to draw them twice.
Posted By: ello

Re: code question / connect points - 03/14/10 20:06

those lines are planned to be 3d line objects so i think it matters to avoid doubles.. i guess i'll give the idea with that boolean a try ,thanks
Posted By: Error014

Re: code question / connect points - 03/14/10 22:59

why not start the second for-loop with j=i (or better yet i+1)?

... I mean, the first cycle connects point 1 with everything else. The second point thus only needs to be connected to all points after the first. (since 1->2 already exists). So in other words, since for the n-th point, the connections to all points lower than n already exist, you only need to connect it with all the others.

but then, its late. so i'm probably wrong.
Posted By: Hummel

Re: code question / connect points - 03/14/10 23:08

you should use Error014īs solution since itīs faster then indexing all points and itīs probably the fastest way anyway wink
Posted By: MichaelGale

Re: code question / connect points - 03/14/10 23:15

Hmm, I think Error014 is right. That should work.

Code:
for (i=0;i<pointnumber-1;i++) // we don't need the last one
{
 for (j=i+1;j<pointnumber;j++) 
 {
  if (i!=j) line(point[i],point[j]);
 }
}



I think the best way to imagine this is by thinking of a circle of points.
Posted By: ello

Re: code question / connect points - 03/15/10 06:08

thank you all.. that helped a lot grin
© 2024 lite-C Forums