Skip to content

Instantly share code, notes, and snippets.

@georgemsavva
Last active January 10, 2023 10:10
Show Gist options
  • Save georgemsavva/2bf3e42f034fba7e0d14fa66bf94cec6 to your computer and use it in GitHub Desktop.
Save georgemsavva/2bf3e42f034fba7e0d14fa66bf94cec6 to your computer and use it in GitHub Desktop.
Code to generate Bezier grids for genuary 2023
# Code to generate Bezier 'mesh' images inspired by the work of Jacquie Tran (https://art.jacquietran.com/)
# Function to calculate Bezier curves
bezierz <- function(z1,z2,ct,ct2,ct3,l,o=2){
ll=1-l
if(o==3) return(z1*ll^4 + 4*ct*ll^3*l + 6*ct2*ll^2*l^2 + 4*ct3*ll*l^3 + z2*l^4)
if(o==2) return(z1*ll^3 + 3*ct*ll^2*l + 3*ct2*ll*l^2 + z2*l^3)
if(o==1) return(z1*ll^2 + 2*ct*ll*l + z2*l^2)
}
# Make 1000 images
for(i in 1:1000){
N=sample(20:50,1) # number of lines
M=sample(100:500,1) # points per line
# start points
z1 <- seq(runif(1),runif(1),l=N) + 1i*seq(runif(1),runif(1),l=N)
# stop points
z2 <- seq(runif(1),runif(1),l=N) + 1i*seq(runif(1),runif(1),l=N)
# control points
ct1 <- seq(runif(1),runif(1),l=N) + 1i*seq(runif(1),runif(1),l=N)
ct2 <- seq(runif(1),runif(1),l=N) + 1i*seq(runif(1),runif(1),l=N)
ct3 <- seq(runif(1),runif(1),l=N) + 1i*seq(runif(1),runif(1),l=N)
# how many control points to use
o=sample(1:3,1)
# now calculate the curves
b <- sapply(seq(0,1,l=M), function(l) bezierz(z1,z2,ct1,ct2,ct3,l,o ) )
# choose some random colours for points and lines
col1 = hsv(runif(1),runif(N),1,runif(1))
col2 = hsv(runif(1),runif(N*M,0,0.2),1,1)
# plot!
png(filename=sprintf("mesh%04d.png",i),width=1000,height=1000,type="cairo")
par(mar=5*c(1,1,1,1), bg="#101010")
plot(NA,xlim=range(Re(b)), ylim=range(Im(b)), asp=1,type="p",axes=F,ann=F)
points(b,col=col2,pch=20,cex=.5)
apply(b,1,lines,col=col1)
apply(b,2,lines,col=col1)
dev.off()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment