登录
首页 >  Golang >  Go问答

坐标基础转换

来源:stackoverflow

时间:2024-03-06 12:54:25 365浏览 收藏

来到golang学习网的大家,相信都是编程学习爱好者,希望在这里学习Golang相关编程知识。下面本篇文章就来带大家聊聊《坐标基础转换》,介绍一下,希望对大家的知识积累有所帮助,助力实战开发!

问题内容

我正在编写一个库,可以将任何类型的坐标转换为另一种坐标。

我正在用 go 编写它。我正在通过将坐标转换为一种类型然后转换回初始类型来测试转换。那么我应该获得相同的起始值(包括浮点精度误差)。

我对 spherical.tocartesian 实现很确定,因为当我测试 keisan 中的值时,我获得了相同的值。

这是 coordinate.go 文件:

import (
    . "math"
)

/////////////
// cartesian coordinates
/////////////
type cartesian struct {
    x float64 `json:"x"`
    y float64 `json:"y"`
    z float64 `json:"z"`
}

// func (c cartesian) tocartesian() cartesian {...}

// following : https://keisan.casio.com/exec/system/1359533867
func (c cartesian) tospherical() spherical{
    r := sqrt(pow(c.x, 2.) + pow(c.y, 2.) + pow(c.z, 2.))
    return spherical{
        latitude:  radtodeg(atan(sqrt(c.x * c.x + c.y * c.y) / c.z)),
        longitude: radtodeg(atan2(c.y, c.x)),
        radius: r,
    }
}

// func (c cartesian) topolar() polar {...}

/////////////
// spherical coordinates
/////////////
type spherical struct {
    radius    float64 `json:"radius"`
    // aka θ
    longitude float64 `json:"longitude"`
    // aka ϕ
    latitude  float64 `json:"latitude"`
}

// following : https://keisan.casio.com/exec/system/1359534351
func (g spherical) tocartesian() cartesian {
    return cartesian{
        // x = r * sin ϕ * cos θ
        x: g.radius * sin(degtorad(g.latitude)) * cos(degtorad(g.longitude)),
        // y = r * sin ϕ * sin θ
        y: g.radius * sin(degtorad(g.latitude)) * sin(degtorad(g.longitude)),
        // z = r * cos ϕ
        z: g.radius * cos(degtorad(g.latitude)),
    }
}

这是不起作用的测试:

var g = spherical{
    longitude: 200,
    latitude:  100,
    radius:    10000,
}

func testspherical_tocartesiantospherical(t *testing.t) {
    v := g.tocartesian().tospherical()

    if !isfloateq(g.radius, v.radius) {
        t.error("bad radius conversion. expected", g.radius, "got", v.radius)
    }
    if !isfloateq(g.longitude, v.longitude) {
        t.error("bad longitude conversion. expected", g.longitude, "got", v.longitude)
    }
    if !isfloateq(g.latitude, v.latitude) {
        t.error("bad latitude conversion. expected", g.latitude, "got", v.latitude)
    }
}

当我 go test 时,我得到这个:

--- FAIL: TestGeographic_ToCartesianToGeographic (0.00s)
    geographic_test.go:34: Bad Longitude conversion. Expected 200 got -160
    geographic_test.go:37: Bad Latitude conversion. Expected 100 got -80
FAIL
FAIL    common/coordinates      0.113s
FAIL

我真的不明白问题出在哪里。

希望得到任何帮助:)


解决方案


球坐标表示为:

  1. r:半径,距原点的直线距离。
  2. φ:倾角或极角,即与垂直轴 z 的角度。
  3. θ:方位角或方位角,即投影点从 x 轴到 xy 平面的角度。

注意1:在物理学中,用于表示两个角度的希腊字母会互换,但我将使用这种符号,因为它似乎是您根据公式使用的符号。

注2:还有另一种表达倾角的方法,称为仰角,它是从 xy 平面测量的。 zqbczq仰角 = 90° - φ。

注3:在地理中,海拔称为纬度,方位角称为经度。

与笛卡尔坐标的不同之处在于,在笛卡尔坐标中,每个点都有一个单一的表示形式,而在球坐标中,同一点可以用不同的方式表示。以下变换都创建新点 p2 = {r2, φ2, θ2} ,其值与原始点 p1 = {r1, φ1, θ1} 但它们都是相同的点 (p2 == p1)与众不同。

/ r2 = r1              / r2 = - r1
| φ2 = φ1              | φ2 = 180° - φ1
\ θ2 = θ1 + 360°       \ θ2 = θ1 + 180°

解决方案是什么?标准化球坐标。最常见的标准化系统仅使用正半径值并将角度限制为 180° 和 360°:

/ 0  <= r <  inf
| 0° <= φ <= 180°  -> which means -90° <= elevation <= 90°
\ 0° <= θ <  360°

之所以将倾角限制在180°,是因为方位角额外旋转180°,并且倾角低于180°,可以获得更高的倾角。

因此,为了能够比较值以检查它们是否相同,您首先需要进行标准化。请按照以下步骤操作:

  1. 如果 r = 0:返回 {0, 0°, 0°}(如果半径为零,角度不会改变任何内容,因此返回 0° 角度)
  2. 如果 r < 0r = -rφ = 180° - φθ += 180°
  3. φ >= 360° 时:φ -= 360°
  4. φ < 0° 时:φ += 360°
  5. 如果 φ = 0°φ = 180°:返回 {r, φ, 0°} (如果倾角为零或 180°,则该点位于垂直轴 z 上,因此方位角不存在)没有任何意义,使用 0°)
  6. 如果 φ > 180°φ = 360° - φθ += 180°
  7. θ >= 360° 时:θ -= 360°
  8. θ < 0° 时:θ += 360°
  9. 返回{r, φ, θ}

在地理中,经度有时会标准化为 -180° < θ <= 180°,这会将步骤 7 和 8 修改为:

  1. θ > 180° 时:θ -= 360°
  2. θ <= -180° 时:θ += 360°

这里有一个 Playground link,其中的类实现了 cartesiansphericalgeograpical 的归一化和转换方法(一个 spherical 但它是从创建并打印纬度和经度而不是倾角和方位角) )。

今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>