Android Question Find closest match between 2 numbers ?

G-ShadoW

Active Member
Licensed User
Longtime User
Like in title said, how to find closest match between 2 numbers
Expert help needed

I have a txt file that contains lines like this...

B4X:
45.2722;18.847421239;023546;31052015;12;3
43.342171;17.45648532;123546;12051995;12;24
46.954171;16.154645;223546;31062022;11;6
43.132171;15.8474;023546;26111976;14;3

and I have 2 textboxes with ( texbox -> Edittext )

first textbox look's like this
45.295768


second texbox
18.8512348


to separate strings in text lines I use " ; "

so basicly first line in this example ist closest match, but how I can get this line of text file ?
and when I find closest match, I need to select that whole line and insert it in texbox 3 ?

the lenght of first and second number in txt file is not smaller than xx.xxxx but it can be bigger xx.xxxxxxxx
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
@Peter Simpson I like the way you've manipulated the subtraction so that the smaller number is always taken away from the larger number, I'd not thought of doing it that way... very clever!
My method is slightly more compact but less obvious...
B4X:
Abs(SplitLine(0) - SplitLine(1))
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
I see Abs (no not Anitlock Braking System) every time I press Ctrl + Space, but I've never ever used it before, so cheers for the above example, I will definitely keep that one in mind :)

BTW talking about Ctrl + Space for Auto Complete, @G-ShadoW have you watched the two videos below from the E-Man? If you have not watched them you should stop whatever you are doing right now and watch them. You will learn a lot in just a few minutes...

First watch this video

Then watch this video
 
Last edited:
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
For others that might have similar problems, this is what we have come up with...
B4X:
Sub btnSearch_Click
    Dim p1_x, p1_y, distanceNew As Float
    Dim distanceShortest As Float = Power(10, 38) ' Largest positive flosting point number possible (I think)
    Dim intIndex As Int
    Dim a As Float = "4516.33696"
    Dim b As Float = "1850.84114"

    txt2.Text = ""
    txt3.Text = ""
    txt4.Text = ""
    txt5.Text = ""

    ' I'm taking a guess that these values will be static whilst looping through the list!!!
    Dim p2_x As Float = a 'txtLat.Text
    Dim p2_y As Float = b 'txtlon.Text

    For i = 0 To MyList.Size - 1
        Dim Line As String = MyList.Get(i)
        Dim SplitLine() As String = Regex.Split(";", Line.Replace(":", ""))
     
        p1_x = SplitLine(0)
        p1_y = SplitLine(1)

        distanceNew = Power(p1_x - p2_x, 2) + Power(p1_y - p2_y, 2) ' I had to look up how to use to the power of a number :-)
        Log("dist = " & distanceNew & " shortest=" & distanceShortest)
        If distanceNew < distanceShortest Then
            distanceShortest = distanceNew
            intIndex = i
            Log("New shotest distance found... " & distanceShortest & " at index position " & intIndex)
        End If
    Next

    Log("###### Shortest distance found at index " & intIndex)
End Sub
@thedesolatesoul why did your solution to calculate the difference use to the power of 2? My own solution would have been...
B4X:
distanceNew = Abs(p1_x - p2_x) + Abs(p1_y - p2_y)
Both yield the same result so happy days! :D
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
thanks randomcoder, that is the exactly solution I had in mind.
I used square as that is usually how we find the distance in 2d space, simple put
V = (Vx^2 + Vy^2)^(1/2)
but for the sake of comparison we usually drop the square root as it isn't needed.
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
thanks randomcoder, that is the exactly solution I had in mind.
I used square as that is usually how we find the distance in 2d space, simple put
V = (Vx^2 + Vy^2)^(1/2)
but for the sake of comparison we usually drop the square root as it isn't needed.
I get the feeling that your some sort of mathematician ;)
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
No ;), that just resolves two points (or vectors) into a single one.
The more complicated (but not 100% accurate) method to calculate the distance based on lat/long is somewhat like this.
Is it any wonder that I always disliked maths? The only time I actually ever made any sense of it was when they replaced the numbers with letters, but maybe I'm just strange as most folk hate algebra :D
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
thanks randomcoder, that is the exactly solution I had in mind.
I used square as that is usually how we find the distance in 2d space, simple put
V = (Vx^2 + Vy^2)^(1/2)
but for the sake of comparison we usually drop the square root as it isn't needed.
It's finally dawned on me where I know this equation from!!! It's basic trigonometry a^2 + b^2=c^2 anything to the power of a half is the same as square rooting that value. :oops:
 
Upvote 0
Top