# a little programing problem

## Recommended Posts

so i am having school problems for programing

if you had a black dot in lets say the center of your screen and several red dots in random places

how would you do to find the closest red dot to the black one, and have a message box pop up and say it's coordinates ?

i don't know what to do D:

##### Share on other sites

Kinda depends on the size of the total area. An easy implementation would be to scan every coordinate for a red dot and store its coordinates in a an array. After the scanning phase, simply determine the distance to R and then take the closest one. The distance would be calculated using the absolute value of the differences in X and Y coordinates.

For example, if B(lack dot) is @ (5,5) and you find a R(ed dot) at (1,2), the distance would be Abs(1-5) + Abs(2-5) = 7.

This isn't very efficient though. The better thing to simply start scanning at all coordinates that have distance = 1, then distance = 2, and so forth. The first red node you find is the closest

Below the distances from B, you should be able to determine these automatically using an algorithm.

```4 3 2 3 4
3 2 1 2 3
2 1 B 1 2
3 2 1 2 3
4 3 2 3 4```
Edited by dani

##### Share on other sites

Kinda depends on the size of the total area. An easy implementation would be to scan every coordinate for a red dot and store its coordinates in a an array. After the scanning phase, simply determine the distance to R and then take the closest one. The distance would be calculated using the absolute value of the differences in X and Y coordinates.

For example, if B(lack dot) is @ (5,5) and you find a R(ed dot) at (1,2), the distance would be Abs(1-5) + Abs(2-5) = 7.

This isn't very efficient though. The better thing to simply start scanning at all coordinates that have distance = 1, then distance = 2, and so forth. The first red node you find is the closest

Below the distances from B, you should be able to determine these automatically using an algorithm.

```4 3 2 3 4
3 2 1 2 3
2 1 B 1 2
3 2 1 2 3
4 3 2 3 4```

Please ciliath, believe me, dani is no nearer than you are. (Shame on you dani!)

Look up Pythagoras's theorum both of you.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

##### Share on other sites

I wouldn't know how to apply Pythagoras' Theorem (not Pythagoras's Theorum) to this problem. Enlighten me

If you are referring only to calculating distance over a diagonal angle, then it's only a matter of definition. I just defined distance as Abs(x1-x2) + Abs(y1-y2). If the diagonal distance should be defined using Pythagoras' Theorem (the 'real' distance), then fine. Do so. I still don't see how that changes the actual problem?

I simply assumed it to be a grid whereas diagonal travel is not possible. Please do not assume I do not know how to calculate the longest side of a triangle given the other two

Edited by dani

##### Share on other sites

I wouldn't know how to apply Pythagoras' Theorem (not Pythagoras's) to this problem. Enlighten me

If you are referring only to calculating distance over a diagonal angle, then it's only a matter of definition. I just defined distance as Abs(x1-x2) + Abs(y1-y2). If the diagonal distance should be defined using Pythagoras' Theorem (the 'real' distance), then fine. Do so. I still don't see how that changes the actual problem?

I simply assumed it to be a grid whereas diagonal travel is not possible. Please do not assume I do not know how to calculate the longest side of a triangle given the other two

If by "closest" the OP means the distance travelled in increments along either the X or the Y axis then adding the X and Y increments would be fine, but by closest I would expect that the OP means the distance in a straight line. If so then the sum of the increments will often not give the correct answer. For example move 10 left and 10 up. Distance travelled is 20. Actual distance apart of the start and end points is 14.14. Next move 19 left then 1 up. Distance travelled is 20, Distance apart is 19.02.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

##### Share on other sites

Yes martin I know this. actual = Math.sqrt(dist_x**2 + dist_y**2). Nothing new there

Anyway, as you said 'shame on you dani' I assumed you had the solution readily available

Next move 19 left then 1 up. Distance travelled is 20, Distance apart is 19.02.

That would be 19.03

@ciliath

How is the distance from a red dot to a black dot defined, and what are the dimensions of the area?

Edited by dani

##### Share on other sites

How is the distance from a red dot to a black dot defined, and what are the dimensions of the area?

the distance as in a straight line ^^

the area size is a square of height 300 and width 500

C:

##### Share on other sites

the distance as in a straight line ^^

the area size is a square of height 300 and width 500

C:

Do you know the coordinates of the red dots and do you understand Pythagoras's theorem?

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

##### Share on other sites

As he said the coordinates of the red dots are random, but he can find them out by traversing the 500 x 300 grid

I still think it's not very efficient to go and traverse the grid starting at top left to bottom right to get the coordinates, and then calculate their distances, then pick the closest one. It will work, but it has a lot of overhead. I still think it's easiest to traverse it starting at the center in a circular motion and adapt the algorithm a little bit considering the actual distance.

I think you can do this yourself, either by brute force or by a clever method. Just to help you create an algorithm for it, below an updated view with actual distances of a 15 x 9 grid:

```["8.06", "7.21", "6.40", "5.66", "5.00", "4.47", "4.12", "   4", "4.12", "4.47", "5.00", "5.66", "6.40", "7.21", "8.06"]

["7.62", "6.71", "5.83", "5.00", "4.24", "3.61", "3.16", "   3", "3.16", "3.61", "4.24", "5.00", "5.83", "6.71", "7.62"]

["7.28", "6.32", "5.39", "4.47", "3.61", "2.83", "2.24", "   2", "2.24", "2.83", "3.61", "4.47", "5.39", "6.32", "7.28"]

["7.07", "6.08", "5.10", "4.12", "3.16", "2.24", "1.41", "   1", "1.41", "2.24", "3.16", "4.12", "5.10", "6.08", "7.07"]

["   7", "   6", "   5", "   4", "   3", "   2", "   1", "   0", "   1", "   2", "   3", "   4", "   5", "   6", "   7"]

["7.07", "6.08", "5.10", "4.12", "3.16", "2.24", "1.41", "   1", "1.41", "2.24", "3.16", "4.12", "5.10", "6.08", "7.07"]

["7.28", "6.32", "5.39", "4.47", "3.61", "2.83", "2.24", "   2", "2.24", "2.83", "3.61", "4.47", "5.39", "6.32", "7.28"]

["7.62", "6.71", "5.83", "5.00", "4.24", "3.61", "3.16", "   3", "3.16", "3.61", "4.24", "5.00", "5.83", "6.71", "7.62"]

["8.06", "7.21", "6.40", "5.66", "5.00", "4.47", "4.12", "   4", "4.12", "4.47", "5.00", "5.66", "6.40", "7.21", "8.06"]```

Or maybe a smaller one, this is the 5x5 grid I also gave you earlier in this topic, but now with real distances:

```["2.83", "2.24", "   2", "2.24", "2.83"]
["2.24", "1.41", "   1", "1.41", "2.24"]
["   2", "   1", "   0", "   1", "   2"]
["2.24", "1.41", "   1", "1.41", "2.24"]
["2.83", "2.24", "   2", "2.24", "2.83"]```

If you are to evaluate this lowest to highest, the sequence would be:

```5 4 3 4 5
4 2 1 2 4
3 1 B 1 3
4 2 1 2 4
5 4 3 4 5```

If you somehow traverse the array starting at the center in this order, the first red dot you find is the closest. There might be some other way using obscure math formulas I don't know though -- not talking about Pythagoras

Edited by dani

##### Share on other sites

Do you know the coordinates of the red dots and do you understand Pythagoras's theorem?

yes, the red dots are known though they are generated randomly

like each dot has it's own array \$red1[0] and \$red1[1]

...

i maybe should have mentioned that there are 8 red dots

and yes i do understand pyth's theorem ^^

##### Share on other sites

Err, you do not even have to find the red dots? This makes the problem much easier, just calculate the distances to those 8 points and pick the smallest one

##### Share on other sites

yes, the red dots are known though they are generated randomly

like each dot has it's own array \$red1[0] and \$red1[1]

...

i maybe should have mentioned that there are 8 red dots

and yes i do understand pyth's theorem ^^

Ok, then you should be able to calculate the distances. Can you calculate the distance to the first red dot and show some code? (At \$red1[0],\$red1[1])

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

##### Share on other sites

You can use this function to calculate the distance from 1 node to the center:

```; Change these as required
Global \$center_x = 2
Global \$center_y = 2

Func CalculateDistance(\$x, \$y)
\$dist_x = Abs(\$center_x - \$x)
\$dist_y = Abs(\$center_y - \$y)

If Not (\$dist_x and \$dist_y) Then ; In the same row or column as the centernode
Return \$dist_x + \$dist_y
Else                              ; Need to use Pythagoras
Return Sqrt(\$dist_x^2 + \$dist_y^2)
EndIf
EndFunc```

Now you only have to loop the array of known coordinates and execute this function for every combination of x and y. Then just pick the lowest distance.

Edited by dani

##### Share on other sites

Err, you do not even have to find the red dots? This makes the problem much easier, just calculate the distances to those 8 points and pick the smallest one

Ok, then you should be able to calculate the distances. Can you calculate the distance to the first red dot and show some code? (At \$red1[0],\$red1[1])

sorry dani for being so unspecific

so yeah, i did so adjustement

and did something like this with pyth theorem

with 8 red dots

```global \$x [8]
\$nr =0
Do
\$x[\$nr] = Sqrt(abs(\$red[\$nr] - \$black[0])^2 + abs(\$red[\$nr+1] - \$black[1])^2)
\$nr = \$nr + 2
until \$nr = 16```

though i don't know if there's an easy way for the computer to find the smallest value of x >_>

i've only been programing for 2-3 month; so please don't hate me for my incompetence >.<

##### Share on other sites

You can use this function to calculate the distance from 1 node to the center:

```; Change these as required
Global \$center_x = 2
Global \$center_y = 2

Func CalculateDistance(\$x, \$y)
\$dist_x = Abs(\$center_x - \$x)
\$dist_y = Abs(\$center_y - \$y)

If Not (\$dist_x and \$dist_y) Then ; In the same row or column as the centernode
Return \$dist_x + \$dist_y
Else                              ; Need to use Pythagoras
Return Sqrt(\$dist_x^2 + \$dist_y^2)
EndIf
EndFunc```

Now you only have to loop the array of known coordinates and execute this function for every combination of x and y. Then just pick the lowest distance.

thanks a lot dani ^^

##### Share on other sites

Come on ciliath, you are in some programming class I am sure you can figure out how to do this if you review your lecture again or study some other study material

Anyway, I decided to write a function for you which will return the smallest value of an array. This is really the last I will do though, you should learn !

```Func SmallestElement(\$aArray) ; \$aArray should have at least 1 element
\$iSmallest = \$aArray[0]
For \$i = 1 To UBound(\$aArray)-1
If \$aArray[\$i] < \$iSmallest Then \$iSmallest = \$aArray[\$i]
Next
Return \$iSmallest
EndFunc```

Good luck with the rest -- it really isn't that hard you can do it

Edited by dani

## Create an account

Register a new account