Skip to content

Instantly share code, notes, and snippets.

@ozjimbob
Last active June 17, 2024 06:03
Show Gist options
  • Save ozjimbob/25b4442f2354fcb11d4e817d8e6be5c2 to your computer and use it in GitHub Desktop.
Save ozjimbob/25b4442f2354fcb11d4e817d8e6be5c2 to your computer and use it in GitHub Desktop.
library(nara)
library(grid)
x11(type = 'dbcairo', antialias = 'none', width = 8, height = 6)
dev.control('inhibit')
w<-400
h<-300
nr <- nr_new(w, h, 'black')
l1 <- c(floor(runif(1,0,w)),floor(runif(1,0,h)))
l2 <- c(floor(runif(1,0,w)),floor(runif(1,0,h)))
d1 <- c(runif(2,-2,2))
d2 <- c(runif(2,-2,2))
cvec<-c("red","orange","yellow","green","blue","purple")
rvec <- matrix(NA,nrow=100,ncol=4)
rvi <- 1
for(i in 1:100000){
if(i>98){
nr_line(nr,rvec[rvi,1],rvec[rvi,2],rvec[rvi,3],rvec[rvi,4],"black")
}
selcol <- cvec[(i %% length(cvec))+1]
nr_line(nr,l1[1],l1[2],l2[1],l2[2],selcol)
if(l1[1] > w | l1[1] <=0){d1[1]=-d1[1]}
if(l1[2] > h | l1[2] <=0){d1[2]=-d1[2]}
if(l2[1] > w | l2[1] <=0){d2[1]=-d2[1]}
if(l2[2] > h | l2[2] <=0){d2[2]=-d2[2]}
l1 <- l1 + d1
l2 <- l2 + d2
rvec[rvi,] <- c(l1[1],l1[2],l2[1],l2[2])
rvi <- rvi + 1
if(rvi == 100){
rvi <- 1
}
dev.hold()
grid.raster(nr, interpolate = FALSE)
dev.flush()
Sys.sleep(0.01)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment