How can i find the index that is closest to the current mouse location ? Variable idx return 0 all

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
In Form1 i have this pictureBox1 mouse down event :private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
label1.Visible = true;
label4.Visible = true;
if (wireObject1 == null)
{
}
else
{
float t = wireObject1.GetIndexByXY(e.X, e.Y, 5);

What i need to do with the variable t is: to find the index that is closest to the current mouse location.
Now i have a point i drawed on the pictureBox and when i click on the point i need to find the index that is closest to the current mouse location .
This is the GetIndexByXY in the Object.cs class i did :public float GetIndexByXY( int x , int y , float tol)
{
for (idx = 0; idx < woc.Point_X.Count; idx++)//++idx)
{
float dx = woc.Point_X[idx] - x;
float dy = woc.Point_Y[idx] - y;
float dist = (float)Math.Sqrt(dx * dx + dy * dy);

if (dist < tol) return idx;
}
return -1;
}

idx is all the time 0 . I know the reason is that dist < tol but why ?
For example i clicked now on a point so : x = 435 Y = 246 and tol = 5.0
The List : woc.Point_X.Count contain 2 indexs now since i have two points in the pictureBox but i clicked only on one of them : indexp0] = 433.0 and index[1] = 430.0
I used a breakpoint on the for :
In the end idx = 0 and in Form1 the variable t is 0.0
So what am i doing wrong here ? Im getting the correct location of the mouse from Form1 e.X and e.Y whats wrong with the functioin : GetIndexByXY ?

View the full article
 
Back
Top