wolph42 wrote: ↑Thu Aug 27, 2020 5:37 am
you made a mistake here:
Code: Select all
[h: firstProduct = firstProduct + (number(listGet(xList, roll.count)) * number(listGet(yList, roll.count+1)))]
That's actually correct... but it put me on the right path. The typo was in the secondProduct which should be:
Code: Select all
[h: secondProduct = secondProduct + (number(listGet(yList, roll.count)) * number(listGet(xList, roll.count+1)))]
And I also left off dividing by two in the final step with the version I shared. Should be:
Code: Select all
[h: polygonArea = absolutevalue(firstProduct - secondProduct)/2]
Now it looks to be working! Or, at least, polyline data looks to be much more accurate instead of the crazy numbers it was spitting before.
New working PoC code:
Code: Select all
[h: res = input(
"xCord|0|X|",
"yCord|0|Y|"
)]
[h: abort(res)]
[h: gridSize = json.get(json.get(getInfo("map"),"grid"),"size")]
[h: rectangle = json.set("{'shape':'rectangle'}", "x", xCord*gridSize, "y", yCord*gridSize, "w", gridSize,"h", gridSize, "fill",1)]
[h: vblData = getVBL(rectangle , 0)]
[h: gridArea = gridSize*gridSize]
[h: strProp = json.toList(vblData)]
[h: pointsJson = json.get(strProp, "points")]
[h: hasVBL = if(json.isEmpty(pointsJson) == 1, 0, 1)]
[h: assert(hasVBL, "There is no VBL in grid ("+xCord+","+yCord+").")]
[h: xList = ""]
[h: yList = ""]
[h, FOREACH(point, pointsJson, ""), CODE:
{
[h: x = json.get(point, "x")]
[h: x = x - (xCord*100)]
[h: xList = listAppend(xList, x)]
[h: y = json.get(point, "y")]
[h: y = y - (yCord*100)]
[h: yList = listAppend(yList, y)]
}
]
[h: firstProduct = 0]
[h: cordCount = listCount(xList)]
[h: productCount = cordCount - 1]
[h, COUNT(productCount), CODE:
{
[h: firstProduct = firstProduct + (number(listGet(xList, roll.count)) * number(listGet(yList, roll.count+1)))]
}
]
[h: secondProduct = 0]
[h, COUNT(productCount), CODE:
{
[h: secondProduct = secondProduct + (number(listGet(yList, roll.count)) * number(listGet(xList, roll.count+1)))]
}
]
[h: polygonArea = absolutevalue(firstProduct - secondProduct)/2]
[h: percentageOfGrid = divide(polygonArea, gridArea) * 100]
[h: broadcast("VBL at grid ("+xCord+","+yCord+") is "+percentageOfGrid+"% of the grid.")]
Now off to build some functions...
Thanks for the help.
*Note: It doesn't work well for grids that have non-contiguous VBL objects in them. That's not likely to be a problem for me given the way I apply VBL and it could (likely) be circumvented by looking for shapes that "close" and then loop through other possible shapes in the data. Probably best to make a separate function for breaking the VBL data into multiple shapes (if they exist). But the payoff for such an unlikely corner case for me might not be there.
Note2: After just looking at multiple VBL object grids, it looks like the first coordinate pair is used as a kind of "break" between additional shapes. So getting those shapes might not be that hard after all...