lua - 太阳在 Lua 中的位置(方位角)

标签 lua sdk coronasdk sun azimuth

我在网上只能找到LUA中的一个函数,但它给出的值是错误的(用专业的在线工具测量)。

看起来从日出到中午之后的一段时间,数学是有效的,但之后,太阳的角度又回到了日出位置。应该是从 106° 到 253°,目前是从 106° 到 ~180° 到 106°。

我正在使用的功能:

-- solar altitude, azimuth (degrees)
function sunposition(latitude, longitude, time)
    time = time or os.time()
    if type(time) == 'table' then time = os.time(time) end

    local date = os.date('*t', time)
    local timezone = (os.time(date) - os.time(os.date('!*t', time))) / 3600
    if date.isdst then timezone = timezone + 1 end

    local utcdate = os.date('*t', time - timezone * 3600)
    local latrad = math.rad(latitude)
    local fd = (utcdate.hour + utcdate.min / 60 + utcdate.sec / 3600) / 24
    local g = (2 * math.pi / 365.25) * (utcdate.yday + fd)
    local d = math.rad(0.396372 - 22.91327 * math.cos(g) + 4.02543 * math.sin(g) - 0.387205 * math.cos(2 * g)
      + 0.051967 * math.sin(2 * g) - 0.154527 * math.cos(3 * g) + 0.084798 * math.sin(3 * g))
    local t = math.rad(0.004297 + 0.107029 * math.cos(g) - 1.837877 * math.sin(g)
      - 0.837378 * math.cos(2 * g) - 2.340475 * math.sin(2 * g))
    local sha = 2 * math.pi * (fd - 0.5) + t + math.rad(longitude)

    local sza = math.acos(math.sin(latrad) * math.sin(d) + math.cos(latrad) * math.cos(d) * math.cos(sha))
    local saa = math.acos((math.sin(d) - math.sin(latrad) * math.cos(sza)) / (math.cos(latrad) * math.sin(sza)))

    return 90 - math.deg(sza), math.deg(saa)
end

请求示例:

lat, long = 45.327063, 14.442176 -- Rijeka, Croatia
time = {year=2016, month=2, day=17, hour=17, min=30} -- end of the day
altitude, azimuth = sunposition(lat, long, time)

结果是:

  • 海拔-0.1度
  • 方位角 106 度。

结果应该是:

  • 海拔-0.1度
  • 方位角 253 度。

我在其他编程语言中找到了多种解决方案,甚至尝试用Lua重写但没有成功。解决方案背后的数学太复杂。

我将其用于我的 Corona SDK 应用程序,该应用程序将显示太阳相对于设备的位置。目前唯一有效的解决方案是 PHP 或 Javascript 脚本,我的应用程序可以通过 Internet 上的 API 调用来询问,但我真的想避免这种情况。

我非常感谢社区的帮助。谢谢你们并爱你们! :)

最佳答案

我找到了解决此问题的方法/技巧。

功能仍然相同:

-- solar altitude, azimuth (degrees)
function sunposition(latitude, longitude, time)
    time = time or os.time()
    if type(time) == 'table' then time = os.time(time) end

    local date = os.date('*t', time)
    local timezone = (os.time(date) - os.time(os.date('!*t', time))) / 3600
    if date.isdst then timezone = timezone + 1 end

    local utcdate = os.date('*t', time - timezone * 3600)
    local latrad = math.rad(latitude)
    local fd = (utcdate.hour + utcdate.min / 60 + utcdate.sec / 3600) / 24
    local g = (2 * math.pi / 365.25) * (utcdate.yday + fd)
    local d = math.rad(0.396372 - 22.91327 * math.cos(g) + 4.02543 * math.sin(g) - 0.387205 * math.cos(2 * g)
      + 0.051967 * math.sin(2 * g) - 0.154527 * math.cos(3 * g) + 0.084798 * math.sin(3 * g))
    local t = math.rad(0.004297 + 0.107029 * math.cos(g) - 1.837877 * math.sin(g)
      - 0.837378 * math.cos(2 * g) - 2.340475 * math.sin(2 * g))
    local sha = 2 * math.pi * (fd - 0.5) + t + math.rad(longitude)

    local sza = math.acos(math.sin(latrad) * math.sin(d) + math.cos(latrad) * math.cos(d) * math.cos(sha))
    local saa = math.acos((math.sin(d) - math.sin(latrad) * math.cos(sza)) / (math.cos(latrad) * math.sin(sza)))

    return 90 - math.deg(sza), math.deg(saa)
end

解决问题的添加代码:

function getSunPos(lat, long, time)
    findTime = {}
    findTime.hour, findTime.min = time.hour, time.min
    fixedAzimuthLast, fixedAzimuth = 0, 0
    for i=0,23 do
        for j=0,59 do
            time.hour, time.min = i, j
            local altitude, azimuth = sunposition(lat, long, time)
            -- fix azimuth
            if fixedAzimuthLast < azimuth then 
                fixedAzimuthLast = azimuth
                fixedAzimuth = fixedAzimuthLast
            else
                fixedAzimuth = fixedAzimuthLast + (180 - azimuth)
            end
            -- find azimuth at target time
            if findTime.hour == i and findTime.min == j then
                -- final result
                return altitude, fixedAzimuth
            end
        end
    end
end

最终得到正确的结果:

lat, long = 45.327063, 14.442176
altitude, azimuth = getSunPos(lat, long, os.date('*t', os.time()))

就是这样。 我会对正确执行数学运算的完整函数感到满意,但这就足够了。它有效并在全局 3 个地点进行了测试。

关于lua - 太阳在 Lua 中的位置(方位角),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35467309/

相关文章:

regex - 需要从 url 中删除特定参数

android - 在实际设备中安装 .apk 文件仅用于测试

c++ - WS_EX_TOOLWINDOW 在窗口销毁时跳转到背景

lua - 更改表中的变量

coronasdk - 如何在 Corona 中执行 sleep() 或 wait() ?

json - Lua-cjson -> require ("cjson") 成功,然后调用 cjson.encode 时出错

function - Lua如何创建可用于变量后续的自定义函数

正则表达式用两个连续的管道 || 分割字符串

java - 找不到属性 ANDROID_BUILD_TOOLS_VERSION

events - 在 Corona SDK 中对象的 alpha 为 0 时获取触摸事件