Skip to content

Instantly share code, notes, and snippets.

@Steboss
Created March 26, 2018 20:29
Show Gist options
  • Save Steboss/7493c24a8a900e7904098c0ad824ae91 to your computer and use it in GitHub Desktop.
Save Steboss/7493c24a8a900e7904098c0ad824ae91 to your computer and use it in GitHub Desktop.
Points in a rectangle -- Python Function
def in_boxes(boxes, points):
# (N, 4, 2) = boxes.shape
# (M, 2) = points.shape
w = np.zeros(points.shape[0])
start = time.time()
for (i, point) in enumerate(points):
in_box = False
for box in boxes:
(A, B, C, D) = box
AP = (point - A)
AB = (B - A)
AD = (D - A)
cond0 = 0 < np.dot(AP, AB) < np.dot(AB, AB)
cond1 = 0 < np.dot(AP, AD) < np.dot(AD, AD)
in_box = in_box or (cond0 and cond1)
if in_box:
w[i] = 1
else:
w[i] = 0
end = time.time()
print("Total time %.4fs" %(end-start))
tot =np.sum(w)
print(tot)
return w
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment