我有一些使用 minmax 算法定位鸟类的简单代码。一切正常,但我发现我的编程不好,我相信有更好的解决方案。我在 RoR 方面没有那么有经验,但如果有人知道更好的方法来实现相同的解决方案,那么我很高兴 ;)。
我讨厌两个部分,我必须创建 4 个列表来确定不同组合的最大值或最小值(最小值-最大值算法的核心)和非常丑陋的 SQL hack。
谢谢!
def index
# fetch all our birds
@birds = Bird.all
# Loop over the birds
@birds.each do |bird|
@fixed = Node.where("d7type = 'f'")
xminmax = []
xmaxmin = []
yminmax = []
ymaxmin = []
@fixed.each do |fixed|
rss = Log.find_by_sql("SELECT logs.fixed_mac, AVG(logs.blinker_rss) AS avg_rss FROM logs
WHERE logs.blinker_mac = '#{bird.d7_mac}' AND logs.fixed_mac = '#{fixed.d7_mac}' ORDER BY logs.id DESC LIMIT 30")
converted_rss = calculate_distance_rss(rss[0].attributes["avg_rss"])
xminmax.push(fixed.xpos + converted_rss)
xmaxmin.push(fixed.xpos - converted_rss)
yminmax.push(fixed.ypos + converted_rss)
ymaxmin.push(fixed.ypos - converted_rss)
end
pos = {x: (xminmax.min + xmaxmin.max) / 2, y: (yminmax.min + ymaxmin.max) / 2}
puts pos
end
结尾