如何计算闪亮或R代码中两个圆的交叉区域

时间:2017-06-08 13:58:02

标签: r statistics shiny

任何人都有关于如何计算两个圆的交叉区域的闪亮代码或R代码?

UI

库(有光泽)

定义绘制直方图的应用程序的UI

shinyUI(fluidPage(

#应用程序标题   titlePanel(“选择你的概率”),

#Sidebar,带有用于箱数的滑块输入   sidebarLayout(     sidebarPanel(

  sliderInput("radius",
              "Probability of A",
              min = 0,
              max = 0.4,
              value = 0.2),
  sliderInput("radius2",
              "Probability of B",
              min = 0,
              max = 0.4,
              value = 0.2)
),
mainPanel(
  plotOutput("distPlot")
)

) ))

服务器

库(有光泽) 库(plotrix) 库(网格)

定义绘制直方图所需的服务器逻辑

shinyServer(函数(输入,输出){

输出$ distPlot< - renderPlot({

isolate({
  plot(c(-1,1),c(-1,1), type = 'n')


})

draw.circle(-0.25,0,input$radius)
draw.circle(0.25,0,input$radius2)

})

})

1 个答案:

答案 0 :(得分:4)

你可以用这个:

circle_intersection <- function(x1, y1, r1, x2, y2, r2){
  rr1 <- r1 * r1
  rr2 <- r2 * r2
  d <- sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))

  if (d > r2 + r1) # Circles do not overlap
  {
    return(0)
  } else if (d <= abs(r1 - r2) && r1 >= r2){ # Circle2 is completely inside circle1  
    return(pi*rr2)
  } else if (d <= abs(r1 - r2) && r1 < r2){ # Circle1 is completely inside circle2
    return(pi*rr1)
  } else { # Circles partially overlap
    phi <- (acos((rr1 + (d * d) - rr2) / (2 * r1 * d))) * 2
    theta <- (acos((rr2 + (d * d) - rr1) / (2 * r2 * d))) * 2
    area2 <- 0.5 * theta * rr2 - 0.5 * rr2 * sin(theta)
    area1 <- 0.5 * phi * rr1 - 0.5 * rr1 * sin(phi)
    return(area1 + area1)
  }
}

circle_intersection(-0.25,0,0.2,0.25,0,0.2)  # 0 (Circles do not overlap)
circle_intersection(-0.25,0,0.2,-0.25,0,0.1) # 0.031 (Circle2 is completely inside circle1)
circle_intersection(-0.25,0,0.1,-0.25,0,0.2) # 0.031 (Circle1 is completely inside circle2)
circle_intersection(-0.25,0,0.3,0.25,0,0.4)  # 0.08051314 (Circles partially overlap)