November 27, 2020, 05:54:06 AM

### Author Topic: Calculate distance between 2 coordinates, fails  (Read 213 times)

#### Santiago

• Sr. Member
• Posts: 283
• Blitz3d
##### Calculate distance between 2 coordinates, fails
« on: October 08, 2020, 04:22:24 PM »
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
Code: [Select]
`v1# =   Cos(    radians(90-lat1#,1))*Cos(radians(90-lat2#,2))  +  Sin(radians(90-lat1#,3))*Sin(radians(90-lat2#,4)) * Cos(radians(long2#-long1#,5)) dist#  = 6371* ACos(    v1#  )Function radians#(valor#,texto\$) ;resultado# = valor# * (Pi / 180 ) resultado# = valor# * 0.0174533 ;DebugLog texto\$ + " : " + resultado# Return resultado# End Function`
Code: [Select]
`;-----------------------------------------------------------------------; ; 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.145Lat2#=90.21Long1#=56.001Long2#=56.21Ra#=Pi/180Aux#=(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.852Print "Calculo Auxiliar" + Aux# Print Dist# + "  Km"Print Dist2# + "  Mn"WaitKey()`

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

• Hero Member
• Posts: 1253
##### Re: Calculate distance between 2 coordinates, fails
« Reply #1 on: October 08, 2020, 06:17:58 PM »
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

• Sr. Member
• Posts: 283
• Blitz3d
##### Re: Calculate distance between 2 coordinates, fails
« Reply #2 on: October 08, 2020, 06:35:26 PM »
hi Matty, thanks, i try it but same result

#### Santiago

• Sr. Member
• Posts: 283
• Blitz3d
##### Re: Calculate distance between 2 coordinates, fails
« Reply #3 on: October 08, 2020, 07:17:37 PM »

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: [Select]
`;-----------------------------------------------------------------------; ; Calculo distancia entre dos puntos terrestres. utilizando una esfera en unidades reales; by. Santiago Gonzalez;;-----------------------------------------------------------------------Graphics3D 1800 , 1000,0 ,2Global radio# =  1852.0*60.0*57.29578Global planeta = CreateSphere(18)FitMesh planeta,-radio#,-radio#,-radio#,radio*2,radio*2,radio*2,1EntityFX planeta,4EntityAlpha planeta,1If FileType("maps\planeta.jpg") = 1 Then t_planeta = LoadTexture("maps\planeta.jpg")     ;  Textura planeta sobre esfera EntityTexture planeta,t_planetaElse EntityColor planeta,100,100,200EndIfSetFont LoadFont("Terminal",20)Global cam = CreateCamera()CameraRange cam,100,radio#*4MoveEntity cam,0,0,-radio*3Global light = CreateLight(1)MoveEntity light,0,radio*2,0;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.52032767Lat1#=-0.00128102Long1#=-0.0821921Lat2#=0.132677Long2#=-0.0854198Lat1#= -0.00721392Long1#= 0.425742Lat2#= -0.001442Long2#= -0.0825546crear_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 WendEndFunction 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 FunctionType contacto Field acquire_ID Field pivot Field entidad 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 radar_x Field radar_y 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 TypeFunction crear_contacto(nombre\$,lat#,long#,rumbo#) c.contacto = New contacto c\pivot = CreatePivot(planeta) c\entidad = CreateSphere(8,c\pivot) e = 100000 ScaleEntity c\entidad,e,e,e c\lat# = lat# c\long# = long# c\hdg# = rumbo# c\nombre\$ = nombre\$ TurnEntity c\pivot,lat#,long#,0 MoveEntity c\pivot,0,0,-radio# EntityColor c\entidad,Rnd(255),Rnd(255),Rnd(255) End Function`

#### 3DzForMe

• Hero Member
• Posts: 1136
##### Re: Calculate distance between 2 coordinates, fails
« Reply #4 on: October 11, 2020, 09:47:01 AM »
@Santiago - interesting stuff.

I may well dust off my copy of Blitz3D later and give this a whirl, intriguing stuff