PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, October 8, 2022

[FIXED] how to calculate the intersection area of two circles in shiny or R code

 October 08, 2022     r, shiny, statistics     No comments   

Issue

Anyone has shiny code or R code about how to calculate the intersection area of two circles?

ui

library(shiny)

Define UI for application that draws a histogram

shinyUI(fluidPage(

# Application title titlePanel("Choose your probability"),

# Sidebar with a slider input for number of bins 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")
)

) ))

server

library(shiny) library(plotrix) library(grid)

Define server logic required to draw a histogram

shinyServer(function(input, output) {

output$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)

})

})


Solution

you can use this:

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 + area2)
  }
}

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.06641675 (Circles partially overlap)


Answered By - moodymudskipper
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing