# server.r

Feb 2nd, 2021
740
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. library(shiny)
2. library(shinyBS)
3. library(shape)
4.
5. # Triunghi
6. tri.gen <- function(wt) {
7.   weight <- wt
8.   len <- 50000
9.
10.   # matrice loc_index care contine toate endpointurile
11.   loc_index <- matrix(NA, ncol = 3, nrow = 3)
12.
13.   loc_index[1, ] <- c(1, 0, 0)
14.   loc_index[2, ] <- c(2, 0.5, sqrt(3) / 2)
15.   loc_index[3, ] <- c(3, 1, 0)
16.
17.   # lista cu toate punctele de intalnire
18.   vertices <- runif(len)
19.   vertices[which(vertices > 2 / 3)] <- 3
20.   vertices[which(1 / 3 < vertices & vertices <= 2 / 3)] <- 2
21.   vertices[which(vertices <= 1 / 3)] <- 1
22.
23.   coords <- matrix(NA, ncol = 2, nrow = (len + 1))
24.   colnames(coords) <- c("x", "y") # ggvis
25.
26.   coords[1, ] <- c(runif(1), runif(1) * sqrt(3) / 2)
27.
28.   for (i in 1:len) {
29.     row <- i + 1
30.     spot <- which(loc_index[, 1] == vertices[i])
31.     x <- loc_index[spot, 2]
32.     y <- loc_index[spot, 3]
33.     x.new <- weight * x + (1 - weight) * coords[i, 1]
34.     y.new <- weight * y + (1 - weight) * coords[i, 2]
35.     coords[row, ] <- c(x.new, y.new)
36.     x <- x.new
37.     y <- y.new
38.   }
39.   return(list(loc_index, vertices, coords))
40. }
41.
42. # Pentagon
43. pent.gen <- function(wt) {
44.   weight <- wt
45.   len <- 50000
46.   loc_index <- matrix(NA, ncol = 3, nrow = 5)
47.
48.   c1 <- 0.25 * (sqrt(5) - 1)
49.   c2 <- 0.25 * (sqrt(5) + 1)
50.   s1 <- 0.25 * (sqrt(10 + 2 * sqrt(5)))
51.   s2 <- 0.25 * (sqrt(10 - 2 * sqrt(5)))
52.
53.   loc_index[1, ] <- c(1, 0, 1)
54.   loc_index[2, ] <- c(2, s1, c1)
55.   loc_index[3, ] <- c(3, s2, -c2)
56.   loc_index[4, ] <- c(4, -s2, -c2)
57.   loc_index[5, ] <- c(5, -s1, c1)
58.
59.   vertices <- runif(len)
60.   vertices[which(vertices > 4 / 5)] <- 5
61.   vertices[which(3 / 5 < vertices & vertices <= 4 / 5)] <- 4
62.   vertices[which(2 / 5 < vertices & vertices <= 3 / 5)] <- 3
63.   vertices[which(1 / 5 < vertices & vertices <= 2 / 5)] <- 2
64.   vertices[which(vertices <= 1 / 5)] <- 1
65.
66.   coords <- matrix(NA, ncol = 2, nrow = (len + 1))
67.   colnames(coords) <- c("x", "y") # ggvis
68.
69.   # coordonatele alese random
70.   coords[1, ] <- c(runif(1, -s1, s1), runif(1, -c2, 1))
71.
72.   for (i in 1:len) {
73.     row <- i + 1
74.     spot <- which(loc_index[, 1] == vertices[i])
75.     x <- loc_index[spot, 2]
76.     y <- loc_index[spot, 3]
77.     x.new <- (weight) * x + (1 - weight) * coords[i, 1]
78.     y.new <- (weight) * y + (1 - weight) * coords[i, 2]
79.     coords[row, ] <- c(x.new, y.new)
80.   }
81.   return(list(loc_index, vertices, coords))
82. }
83.
84. shinyServer(function(input, output, session) {
85.   output\$my.pts <- renderUI({
86.     input\$shape
87.     sliderInput("pts", "Numar de puncte:", min = 1000, max = 50000, step = 1000, value = 1000, animate = animationOptions(interval = 200))
88.   })
89.
90.   updateButton(session, "gen", style = "primary", size = "default", disabled = FALSE)
91.
92.   all.list <- reactive({
93.     if (input\$shape == "tri") {
94.       return(tri.gen(input\$dist.tri * (input\$gen > -1)))
95.     }
96.     if (input\$shape == "pent") {
97.       return(pent.gen(input\$dist.pent * (input\$gen > -1)))
98.     }
99.   })
100.
101.   output\$compPlot <- renderPlot({
102.     loc_index <- all.list()[[1]]
103.     vertices  <- all.list()[[2]]
104.     coords    <- all.list()[[3]]
105.
106.     # Triunghi
107.     if (input\$shape == "tri") {
108.       par(mar = c(0.5, 0.5, 0.5, 0.5))
109.       plot(0, 0, xlim = c(0, 1), ylim = c(0, sqrt(3) / 2), col = 0, yaxt = "n", xaxt = "n", xlab = "", ylab = "", bty = "n")
110.     }
111.
112.     # Pentagon
113.     if (input\$shape == "pent") {
114.       c1 <- 0.25 * (sqrt(5) - 1)
115.       c2 <- 0.25 * (sqrt(5) + 1)
116.       s1 <- 0.25 * (sqrt(10 + 2 * sqrt(5)))
117.       s2 <- 0.25 * (sqrt(10 - 2 * sqrt(5)))
118.
119.       par(mar = c(0.5, 0.5, 0.5, 0.5))
120.       plot(0, 0, xlim = c(-s1, s1), ylim = c(-c2, 1), col = 0, yaxt = "n", xaxt = "n", xlab = "", ylab = "", bty = "n")
121.     }
122.
123.     if (!is.null(input\$pts)) {
124.       if (input\$pts != 0) {
125.         points(coords[1:input\$pts, 1], coords[1:input\$pts, 2], pch = ".", cex = 2.5, col = "blue")
126.       }
127.     }
128.     points(loc_index[, 2], loc_index[, 3], pch = 20, cex = 2, col = "red")
129.   })
130. })
RAW Paste Data

# Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×