Advertisement
hegemon88676

First batch

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