1

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

交叉数似乎最容易实现(我认为),因为它本质上是二进制的(偶数(0)与奇数(1)),其中每一侧的交叉数测试的结果可以xor与之前的结果相结合,例如在下面给出的代码中,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

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

视觉结果

4

1 回答 1

0

Aha!

&!==and and |!==or. By changing the operators it worked.

an_image

于 2015-06-22T19:02:35.580 回答