Für Vorlesungen, bitte die Webseite verwenden. https://flavigny.de/lecture
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

59 řádky
1.6KB

  1. # Josua Kugler, Christian Merten
  2. library(R6)
  3. compass <- c("N", "E", "S", "W")
  4. movement <- list(N = c(0, 1), S = c(0, -1), W = c(-1, 0), E = c(1, 0))
  5. # helper because R indexing beginning at 1 is stupid
  6. get_dir <- function(k) {
  7. compass[(k-1) %% 4 + 1]
  8. }
  9. arrow_coords <- function(n, coord) {
  10. (coord + 0.5) / n
  11. }
  12. GridPath <- R6Class("GridPath", list(
  13. path = matrix(c(0,0), nrow=1),
  14. dir = character(0),
  15. initialize = function(dir) {
  16. self$dir <- dir
  17. },
  18. move = function(steps) {
  19. if (steps == 0) return(invisible(self))
  20. n <- nrow(self$path)
  21. self$path <- rbind(self$path,
  22. self$path[n,] + movement[[self$dir]])
  23. self$move(steps-1)
  24. },
  25. rotate_right = function() {
  26. self$dir <- get_dir(which(self$dir == compass) + 1)
  27. invisible(self)},
  28. rotate_left = function() {
  29. self$dir <- get_dir(which(self$dir == compass) - 1)
  30. invisible(self)
  31. },
  32. print = function() {
  33. n <- max(self$path)+1
  34. grid <- matrix(rep(F, n^2), nrow=n)
  35. apply(self$path, 1, function(r) grid[r[1]+1, r[2]+1] <<- T)
  36. image(grid, useRaster=T, asp=T, col=c("white", "black"),
  37. axes=F)
  38. final <- self$path[nrow(self$path),]
  39. print(final)
  40. mv <- movement[[self$dir]]
  41. coords <- c((final)/(n-1),
  42. (final + mv) / (n-1))
  43. arrows(coords[1], coords[2], coords[3], coords[4], col="red")
  44. }))
  45. gpath3 <- GridPath$new("N")
  46. gpath3$move(7)$
  47. rotate_right()$move(8)$
  48. rotate_right()$move(7)$
  49. rotate_right()$move(6)$
  50. rotate_right()$move(5)
  51. gpath3