As in the question I have several points, lets call them A, B, and C (but they can be more) of which I know the position in (x, y). I also have a defined area ranging in between [1-n] on both axis. I need to find all the points that fall in the polygon that is generated by A, B, and C.
Since I don't have a set of points, I thought to use the whole range for both x and y axes, i.e. [1-n], as the set of possible points falling in the polygon generated by A, B, and C but I am not sure whether this could be what the function was designed for.
At this stage, I tried something involving the following function (of which I found other questions here in SO).
#define the range as the possible points falling in the polygon
allPointsX <- allPointsY <- c(1:2048)
#get some coordinates for three points generating the actual polygon (which in this case is a simple triangle)
xCoord <- c(127, 120, 152)
yCoord <- c(77, 96, 107)
#look for points into the polygon
points <- point.in.polygon(allPointsX, allPointsY, xCoord, yCoord)
but either I am not getting the output (which is all zeros: all(points==0)
), or this is not what I am looking for.
Any suggestions? What am I missing?
Your code is correct. It is just that all of your points lie outside the triangle.
library(tidyverse)
ggplot(mapping = aes(x, y)) +
geom_point(data = tibble(x = allPointsX, y = allPointsY)) +
geom_polygon(data = tibble(x = xCoord, y = yCoord)) +
xlim(75, 160) +
ylim(75, 160)
Here is a point that lies inside the triange.
sp::point.in.polygon(127, 78, xCoord, yCoord)
#> [1] 1