Skip to content

Instantly share code, notes, and snippets.

@Akiyah
Last active October 26, 2019 16:36
Show Gist options
  • Save Akiyah/20e38429ca34f67f6d8b009dbff3369f to your computer and use it in GitHub Desktop.
Save Akiyah/20e38429ca34f67f6d8b009dbff3369f to your computer and use it in GitHub Desktop.
slime_equation_edit
# plus
"%+%" <- function(f, g) {
return(paste0("(", f, ") * (", g,")"))
}
# minus
m <- function(f) {
paste0( "-(", f, ")" )
}
# intersection
"%I%" <- function(f, g) {
paste0( "min(", f, ",", g, ")" )
}
# cut
"%C%" <- function(f, g) {
f %I% m(g)
}
mirror <- function(f) {
gsub("x", "abs(x)", f)
}
union <- function(...) {
paste0("MAX(", paste0(c(...), collapse=","), ")")
}
water <- function(f, g, depth) {
paste0(f %+% g, "-", depth)
}
superellipse <- function(ox,oy,rx,ry,p) {
xpart <- ifelse (ox == 0, "x", paste0("(x-",ox,")"))
ypart <- ifelse (oy == 0, "y", paste0("(y-",oy,")"))
paste0("(",xpart,"/",rx,")^", p, "+(",ypart,"/",ry,")^", p, "-1")
}
ellipse <- function(ox,oy,rx,ry) {
superellipse(ox,oy,rx,ry,2)
}
library(rgl)
draw <- function(f_str, name="") {
f_str <- gsub("MAX", "max", f_str)
f_str <- gsub("--", "+", f_str)
print(f_str)
f <- parse(text=f_str)
n <- 400
#n <- 200
xx <- seq(-150, 150, length = n)
yy <- seq(-150, 150, length = n)
zz <- outer(xx, yy, Vectorize(function(x, y) { eval(f) }))
#png(filename=paste0("png/graph_", name, ".png"), width=480, height=480)
contour(xx, yy, zz, drawlabels=FALSE, levels=0, lwd=1, main=f)
#dev.off()
#png(filename=paste0("png/graph_", name, ".png"), width=480, height=480)
#contour(xx, yy, zz, drawlabels=FALSE, levels=0, lwd=1)
#dev.off()
logz <- -log(-pmin(-zz,0)+1)
clear3d(type = "all")
par3d(windowRect=c(65, 52, 65+480, 52+480))
light3d(diffuse="gray90", specular="gray90")
bg3d("gray10")
surface3d(xx, yy, logz, color = "#FFFFFF")
#aspect3d(1,1,0.5)
aspect3d(1,1,1)
view3d(20, 10, zoom = 1)
rgl.snapshot(paste0("png/snapshot_", name, ".png"))
}
rm(list=ls())
setwd('~/Dropbox/tech/function_draw/')
for (i in 1:100) {
source('slime.r')
Sys.sleep(5)
}
source('function_dsl.r')
source('function_viewer.r')
horn <- ellipse( 0, 105, 8, 30)
face <- ellipse( 0, 0, 105, 75)
body <- water(horn, face, 1)
eye <- paste0("(", ellipse(27, 8, 15, 17), ")^2-1/3")
eyes <- mirror(eye)
mouse <- superellipse(0, -45, 67, 8, 4)
mouse <- gsub("y", "(y-(x/13)^2)", mouse)
slime <- (
body %+% eyes %+% mouse
)
draw(slime, "slime")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment