# Select Points on Map in PicBox Randomly and Calculate Dist Per Route

Page 1 of 1

## 8 Replies - 498 Views - Last Post: 30 April 2011 - 05:02 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=229978&amp;s=8f5563fa415482051b09e1252a3df916&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 nanosystems

• New D.I.C Head

Reputation: 0
• Posts: 16
• Joined: 29-October 10

# Select Points on Map in PicBox Randomly and Calculate Dist Per Route

Posted 30 April 2011 - 04:17 AM

Thank you, Guys,

The code below is returning a zero value for the calculation of distance of selected route(array). I have been trying to debug it but to no avail. Additionally, I will like points or cities modeled as array, to be randomly generated from mouse clicks on a map in picBox. Can anyone please help or give some hints?

```namespace TSA
{
class Route
{
ArrayList Cities = new ArrayList();
double travelCost;
int dist;

public  double RunGA(int numCities, int[,] distancematrix)
{
for (int i = 0; i < distancematrix.Length; i++)
{
for (int j = 0; j < distancematrix.Length; j++)
{
}
}
// distancematrix = new int[numCities,numCities];
int dX = 0;
int dY = 0;

for (int i = 0; i < numCities; i++)
{
for (int j = i; j < numCities; j++)
{
if (i == j)
distancematrix[i, j] = 0;
else
{
dX = Math.Abs(((Point)Cities[i]).X - ((Point)Cities[j]).X);
dY = Math.Abs(((Point)Cities[i]).Y - ((Point)Cities[j]).Y);
dist = (int)(Math.Sqrt(dX * dX + dY * dY));
distancematrix[i, j] = dist;
distancematrix[j, i] = dist;
}

}

}
travelCost = (double)dist;
return travelCost;
}

}

}
namespace TSA
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnRunGA_Click(object sender, EventArgs e)
{

int numcities = 6;
Route r = new Route();
double cost=r.RunGA(numcities, new int[,] {
{0, 20, 25, 15, 45, 32 },
{20, 0, 35, 17, 28, 39 },
{25, 35, 0, 45, 35, 22 },
{15, 17, 45, 0, 15, 42 },
{45, 28, 35, 15, 0, 52 },
{32, 39, 22, 42, 52, 0 }
});
MessageBox.Show("Cost for this Rout is:  " +"" + cost.ToString());

}
}
}

```

Thank you.

nanosystems

MOD EDIT: Fixed code tags. The RIGHT way to use code tags:

This post has been edited by JackOfAllTrades: 30 April 2011 - 05:25 AM

Is This A Good Question/Topic? 0

## Replies To: Select Points on Map in PicBox Randomly and Calculate Dist Per Route

### #2 tlhIn`toq

• Not here as much anymore

Reputation: 5969
• Posts: 13,048
• Joined: 02-June 10

## Re: Select Points on Map in PicBox Randomly and Calculate Dist Per Route

Posted 30 April 2011 - 04:24 AM

### #3 tlhIn`toq

• Not here as much anymore

Reputation: 5969
• Posts: 13,048
• Joined: 02-June 10

## Re: Select Points on Map in PicBox Randomly and Calculate Dist Per Route

Posted 30 April 2011 - 04:30 AM

Right off the bat this seems screwy.

```ArrayList Cities = new ArrayList();

public double RunGA(int numCities, int[,] distancematrix)
{
for (int i = 0; i < distancematrix.Length; i++)
{
for (int j = 0; j < distancematrix.Length; j++)
{
}
}
```

You add the entire matrix to an arraylist, every time you loop through both dimensions of the matrix?

So of the matrix is 10x10 you will add the entire matrix to the ArrayList 100 times?

I'm guessing you are not familiar with breakpoints or looking at your variables' values in the Locals and Autos pallet.

You need to become comfortable with placing a breakpoint at the start of a method and walking through the entire method with the F10 key, looking at the values. This is the start of how to debug your application.

Debugging tutorial
Debugging tips
Great debugging tips

### #4 nanosystems

• New D.I.C Head

Reputation: 0
• Posts: 16
• Joined: 29-October 10

## Re: Select Points on Map in PicBox Randomly and Calculate Dist Per Route

Posted 30 April 2011 - 04:31 AM

tlhIn`toq, on 30 April 2011 - 04:24 AM, said:

Thank you, Guys,

The code below is returning a zero value for the calculation of distance of selected route(array). I have been trying to debug it but to no avail. Additionally, I will like points or cities modeled as array, to be randomly generated from mouse clicks on a map in picBox. Can anyone please help or give some hints?

```namespace TSA
{
class Route
{
ArrayList Cities = new ArrayList();
double travelCost;
int dist;

public double RunGA(int numCities, int[,] distancematrix)
{
for (int i = 0; i < distancematrix.Length; i++)
{
for (int j = 0; j < distancematrix.Length; j++)
{
}
}
// distancematrix = new int[numCities,numCities];
int dX = 0;
int dY = 0;

for (int i = 0; i < numCities; i++)
{
for (int j = i; j < numCities; j++)
{
if (i == j)
distancematrix[i, j] = 0;
else
{
dX = Math.Abs(((Point)Cities[i]).X - ((Point)Cities[j]).X);
dY = Math.Abs(((Point)Cities[i]).Y - ((Point)Cities[j]).Y);
dist = (int)(Math.Sqrt(dX * dX + dY * dY));
distancematrix[i, j] = dist;
distancematrix[j, i] = dist;
}

}

}
travelCost = (double)dist;
return travelCost;
}

}

}
namespace TSA
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnRunGA_Click(object sender, EventArgs e)
{

int numcities = 6;
Route r = new Route();
double cost=r.RunGA(numcities, new int[,] {
{0, 20, 25, 15, 45, 32 },
{20, 0, 35, 17, 28, 39 },
{25, 35, 0, 45, 35, 22 },
{15, 17, 45, 0, 15, 42 },
{45, 28, 35, 15, 0, 52 },
{32, 39, 22, 42, 52, 0 }
});
MessageBox.Show("Cost for this Rout is: " +"" + cost.ToString());

}
}
}

```

Thank you.

nanosystems

### #5 tlhIn`toq

• Not here as much anymore

Reputation: 5969
• Posts: 13,048
• Joined: 02-June 10

## Re: Select Points on Map in PicBox Randomly and Calculate Dist Per Route

Posted 30 April 2011 - 04:41 AM

I don't mind helping someone with logic problems, but your code doesn't even complete. You need to at least do your own basic debugging. We're not here to clean up everything for you.

### #6 tlhIn`toq

• Not here as much anymore

Reputation: 5969
• Posts: 13,048
• Joined: 02-June 10

## Re: Select Points on Map in PicBox Randomly and Calculate Dist Per Route

Posted 30 April 2011 - 04:48 AM

I'm really pretty sure you didn't mean to have nearly 1300 cities. Did you?

Work on cleaning up everything around your calculation before worrying about the calculation itself. This code has many issues that need attention.

### #7 tlhIn`toq

• Not here as much anymore

Reputation: 5969
• Posts: 13,048
• Joined: 02-June 10

## Re: Select Points on Map in PicBox Randomly and Calculate Dist Per Route

Posted 30 April 2011 - 04:54 AM

And the distance between your cities will always be zero if all of your cities are at the same location. All 1296 cities are at location 6,6

This is all basic debugging where you look at the values of your objects and see if they actually are what you expected them to be.

I think those tutorials I linked to you will help you a lot if you really work them and not just quick skim them.

### #8 nanosystems

• New D.I.C Head

Reputation: 0
• Posts: 16
• Joined: 29-October 10

## Re: Select Points on Map in PicBox Randomly and Calculate Dist Per Route

Posted 30 April 2011 - 04:57 AM

Thank you, I have been able to correct the appropriate number of cities(36), but how do I get the right cast for the Point Class, and how do I randomly select the cities from a map on picBox? Any hint or help, please?

### #9 tlhIn`toq

• Not here as much anymore

Reputation: 5969
• Posts: 13,048
• Joined: 02-June 10

## Re: Select Points on Map in PicBox Randomly and Calculate Dist Per Route

Posted 30 April 2011 - 05:02 AM

Let's also see the values for each city. I don't want to waste my time or yours if they are all still 6,6.

You are so far away from having this working that you really don't need to worry about random selection yet.

Don't try to cast to a point. Make a new point using the values you have

```point tempPoint = new Point(x, y);
```

then work with that point.

Don't worry about condensing everything to the least number of lines right now.

First make it work. Use lots of lines that have one function each so you can confirm each works.