hegemon88676

2nd batch

Feb 2nd, 2021 (edited)
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 6.19 KB | None | 0 0
  1. library(shiny)
  2. library(shinyBS)
  3. library(shape)
  4.  
  5.  
  6. ui <- shinyUI(fluidPage(
  7.   tags$title("Proiect PS: Chaos Game"),
  8.   tags$h3("Proiect PS: Chaos Game"),
  9.   sidebarLayout(
  10.     sidebarPanel (
  11.       selectizeInput(
  12.         'shape',
  13.         h5(tags$b('Shape')),
  14.         choices = list("Forme" = c(
  15.           `Triunghi` = 'tri', `Pentagon` = 'pent'
  16.         )),
  17.         selected = 'tri'
  18.       ),
  19.       conditionalPanel(
  20.         condition = "input.shape=='tri'",
  21.         sliderInput(
  22.           "dist.tri",
  23.           label = h5(tags$b("Ratie Distanta:")),
  24.           min = 0.01,
  25.           max = .99,
  26.           value = .50,
  27.           step = .01
  28.         ),
  29.         div("Ratia default este: 0.50",
  30.             style = "font-size: 9.5pt;color:teal", align = "left")
  31.       ),
  32.      
  33.       conditionalPanel(
  34.         condition = "input.shape=='pent'",
  35.         sliderInput(
  36.           "dist.pent",
  37.           label = h5(tags$b("Ratie Distanta:")),
  38.           min = 0.01,
  39.           max = .99,
  40.           value = .63,
  41.           step = .01
  42.         ),
  43.         div("Ratia default 0.63", style = "font-size: 9.5pt;color:teal", align =
  44.               "left")
  45.       ),
  46.       br(),
  47.       div(uiOutput("my.pts")),
  48.       br(),
  49.       div(bsButton("generate", label = "Randomize"), align = "right")
  50.     ),
  51.     mainPanel("Vizualizare", value = 3,
  52.               fluidRow(
  53.                   div(div(
  54.                     plotOutput("compPlot")
  55.                   ),
  56.                   align = "center"),
  57.                   HTML(
  58.                     "<hr style='height: 2px; color: #BDBDBD; background-color: #D9D9D9; border: none;'>"
  59.                   )
  60.               ))
  61.   )
  62. ))
  63.  
  64. # ------------------------------------------------------------------------------------------------------------
  65.  
  66. # Triunghi
  67. tri.generate <- function(wt) {
  68.   weight <- wt
  69.   len <- 50000
  70.  
  71.   # matrice loc_index care contine toate endpointurile
  72.   loc_index <- matrix(NA, ncol = 3, nrow = 3)
  73.  
  74.   loc_index[1, ] <- c(1, 0, 0)
  75.   loc_index[2, ] <- c(2, 0.5, sqrt(3) / 2)
  76.   loc_index[3, ] <- c(3, 1, 0)
  77.  
  78.   # lista cu toate punctele de intalnire
  79.   vertices <- runif(len)
  80.   vertices[which(vertices > 2 / 3)] <- 3
  81.   vertices[which(1 / 3 < vertices & vertices <= 2 / 3)] <- 2
  82.   vertices[which(vertices <= 1 / 3)] <- 1
  83.  
  84.   coords <- matrix(NA, ncol = 2, nrow = (len + 1))
  85.   colnames(coords) <- c("x", "y") # ggvis
  86.  
  87.   coords[1, ] <- c(runif(1), runif(1) * sqrt(3) / 2)
  88.  
  89.   for (i in 1:len) {
  90.     row <- i + 1
  91.     spot <- which(loc_index[, 1] == vertices[i])
  92.     x <- loc_index[spot, 2]
  93.     y <- loc_index[spot, 3]
  94.     x.new <- weight * x + (1 - weight) * coords[i, 1]
  95.     y.new <- weight * y + (1 - weight) * coords[i, 2]
  96.     coords[row, ] <- c(x.new, y.new)
  97.     x <- x.new
  98.     y <- y.new
  99.   }
  100.   return(list(loc_index, vertices, coords))
  101. }
  102.  
  103. # Pentagon
  104. pent.generate <- function(wt) {
  105.   weight <- wt
  106.   len <- 50000
  107.   loc_index <- matrix(NA, ncol = 3, nrow = 5)
  108.  
  109.   c1 <- 0.25 * (sqrt(5) - 1)
  110.   c2 <- 0.25 * (sqrt(5) + 1)
  111.   s1 <- 0.25 * (sqrt(10 + 2 * sqrt(5)))
  112.   s2 <- 0.25 * (sqrt(10 - 2 * sqrt(5)))
  113.  
  114.   loc_index[1, ] <- c(1, 0, 1)
  115.   loc_index[2, ] <- c(2, s1, c1)
  116.   loc_index[3, ] <- c(3, s2, -c2)
  117.   loc_index[4, ] <- c(4, -s2, -c2)
  118.   loc_index[5, ] <- c(5, -s1, c1)
  119.  
  120.   vertices <- runif(len)
  121.   vertices[which(vertices > 4 / 5)] <- 5
  122.   vertices[which(3 / 5 < vertices & vertices <= 4 / 5)] <- 4
  123.   vertices[which(2 / 5 < vertices & vertices <= 3 / 5)] <- 3
  124.   vertices[which(1 / 5 < vertices & vertices <= 2 / 5)] <- 2
  125.   vertices[which(vertices <= 1 / 5)] <- 1
  126.  
  127.   coords <- matrix(NA, ncol = 2, nrow = (len + 1))
  128.   colnames(coords) <- c("x", "y") # ggvis
  129.  
  130.   # coordonatele alese random
  131.   coords[1, ] <- c(runif(1, -s1, s1), runif(1, -c2, 1))
  132.  
  133.   for (i in 1:len) {
  134.     row <- i + 1
  135.     spot <- which(loc_index[, 1] == vertices[i])
  136.     x <- loc_index[spot, 2]
  137.     y <- loc_index[spot, 3]
  138.     x.new <- (weight) * x + (1 - weight) * coords[i, 1]
  139.     y.new <- (weight) * y + (1 - weight) * coords[i, 2]
  140.     coords[row, ] <- c(x.new, y.new)
  141.   }
  142.   return(list(loc_index, vertices, coords))
  143. }
  144.  
  145.  
  146.  
  147. # ------------------------------------------------------------------------------------------------------------
  148.  
  149. server <- shinyServer(function(input, output, session) {
  150.   output$my.pts <- renderUI({
  151.     input$shape
  152.     sliderInput(
  153.       "pts",
  154.       "Numar de puncte:",
  155.       min = 1000,
  156.       max = 50000,
  157.       step = 1000,
  158.       value = 1000,
  159.       animate = animationOptions(interval = 200)
  160.     )
  161.   })
  162.  
  163.   updateButton(
  164.     session,
  165.     "generate",
  166.     style = "primary",
  167.     size = "default",
  168.     disabled = FALSE
  169.   )
  170.  
  171.   all.list <- reactive({
  172.     if (input$shape == "tri") {
  173.       return(tri.generate(input$dist.tri * (input$generate > -1)))
  174.     }
  175.     if (input$shape == "pent") {
  176.       return(pent.generate(input$dist.pent * (input$generate > -1)))
  177.     }
  178.   })
  179.  
  180.   output$compPlot <- renderPlot({
  181.     loc_index <- all.list()[[1]]
  182.     vertices  <- all.list()[[2]]
  183.     coords    <- all.list()[[3]]
  184.    
  185.     # Triunghi
  186.     if (input$shape == "tri") {
  187.       par(mar = c(0.5, 0.5, 0.5, 0.5))
  188.       plot(
  189.         0,
  190.         0,
  191.         xlim = c(0, 1),
  192.         ylim = c(0, sqrt(3) / 2),
  193.         col = 0,
  194.         yaxt = "n",
  195.         xaxt = "n",
  196.         xlab = "",
  197.         ylab = "",
  198.         bty = "n"
  199.       )
  200.     }
  201.    
  202.     # Pentagon
  203.     if (input$shape == "pent") {
  204.       c1 <- 0.25 * (sqrt(5) - 1)
  205.       c2 <- 0.25 * (sqrt(5) + 1)
  206.       s1 <- 0.25 * (sqrt(10 + 2 * sqrt(5)))
  207.       s2 <- 0.25 * (sqrt(10 - 2 * sqrt(5)))
  208.      
  209.       par(mar = c(0.5, 0.5, 0.5, 0.5))
  210.       plot(
  211.         0,
  212.         0,
  213.         xlim = c(-s1, s1),
  214.         ylim = c(-c2, 1),
  215.         col = 0,
  216.         yaxt = "n",
  217.         xaxt = "n",
  218.         xlab = "",
  219.         ylab = "",
  220.         bty = "n"
  221.       )
  222.     }
  223.    
  224.     if (!is.null(input$pts)) {
  225.       if (input$pts != 0) {
  226.         points(coords[1:input$pts, 1],
  227.                coords[1:input$pts, 2],
  228.                pch = ".",
  229.                cex = 2.5,
  230.                col = "blue")
  231.       }
  232.     }
  233.     points(
  234.       loc_index[, 2],
  235.       loc_index[, 3],
  236.       pch = 20,
  237.       cex = 2,
  238.       col = "red"
  239.     )
  240.   })
  241. })
  242.  
  243. shinyApp(ui = ui, server = server)
  244.  
Add Comment
Please, Sign In to add comment