How to check if a point is inside a square using bivrp::is_point_inside

103 views Asked by At

I am trying bivrp::is_point_inside to check if a point is inside a square, but no matter what I try, I always get FALSE, even in the most obvious cases... What am I doing wrong??

My code:

polyg=data.frame(c(15, 15, 0, 0), c(15, 0, 0, 15))
point=c(5,5)
bivrp::is_point_inside(point, polyg)

I think we would all agree that a point at 5,5 would be inside a square with coordinates 15,15, 15,0, 0,0, 0,15, however:

> bivrp::is_point_inside(point, polyg)
[1] FALSE

Is there anything I'm doing wrong here?

Note I prioritise the use of bivrp::is_point_inside; I have some older code where I used it and never had any problem, if it doesn't work properly anymore, I would have to revise the older code.

EDIT

This function clearly does not work... Can someone suggest a function that does the same (check if a point is inside a polygon) but works? I will have to revise my old code using it...

1

There are 1 answers

1
TCamara On

Good news. I just tested your example with secr and it seems to work just fine.

Here's the sample code:

library(secr)
polyg <- data.frame(x = c(0, 15, 15, 0),
                    y = c(0, 0, 15, 15))

# Here is your point
point_inside <- matrix(c(5, 5), ncol = 2)
secr::pointsInPolygon(point_inside, polyg)

# Here is an arbitrary point outside
point_outside <- matrix(c(16, 5), ncol = 2)
secr::pointsInPolygon(point_outside, polyg)

Hope this helps.