Learn R Programming

SearchTrees (version 0.1.6)

getPointsInRect: Perform Rectangular Range Lookup on a Tree Object

Description

Determine which two dimensional points (which have already been stored in a search tree) fall within a specified rectangle.

Usage

getPointsInRect(tree, ptOne, ptTwo, data, columns)

Arguments

tree
S4 object representing the tree to be searched over.
ptOne
Numeric vector of length 2 indicating x and y coordinates of first point defining a rectangle.
ptTwo
Numeric vector of length 2 indicating x and y coordinates of second point defining a rectangle. Must be non-colinear with ptOne in both the x and y directions.
data
Data used to create the tree.
columns
Columns in the data object which contain the relevant data.

Value

  • An integer vector is returned containing the indices (row numbers) of points which fall within the specified rectangle.

See Also

findKNN,createIndex

Examples

Run this code
x = rnorm(100)
y = rnorm(100)
dat = cbind(x,y)
tree = createIndex(dat) #default tree type is "quad"
#our rectangle has corners at (0,0), (0,2), (2, 2), and (2,0)
inds = getPointsInRect(tree, c(0, 0), c(2, 2), dat)

#prepare vector of plotting characters
ch = rep(1, times = 100)
ch[inds] = 3

#plot points indicating which ones were selected as in our rectangle
plot(x,y, pch = ch)
#add rectangle to plot
xpts = c(0, 0, 2, 2, 0)
ypts = c(0, 2, 2, 0, 0)
lines(xpts, ypts)

Run the code above in your browser using DataLab