Cartesian & Polar coverting some source

Just starting out? Need help? Post your questions and find answers here.
ehowington
Enthusiast
Enthusiast
Posts: 115
Joined: Sat Sep 12, 2009 3:06 pm

Cartesian & Polar coverting some source

Post by ehowington »

Wonder If I could get some input from anyone if this seems correct or better way to do it?

Code: Select all

EarthRadius.d = 6378.1370; 6372.795477598;6,378.1370
Structure point3d
  x.d
  y.d
  z.d
EndStructure
Structure point2d
  x.d
  y.d
EndStructure
ProcedureDLL DEG_TO_RAD()
  ProcedureReturn 1/0.0174532925199432958
EndProcedure
ProcedureDLL RAD_TO_DEG()
  ProcedureReturn 1/57.29577951308232
EndProcedure 
; /**
;  * Convert [lat,lon] polar coordinates To [x,y,z] cartesian coordinates
;  * @param {Number} lon
;  * @param {Number} lat
;  * @param {Number} radius
;  * @return {Vector3}
;  */
; Latitude is the Y axis, longitude is the X axis.
; function polarToCartesian( lon, lat, radius ) {
; 
;   var phi = ( 90 - lat ) * DEG2RAD
;   var theta = ( lon + 180 ) * DEG2RAD
; 
;   Return {
;     x: -(radius * Math.sin(phi) * Math.sin(theta)),
;     y: radius * Math.cos(phi),
;     z: radius * Math.sin(phi) * Math.cos(theta),
;   }
; 
; }
ProcedureDLL PolarToCartesian(x.d,y.d,r.d,*return.point3d)
  phi.d = (90 - y) * DEG_TO_RAD()
  theta.d =(x + 180) * DEG_TO_RAD()
  *return\x = (r * Sin(phi) * Sin(theta))
  *return\y = r * Cos(Phi)
  *return\z = r * Sin(phi) * Cos(theta)
  ProcedureReturn *return
EndProcedure

; /**
;  * Convert [x,y,z] cartesian coordinates To polar [lat,lon]
;  * @param {Vector3} coord
;  * @return {Array<Number>}
;  */
; function cartesianToPolar( coord, radius ) {
; 
; var lon = Math.atan2( coord.x, -coord.z ) * RAD2DEG
; var length = Math.sqrt( coord.x * coord.x + coord.z * coord.z )
; var lat = Math.atan2( coord.y, length ) * RAD2DEG
; 
; Return [ lon, lat ]
; 
; }


ProcedureDLL CartesianToPolar(coord.point3d,radius,*return.point2d)
  lon.d = ATan2(coord\x, coord\z) * RAD_TO_DEG
  length.d Sqr(coord\x, coord\x + coord\z * coord\z)
  lat.d = ATan2(coord\y,length) * RAD_TO_DEG
  *return\x = lon
  *return\y = lat
  ProcedureReturn *return
EndProcedure
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Cartesian & Polar coverting some source

Post by Demivec »

I took out the definitions for Deg2Rad and Rad2Deg and substituted the PureBasic functions Degree() and Radian().
Aside from that you had a few typos.

Code: Select all

EarthRadius.d = 6378.1370; 6372.795477598;6,378.1370
Structure point3d
  x.d
  y.d
  z.d
EndStructure

Structure point2d
  x.d
  y.d
EndStructure

; /**
;  * Convert [lat,lon] polar coordinates To [x,y,z] cartesian coordinates
;  * @param {Number} lon
;  * @param {Number} lat
;  * @param {Number} radius
;  * @return {Vector3}
;  */
; Latitude is the Y axis, longitude is the X axis.
; function polarToCartesian( lon, lat, radius ) {
; 
;   var phi = ( 90 - lat ) * DEG2RAD
;   var theta = ( lon + 180 ) * DEG2RAD
; 
;   Return {
;     x: -(radius * Math.sin(phi) * Math.sin(theta)),
;     y: radius * Math.cos(phi),
;     z: radius * Math.sin(phi) * Math.cos(theta),
;   }
; 
; }
ProcedureDLL PolarToCartesian(x.d,y.d,r.d,*return.point3d)
  phi.d = Radian(90 - y)
  theta.d = Radian(x + 180)
  *return\x = -(r * Sin(phi) * Sin(theta))
  *return\y = r * Cos(Phi)
  *return\z = r * Sin(phi) * Cos(theta)
  ProcedureReturn *return
EndProcedure

; /**
;  * Convert [x,y,z] cartesian coordinates To polar [lat,lon]
;  * @param {Vector3} coord
;  * @return {Array<Number>}
;  */
; function cartesianToPolar( coord, radius ) {
; 
; var lon = Math.atan2( coord.x, -coord.z ) * RAD2DEG
; var length = Math.sqrt( coord.x * coord.x + coord.z * coord.z )
; var lat = Math.atan2( coord.y, length ) * RAD2DEG
; 
; Return [ lon, lat ]
; 
; }

ProcedureDLL CartesianToPolar(*coord.point3d,radius,*return.point2d)
  lon.d = Degree(ATan2(*coord\x, *coord\z))
  length.d = Sqr(*coord\x * *coord\x + *coord\z * *coord\z)
  lat.d = Degree(ATan2(*coord\y,length))
  *return\x = lon
  *return\y = lat
  ProcedureReturn *return
EndProcedure
I did not test the code or evaluate it as to its effectiveness. I merely evaluated whether it was equivalent to the functionality of the original source code.
ehowington
Enthusiast
Enthusiast
Posts: 115
Joined: Sat Sep 12, 2009 3:06 pm

Re: Cartesian & Polar coverting some source

Post by ehowington »

Pointer null line 36?
Demivec wrote: Sun Jul 31, 2022 2:41 pm I took out the definitions for Deg2Rad and Rad2Deg and substituted the PureBasic functions Degree() and Radian().
Aside from that you had a few typos.

Code: Select all

EarthRadius.d = 6378.1370; 6372.795477598;6,378.1370
Structure point3d
  x.d
  y.d
  z.d
EndStructure

Structure point2d
  x.d
  y.d
EndStructure

; /**
;  * Convert [lat,lon] polar coordinates To [x,y,z] cartesian coordinates
;  * @param {Number} lon
;  * @param {Number} lat
;  * @param {Number} radius
;  * @return {Vector3}
;  */
; Latitude is the Y axis, longitude is the X axis.
; function polarToCartesian( lon, lat, radius ) {
; 
;   var phi = ( 90 - lat ) * DEG2RAD
;   var theta = ( lon + 180 ) * DEG2RAD
; 
;   Return {
;     x: -(radius * Math.sin(phi) * Math.sin(theta)),
;     y: radius * Math.cos(phi),
;     z: radius * Math.sin(phi) * Math.cos(theta),
;   }
; 
; }
ProcedureDLL PolarToCartesian(x.d,y.d,r.d,*return.point3d)
  phi.d = Radian(90 - y)
  theta.d = Radian(x + 180)
  *return\x = -(r * Sin(phi) * Sin(theta))
  *return\y = r * Cos(Phi)
  *return\z = r * Sin(phi) * Cos(theta)
  ProcedureReturn *return
EndProcedure

; /**
;  * Convert [x,y,z] cartesian coordinates To polar [lat,lon]
;  * @param {Vector3} coord
;  * @return {Array<Number>}
;  */
; function cartesianToPolar( coord, radius ) {
; 
; var lon = Math.atan2( coord.x, -coord.z ) * RAD2DEG
; var length = Math.sqrt( coord.x * coord.x + coord.z * coord.z )
; var lat = Math.atan2( coord.y, length ) * RAD2DEG
; 
; Return [ lon, lat ]
; 
; }

ProcedureDLL CartesianToPolar(*coord.point3d,radius,*return.point2d)
  lon.d = Degree(ATan2(*coord\x, *coord\z))
  length.d = Sqr(*coord\x * *coord\x + *coord\z * *coord\z)
  lat.d = Degree(ATan2(*coord\y,length))
  *return\x = lon
  *return\y = lat
  ProcedureReturn *return
EndProcedure
I did not test the code or evaluate it as to its effectiveness. I merely evaluated whether it was equivalent to the functionality of the original source code.
ehowington
Enthusiast
Enthusiast
Posts: 115
Joined: Sat Sep 12, 2009 3:06 pm

Re: Cartesian & Polar coverting some source

Post by ehowington »

Code: Select all

EarthRadius.d = 6378.1370; 6372.795477598;6,378.1370
Structure point3d
  x.d
  y.d
  z.d
EndStructure

Structure point2d
  x.d
  y.d
EndStructure

; /**
;  * Convert [lat,lon] polar coordinates To [x,y,z] cartesian coordinates
;  * @param {Number} lon
;  * @param {Number} lat
;  * @param {Number} radius
;  * @return {Vector3}
;  */
; Latitude is the Y axis, longitude is the X axis.
; function polarToCartesian( lon, lat, radius ) {
; 
;   var phi = ( 90 - lat ) * DEG2RAD
;   var theta = ( lon + 180 ) * DEG2RAD
; 
;   Return {
;     x: -(radius * Math.sin(phi) * Math.sin(theta)),
;     y: radius * Math.cos(phi),
;     z: radius * Math.sin(phi) * Math.cos(theta),
;   }
; 
; }
ProcedureDLL PolarToCartesian(x.d,y.d,r.d,*return.point3d)
  phi.d = Radian(90 - y)
  theta.d = Radian(x + 180)
  *return\x = -(r * Sin(phi) * Sin(theta))
  *return\y = r * Cos(Phi)
  *return\z = r * Sin(phi) * Cos(theta)
  ProcedureReturn *return
EndProcedure

; /**
;  * Convert [x,y,z] cartesian coordinates To polar [lat,lon]
;  * @param {Vector3} coord
;  * @return {Array<Number>}
;  */
; function cartesianToPolar( coord, radius ) {
; 
; var lon = Math.atan2( coord.x, -coord.z ) * RAD2DEG
; var length = Math.sqrt( coord.x * coord.x + coord.z * coord.z )
; var lat = Math.atan2( coord.y, length ) * RAD2DEG
; 
; Return [ lon, lat ]
; 
; }

ProcedureDLL CartesianToPolar(*coord.point3d,radius,*return.point2d)
  lon.d = Degree(ATan2(*coord\x, *coord\z))
  length.d = Sqr(*coord\x * *coord\x + *coord\z * *coord\z)
  lat.d = Degree(ATan2(*coord\y,length))
  *return\x = lon
  *return\y = lat
  ProcedureReturn *return
EndProcedure
Dim test.Point2D(1)
result1 = PolarToCartesian(0,0,EarthRadius,@vertices.Point2D)
Debug vertices\x
Debug vertices\y
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Cartesian & Polar coverting some source

Post by Demivec »

You seem to have figured out some of the null pointer issues for the first procedure, but not all.

Code: Select all

EarthRadius.d = 6378.1370; 6372.795477598;6,378.1370
Structure point3d
  x.d
  y.d
  z.d
EndStructure

Structure point2d
  x.d
  y.d
EndStructure

; /**
;  * Convert [lat,lon] polar coordinates To [x,y,z] cartesian coordinates
;  * @param {Number} lon
;  * @param {Number} lat
;  * @param {Number} radius
;  * @return {Vector3}
;  */
; Latitude is the Y axis, longitude is the X axis.
; function polarToCartesian( lon, lat, radius ) {
; 
;   var phi = ( 90 - lat ) * DEG2RAD
;   var theta = ( lon + 180 ) * DEG2RAD
; 
;   Return {
;     x: -(radius * Math.sin(phi) * Math.sin(theta)),
;     y: radius * Math.cos(phi),
;     z: radius * Math.sin(phi) * Math.cos(theta),
;   }
; 
; }
ProcedureDLL PolarToCartesian(x.d, y.d, r.d, *return.point3d)
  phi.d = Radian(90 - y)
  theta.d = Radian(x + 180)
  *return\x = -(r * Sin(phi) * Sin(theta))
  *return\y = r * Cos(Phi)
  *return\z = r * Sin(phi) * Cos(theta)
  :*return parameter contains the return value
EndProcedure

; /**
;  * Convert [x,y,z] cartesian coordinates To polar [lat,lon]
;  * @param {Vector3} coord
;  * @return {Array<Number>}
;  */
; function cartesianToPolar( coord, radius ) {
; 
; var lon = Math.atan2( coord.x, -coord.z ) * RAD2DEG
; var length = Math.sqrt( coord.x * coord.x + coord.z * coord.z )
; var lat = Math.atan2( coord.y, length ) * RAD2DEG
; 
; Return [ lon, lat ]
; 
; }

ProcedureDLL CartesianToPolar(*coord.point3d, radius, *return.point2d)
  lon.d = Degree(ATan2(*coord\x, *coord\z))
  length.d = Sqr(*coord\x * *coord\x + *coord\z * *coord\z)
  lat.d = Degree(ATan2(*coord\y, length))
  *return\x = lon
  *return\y = lat
  :*return parameter contains the return value
EndProcedure

Dim test.Point2D(1)
PolarToCartesian(0, 0, EarthRadius, @vertices.Point2D)
Debug vertices\x
Debug vertices\y

;can also call it this way
PolarToCartesian(0, 0, EarthRadius, @test.point2d(0))
Debug test(0)\x
Debug test(0)\y

; test for second procedure
CartesianToPolar(@coord.point3d, EarthRadius, @test.point2d(1))
Debug test(1)\x
Debug test(1)\y
Olli
Addict
Addict
Posts: 1244
Joined: Wed May 27, 2020 12:26 pm

Re: Cartesian & Polar coverting some source

Post by Olli »

A pointor null error, in this way, seems you have to prepare the memory place. Demivec concept allows you to stay free to choose which type of memory place you want to manage :

Example for a structured variable named 'p' :

Code: Select all

Define p.point3d
PolarToCartesian(3.0, 100.0, 3000.0, p)
Post Reply