python - numpy filter points within bounding box -


i have list of 2d points [(x1,y1),(x2,y2),...,(xn,yn)] in numpy, how list of points within bounding box specified corners ((bx1,by1),(bx2,by2))?

if c++ use ogc "within" specification in boost geometry filter list.

right i'm dealing list of indexes nxn 2d numpy array, expect should 1-2 lines of code numpy.

using combination of all, logical_and , <= operator, 1 can express main idea in 1 line.

import random import numpy np matplotlib import pyplot  points = [(random.random(), random.random()) in range(100)]  bx1, bx2 = sorted([random.random(), random.random()]) by1, by2 = sorted([random.random(), random.random()])  pts = np.array(points) ll = np.array([bx1, by1])  # lower-left ur = np.array([bx2, by2])  # upper-right  inidx = np.all(np.logical_and(ll <= pts, pts <= ur), axis=1) inbox = pts[inidx] outbox = pts[np.logical_not(inidx)]  # drawing rect = np.array([[bx1, by1], [bx1, by2], [bx2, by2], [bx2, by1], [bx1, by1]])  pyplot.plot(inbox[:, 0], inbox[:, 1], 'rx',             outbox[:, 0], outbox[:, 1], 'bo',             rect[:, 0], rect[:, 1], 'g-') pyplot.show() 

illustration of bounding box result


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -