【问题标题】:Crossing number/Winding number polygon test expressed as binary (Python)以二进制表示的交叉数/绕组数多边形测试(Python)
【发布时间】:2015-06-21 14:24:38
【问题描述】:

我正在尝试实现绕组数或交叉数测试,主要围绕布尔运算构建。 布尔值的要求是由于底层数据集的方法和效率,使得将变量委托给除布尔值以外的计数是次优的。

交叉数似乎最容易实现(我认为),因为它本质上是二进制的(偶数 (0) 与奇数 (1)),其中每一侧的交叉数测试的结果可以是 xor-ed使用前面的结果,例如下面给出的代码,其中xyz 是我们评估的坐标。代码最后改编自http://geomalgorithms.com/a03-_inclusion.html

#Original points:
pts=[[100,100],[200,200],[300,100],[400,300],[300,400],[200,300],[100,100]]

#Extremas:
min=[pts[0][0],pts[0][1]]
max=[pts[0][0],pts[0][1]]

for i in pts:
  for j in range(2):
    if i[j]<min[j]:
      min[j]=i[j]
    if i[j]>max[j]:
      max[j]=i[j]

#New dimensions:
w=max[0]-min[0]
h=max[1]-min[1]
if len(sys.argv) > 2:
  xyz=[int(sys.argv[1]),int(sys.argv[2])]
else:
  xyz=[200,100]

#Normalize by cutting off lower than minimum, higher than maximum:
for i,p in enumerate(pts):
  pts[i]=[p[0]-min[0],p[1]-min[1]]

x=0
y=1
logic=None
counting=0
for i in range(len(pts)-1):
  test=(  ( (pts[i][y] <= xyz[y]) and (pts[i+1][y] > xyz[y]) ) or \
           ( (pts[i][y] > xyz[y]) and (pts[i+1][y] <= xyz[y]) ) ) and \
             (xyz[x] < pts[i][x] + ( (xyz[y]-pts[i][y])/(pts[i+1][y]-pts[i][y]) ) * (pts[i+1][x] - pts[i][x]))
  if logic is None:
    logic=test
  else:
    logic^=test
  if test:
    counting+=1
print logic
print counting

结果: 对于整个图像,二进制流会导致这些图像中的每个正方形都是一个步骤。 显然有些不对劲,但我似乎无法弄清楚为什么它在绕过右下角后变得混乱......有什么想法吗?

【问题讨论】:

    标签: python vector geometry point-in-polygon


    【解决方案1】:

    啊哈!

    &amp;!==and|!==or。通过更改操作符,它起作用了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-13
      • 2015-03-12
      • 1970-01-01
      • 2011-06-14
      • 2015-09-26
      • 2021-01-03
      • 2011-02-18
      • 1970-01-01
      相关资源
      最近更新 更多