Equation-driven smoothly shaded concentric shapes

时光总嘲笑我的痴心妄想 提交于 2019-11-30 03:12:10

Try this in R:

# create palette
greyScale <- colorRampPalette(c("black","white"))

# function to draw shape
plotHeart <- function(r, col){
  t <- seq(0,2*pi,length.out=100)
  x <- r*sin(t)^3
  y <- (13*r/16)*cos(t) - (5*r/16)*cos(2*t) - (2*r/16)*cos(3*t) - (r/16)*cos(4*t)
  polygon(x,y,col=col,border=NA)
}



# create new plot canvas
plot.new()
# limits are approximate here
plot.window(xlim=c(-16,16),ylim=c(-16,13))

# use mapply to loop
mapply(plotHeart,seq(16,0,length.out=100),greyScale(100))

Which results in:

This works by repeated drawing filled polygons of decreasing size and different colour atop of one another. To answer your questions:

(1) This was produced by my machine (a modest Core 2 duo laptop) in 0.09 seconds. They may be other languages/implementations that are faster, but this seems quick enough to me.

(2) A planar shape made up of lines which do not cross other is usually called a simple polygon.

Using 2D Graphics, this example alters the transparency of concentric circles using drawOval() to achieve a similar effect, but the approach can be extended to draw() any class implementing the Shape interface. The createTransformedShape() method of AffineTransform may be used to translate and scale the outline concentrically.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!