Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
44
Task/Ray-casting-algorithm/R/ray-casting-algorithm-1.r
Normal file
44
Task/Ray-casting-algorithm/R/ray-casting-algorithm-1.r
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
point_in_polygon <- function(polygon, p) {
|
||||
count <- 0
|
||||
for(side in polygon) {
|
||||
if ( ray_intersect_segment(p, side) ) {
|
||||
count <- count + 1
|
||||
}
|
||||
}
|
||||
if ( count %% 2 == 1 )
|
||||
"INSIDE"
|
||||
else
|
||||
"OUTSIDE"
|
||||
}
|
||||
|
||||
ray_intersect_segment <- function(p, side) {
|
||||
eps <- 0.0001
|
||||
a <- side$A
|
||||
b <- side$B
|
||||
if ( a$y > b$y ) {
|
||||
a <- side$B
|
||||
b <- side$A
|
||||
}
|
||||
if ( (p$y == a$y) || (p$y == b$y) ) {
|
||||
p$y <- p$y + eps
|
||||
}
|
||||
if ( (p$y < a$y) || (p$y > b$y) )
|
||||
return(FALSE)
|
||||
else if ( p$x > max(a$x, b$x) )
|
||||
return(FALSE)
|
||||
else {
|
||||
if ( p$x < min(a$x, b$x) )
|
||||
return(TRUE)
|
||||
else {
|
||||
if ( a$x != b$x )
|
||||
m_red <- (b$y - a$y) / (b$x - a$x)
|
||||
else
|
||||
m_red <- Inf
|
||||
if ( a$x != p$x )
|
||||
m_blue <- (p$y - a$y) / (p$x - a$x)
|
||||
else
|
||||
m_blue <- Inf
|
||||
return( m_blue >= m_red )
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue