gpt4 book ai didi

javascript - Coffeescript:for 循环中的条件变量未正确声明

转载 作者:行者123 更新时间:2023-12-03 06:59:31 25 4
gpt4 key购买 nike

我正在使用 CoffeeScript 来渲染绘图。我之前发布过一个关于设置条件的问题;我相信现在已经解决了。然而,我认为用于其中一个条件的变量 curr_visibility 导致了一个问题,因为它没有正确定义。图表基本上是这样工作的; 0(不可见)或 1(可见)被分配给图形上的每个点(这些点用于绘制一条线,该线本质上是来自使用 DEM 图像的 map 的地形轮廓)。我附上一张屏幕截图来说明我的错误(LV = lastVisibilty 和 CV = curr_visibility)。变量 curr_visibility 位于 for 循环内。我需要确保它在每次迭代后更新,但我只是不确定它是否设置正确以在我的 fillColor: if 条件语句中工作。代码以两个空集开始 - line = []datasets = [] Plot graph showing the bug. The area between LV and CV should be red for No visibility

prev_visibility = data[0].visibility
for elem, index in data
curr_visibility = elem.visibility

point = [
index
elem.geometry[2]
]

line.push point
unless prev_visibility is curr_visibility
datasets.push line
line = [point]
prev_visibility = curr_visibility

datasets.push line

line = []
lastVisibility = data[0].visibility

newfillColor = if lastVisibilty == 0 && curr_visibility == 0
"#C90E30"
else if lastVisibilty == 0 && curr_visibility == 1
"#439C32"
else if lastVisibilty == 1 && curr_visibility == 0
"#C90E30"
else
"#439C32"

for set in datasets
line.push
data: set,
lines:
show: true
fill: true
opacity: 0.7
fillColor: newfillColor

lastVisibility = 1 - lastVisibility

最佳答案

好的,在同事的帮助下,我解决了这个问题。首先,在上面的代码中,变量 prev_visibility 的每个实例都被删除。确定没有必要。其次,我们确定需要利用索引方法来关联一个新变量 next_visibility,它将在每次迭代中将一个点的当前可见性值与下一个点的可见性值进行比较(我希望我能正确解释这一点)。为此,我们添加了以下内容:

line.push point
if (index + 1) < data.length
next_visibility = data[index + 1].visibility
else
next_visibility = curr_visibility

unless next_visibility is curr_visibility
datasets.push line
line = [point]

最后,所有 newFillColor 内容都被删除,我恢复到 fillColor: if lastVisibility is 0 then "#C90E30"else "439C32"

关于javascript - Coffeescript:for 循环中的条件变量未正确声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37122354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com