簡體   English   中英

Python:檢查一組 2D 點是否包含至少 2 條水平線

[英]Python: Check if set of 2D points contain at least 2 horizontal lines

給定的是一個二維 (x,y) 點(整數)的 numpy 數組。 例如:

points = np.array([[3,3],[5,4],[1,6],[10,6],[100,100]])

我想檢查:

數據是否至少構建了兩條水平線? 水平線被定義為 MAXIMUM y-difference 為 1 和 MINIMUM x-difference 為 1 的一組 2D 點。兩條水平線必須具有不同的 y 坐標。

輸出應該是一個布爾值:如果集合包含兩條水平線,則為 True,否則為 False。 上面提到的數組應該輸出 True。 一條線由 (3,3) 和 (5,4) 形成,另一條水平線由 (1,6) 和 (10,6) 形成。

謝謝你的幫助

這不是最好的比較方式! (也許這是最糟糕的 - 將每個點與每個點進行比較 (O = n*n) )您應該檢查其他算法。

=> 演示

import numpy as np

points = np.array([[3,3],[5,4],[1,6],[10,6],[100,100]])

def checkIfLineInArray(array):
  result = 0
  for p1 in points:
      for p2 in points:
          if(p1[0]==p2[0] and p1[1]==p2[1]):
              #same point we want skip
              continue
          
          if(((p2[1]-1) < p1[1] < (p2[1]+1))):
              print('there is horizontal line between this points')
              print(p1,p2)
              result = 1
  return result

checkIfLineInArray(points)

根據需要對此功能進行評論或刪除打印:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM