Languages & Coding > Blitz2D, BlitzPlus, Blitz3D

Calculate distance between 2 coordinates, fails

(1/1)

Santiago:
Hi good day!
I am trying to calculate distance between two objects that have their position by coordinate system "Latitude and Longitude"

I tried to do it using different methods, but failed in all attempts.

The general formula, in blitz3D gives me bad, since having no precision of numbers when the distances are short ends up rounding and resulting in values that are not real.

have you ever dealt with this dilemma?

I share with you my failed attempts, which lack precision and have flaws in the results.

blitz3d 1   fail

dist#  = 6371* ACos(    v1#  )

;resultado# = valor# * (Pi / 180 )
;DebugLog texto\$ + " : " + resultado#

End Function
--- End code ---

--- Code: ---;-----------------------------------------------------------------------
;
; Calculo distancia entre dos puntos terrestres.
; by Ignacio L.
;
;-----------------------------------------------------------------------

Const width = 640, height = 480

Graphics width, height

Local Aux#, Ra#, Dist#, Dist2#, Lat1#, Lat2#, Long1#, Long2#

Lat1#=0.145

Lat2#=90.21

Long1#=56.001

Long2#=56.21

Ra#=Pi/180

Aux#=(Cos(Ra#*(90-Lat1#))*Cos(Ra#*(90-Lat2#)))+(Sin(Ra#*(90-Lat1#))*Sin(Ra#*(90-Lat2#))*Cos(Ra#*(Long1#-Long2#)))

Dist# = 6371*ACos(Aux#)

Dist2#= Dist#/1.852

Print "Calculo Auxiliar" + Aux#

Print Dist# + "  Km"

Print Dist2# + "  Mn"

WaitKey()
--- End code ---

the ecuation in excel works perfect
but i can't do it on blitz3D

EXCEL WORKS :
Lat 1             Long 1         Lat 2      Long 2   dist km   dist MN
-0.00128102   -0.0821921   0.132677   -0.0854198   14.89977544   8.04523512
-0.00721392   0.425742   -0.001442   -0.0825546   56.52364685   30.52032767

1.570796327

Matty:
Out of curiosity in blitz3d what happens if you change '90' to '90.0'?

Reason I ask is perhaps blitz3d is performing a calculation with integers rather than floats at this point in your equation.  That happens in some languages.

Santiago:
hi Matty, thanks, i try it but same result

Santiago:

its hard to me do it with calculation without presition

so i make a 3d earth, place the contacts on surface and get the 3d distance

simple way to resolve this problem.

I share the code i make.

regards!

--- Code: ---;-----------------------------------------------------------------------
;
; Calculo distancia entre dos puntos terrestres. utilizando una esfera en unidades reales
; by. Santiago Gonzalez
;
;-----------------------------------------------------------------------

Graphics3D 1800 , 1000,0 ,2

Global planeta = CreateSphere(18)

EntityFX planeta,4
EntityAlpha planeta,1

If FileType("maps\planeta.jpg") = 1 Then
t_planeta = LoadTexture("maps\planeta.jpg")     ;  Textura planeta sobre esfera
EntityTexture planeta,t_planeta
Else
EntityColor planeta,100,100,200
EndIf

Global cam = CreateCamera()

Global light = CreateLight(1)

;Lat 1             Long 1         Lat 2      Long 2   dist km   dist MN
;-0.00128102   -0.0821921   0.132677   -0.0854198   14.89977544   8.04523512
;-0.00721392   0.425742   -0.001442   -0.0825546   56.52364685   30.52032767

Lat1#=-0.00128102
Long1#=-0.0821921

Lat2#=0.132677
Long2#=-0.0854198

Lat1#= -0.00721392
Long1#= 0.425742

Lat2#= -0.001442
Long2#= -0.0825546

crear_contacto("barco 1",lat1#,long1#,0)
crear_contacto("barco 2",lat2#,long2#,0)

While Not KeyHit(1)

;MoveEntity cam,0,MouseYSpeed()*10000,0
TurnEntity planeta,0,.001,0

PointEntity cam,planeta
RenderWorld()

calc_distancia#("barco 1","barco 2")

Flip

Wend

End

Function calc_distancia#(origen\$,destino\$)

For c.contacto = Each contacto

If c\nombre\$ = origen\$ Then

For c2.contacto = Each contacto

If c2\nombre\$  = destino\$
dist# = EntityDistance(c\pivot,c2\pivot)

Text 30,100,"lat 1 : " + c\lat
Text 30,120,"Long 1 : " + c\long

Text 30,170,"lat 2 : " + c2\lat
Text 30,190,"Long 2 : " + c2\long

Text 30,230,"dist : " + dist#   + " metros"
Text 30,250,"dist : " + dist#*.001   + " km"

Text 30,300 , "resultado deseado : 56.52364685 km  30.52032767 mn     " ;14.89977544 km   8.04523512 mn"
End If
Next

End If

Next

Return dist#

End Function

Type contacto

Field acquire_ID

Field pivot

Field spd# ; velocidad en nudos
Field hdg# ; rumbo gyrocompas grados
Field lat#
Field long#

Field tail_lat#[10]
Field tail_long#[10]

Field largo#
Field ancho#
Field alto#
Field nombre\$

Field screen_x
Field screen_y

Field selected
Field visible    ; cuenta s fue visible en la ultima pasada de radar

Field archivo\$ ;nombre del archivo ship_ara_pepe.txt

Field hora_archivo
Field time_detected_in_frames
Field last_update ;millisecs()

End Type

Function crear_contacto(nombre\$,lat#,long#,rumbo#)

c.contacto = New contacto
c\pivot = CreatePivot(planeta)

e = 100000

c\lat# = lat#
c\long# = long#
c\hdg# = rumbo#
c\nombre\$ = nombre\$

TurnEntity c\pivot,lat#,long#,0