Lua 中的太阳位置(方位角)
2016-2-20 17:47:22
收藏:0
阅读:94
评论:2
我在网上只找到了一个 LUA 中的函数,但它给出了错误的值(使用专业的在线工具进行比较)。
从日出到正午后的一段时间内,这个函数似乎运行良好,但是午后,太阳的角度回到了日出的位置。应该是106°到253°,但目前是从106°到~180°再回到106°。
我正在使用的函数:
-- 太阳高度、方位角(度)
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 -- 克罗地亚里耶卡
time = {year=2016, month=2, day=17, hour=17, min=30} -- 当天结束
altitude, azimuth = sunposition(lat, long, time)
结果是:
- 高度为-0.1度
- 方位角为106度。
结果应该为:
- 高度为-0.1度
- 方位角为253度。
我在其他编程语言中找到了多种解决方案,甚至尝试在 Lua 中重新编写,但都没有成功。这个解决方案背后有太复杂的数学。
我正在使用它为我的 Corona SDK 应用程序显示太阳相对于设备的位置。目前唯一的解决方案是通过互联网的 PHP 或 Javascript 脚本进行 API 调用,但我真的想避免这种情况。
非常感谢社区提供的任何帮助。谢谢并爱您们! :)
点赞
用户405845
我已经找到了一种方法/技巧来解决这个问题。
函数仍然是一样的:
-- 太阳高度角,方位角(度)
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)
-- 修复方位角
if fixedAzimuthLast < azimuth then
fixedAzimuthLast = azimuth
fixedAzimuth = fixedAzimuthLast
else
fixedAzimuth = fixedAzimuthLast + (180 - azimuth)
end
-- 在目标时间找到方位角
if findTime.hour == i and findTime.min == j then
-- 最终结果
return altitude, fixedAzimuth
end
end
end
end
最终获得正确结果的方法:
lat, long = 45.327063, 14.442176
altitude, azimuth = getSunPos(lat, long, os.date('*t', os.time()))
就这样。 我更喜欢完整的函数可以正确进行数学计算,但这已足够。它已在全球三个地点进行了测试。
2016-02-26 17:25:22
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
Arccos(反余弦函数)的角度等于 360 减去该角度的余弦值。可以简单地返回360-angle。