Measuring Distance

ARC

Well-known member
Joined
Sep 9, 2006
Messages
63
Location
Minnesota
Programming Experience
Beginner
I have 25 images lined up on a form in a 5x5 pattern. I can determine which images the user has or has not clicked yet, but I was wondering if there was a way to measure distance between two points on the form? I want the user to be able to click on imageX, and then get the distance between imageX and imageY -- where imageY is a randomly selected image on the Form Load.
 
Last edited:
Use Pythagoras:
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] pt1 [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Point(1, 200)[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] pt2 [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Point(100, 2)[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] xdist [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = Math.Abs(pt1.X - pt2.X)[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ydist [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = Math.Abs(pt1.Y - pt2.Y)[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] distance [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Double[/COLOR][/SIZE][SIZE=2] = Math.Sqrt(xdist ^ 2 + ydist ^ 2)[/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Text = distance.ToString[/SIZE]
 
vector distance is the square root of the x difference squared multiplied by the y differance squared i.e.

P1(x,y)
P2(x,y)

a=P1.x-P2.x ' dont worry about using ABS as the square we need is positive
b=P1.y-P2.y

|V|=sqrt(a*a+b*b)

note the angle of travel might be needed this is achieved using the Atn2 function

angle=Atn2(b,a)

hope this helps essentially it is rectangular to polar coordinates you are dealing with here

EDIT: same as John H i tried to get a theta char and ended up losing my type :) as i mention tho the ABS is not needed as any number squared is positive leaving them signed makes angle calculation easier tho ;)
 
And this is the method I would use then to display the distance between which image they need to click on, and which they actually clicked?

I would then take the distance that gives me, and devide it by the width of the imagebox, as they are all the same size, and square. Thus giving them the number of imagebox's they are away from the goal imagebox.


correct?

edit: this would aslo mean I would need to place the randomly generated point at the center of the image box, and round down to the nearest whole imagebox; yeah?
 
that would seem right it would be the distance in 'image widths' after the scaling and the distance in pixels before scaling

yes the random point would have to be central as the user will no doubt click near the middle.

you could use each picturebox's click event, and fill a 5 by 5 array, you would still need the maths though.
 
edit: this would aslo mean I would need to place the randomly generated point at the center of the image box, and round down to the nearest whole imagebox; yeah?
You can just use the pictureboxes Location point. For the initally selected random point you can get this picturebox by using Control.GetChildAtPoint method (if the current form is the container write Me.GetCh..) and get the location. For each Picturebox.Click you get its location and run the calculation.

Edit: You are also missing something, the angle does matter because width=height doesn't equal the squares length across, this could get your calculation off. If you got 25*25 boxes the size 3*3 each and measure all across you get sqr(75^2+75^2)=106 and divide by 3 you get 35 which is wrong when it actually is 25 (106/4,24). Well I think the distance between the end corners is actually 24, so you also would subtract 1 to that. When using the pictureboxes location point you can simply detect if there is an angle by checking if the box point and compare box point got the same Y value. So you would need to also calculate the angle, then find the distance across an picturebox for that angle then measure the full distance.. if I didn't miss a thought somewhere here :)
 
Back
Top