本文介绍了在R或Matlab中绘制一个像这样着色的多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 http://www.texample.net/tikz/examples/lindenmayer- systems / 我的示例代码如下所示,我不知道如何用色相颜色着色。 plot.koch < - 函数(k,col =blue){ plot(0,0,xlim = c(0,1),ylim = c(-sqrt(3)/ 6,sqrt(3)/ 2),asp = 1,type =n,xlab =,ylab =) plotkoch if (2 * 1 + y2)/ 3,n-1); (2 * x1 + x2)/ 3,(2 * y1 + y2)/ 3,(x1 + x2)/ 2(y1-y2)* sqrt(3)/ 6,(y1 + y2)/ 2-(x2-x1)* sqrt(3)/ 6,n-1)。 (3)/ 6,(2)/ 2(x2-x1)* sqrt(3)/ 6,(2) * 2 + x 1)/ 3,(2 * y 2 + y 1)/ 3,n-1)。 plotkoch((2 * x2 + x1)/ 3,(2 * y2 + y1)/ 3,x2,y2,n-1)} else {x = C(X 1,(2 * X1 + X2)/ 3,(X1 + X2)/ 2-(Y1-Y2)* SQRT(3)/ 6,(2 * X2 + X1)/ 3,X 2);通过= C(Y1 $ B $,(2 * Y1 + Y2)/ 3,(Y1 + Y2)/ 2-(X2-X1)* SQRT(3)/ 6,(2 * Y2 + Y1)/ 3, Y2); polygon(x,y,type =l,col = col)} } plotkoch(0,0,1,0,k) plotkoch(0.5,sqrt(3)/ 2,0,0,k) plotkoch(1,0,0.5,sqrt(3)/ 2,k)} plot.koch (3,col = 3) 解决方案 R中的空间对象,其中 sp , rgeos 和栅格包中。 函数稍作修改以返回 x , y 与用户的坐标(以正确的顺序): Koch yy 1){ Koch(x1,y1,(2 * x1 + x2)/ 3,(2 * y1 + y2)/ 3,n-1) Koch((2 * x1 + x2)/ 3,(2 * y1 + y2)/ 3,(x1 + x2)/ 2-(y1-y2)* sqrt(3)/ 6,(y1 + y2)/ 2-(x2-x1)* sqrt(3)/ 6,n-1)。 Koch((x1 + x2)/ 2-(y1-y2)* sqrt(3)/ 6,(y1 + y2)/ 2-(x2-x1)* sqrt(3)/ 6,$ b (2 * x2 + x1)/ 3,(2 * y2 + y1)/ 3,n-1)。 Koch((2 * x2 + x1)/ 3,(2 * y2 + y1)/ 3,x2,y2,n-1)} else {x< (x1,(2 * x1 + x2)/ 3,(x1 + x2)/ 2-(y1-y2)* sqrt(3)/ 6,(2 * x2 + x1)/ 3,x2)。 (y1,(2 * y1 + y2)/ 3,(y1 + y2)/ 2-x2-x1) * sqrt(3)/ 6,(2 * y2 + y1)/ 3,y2); yy<< - c(yy,y)} } Koch(0,0,1,0,k) Koch(1, 0,0.5,sqrt(3)/ 2,k) Koch(0.5,sqrt(3)/ 2,0,0,k) xy rbind(unique(xy),xy [1,])} $ b colr xy b 加载空间包并创建 SpatialPolygons library(sp) library(rgeos)$ b分形并将其绘制一次以设置绘图区域。 (多边形(xy)),1))) plot(poly) code> 绘制一系列具有所需原点和足够大的半径以覆盖分形多边形的片段(这里我们使用rad ius r )。 r mapply(function(theta,col){ segments(0.5, 0.3,0.5 + r *cosθ,0.3 + r *sinθ,lwd = 3,col = col)},seq(0,360,长度= 1000)* pi / 180,colr (1000)) 创建绘图区域和分形之间差异的第二个多边形( col ='white')来掩盖不需要的渐变区域。 plot(gDifference(as(extent(par('usr')),'SpatialPolygons'),poly), col ='white',border ='white',add = TRUE) plot(poly,add = TRUE) http://www.texample.net/tikz/examples/lindenmayer-systems/My sample code shown below, I don't know how to colored with hue color.plot.koch <- function(k,col="blue"){ plot(0,0,xlim=c(0,1), ylim=c(-sqrt(3)/6,sqrt(3)/2), asp = 1,type="n",xlab="", ylab="") plotkoch <- function(x1,y1,x2,y2,n){ if (n > 1){ plotkoch(x1,y1,(2*x1+x2)/3,(2*y1+y2)/3,n-1); plotkoch((2*x1+x2)/3,(2*y1+y2)/3,(x1+x2)/2-(y1-y2)*sqrt(3)/6,(y1+y2)/2-(x2-x1) *sqrt(3)/6,n-1); plotkoch((x1+x2)/2-(y1-y2)*sqrt(3)/6,(y1+y2)/2-(x2-x1)*sqrt(3)/6,(2*x2+x1)/3,(2 *y2+y1)/3,n-1); plotkoch((2*x2+x1)/3,(2*y2+y1)/3,x2,y2,n-1) } else { x=c(x1,(2*x1+x2)/3,(x1+x2)/2-(y1-y2)*sqrt(3)/6,(2*x2+x1)/3,x2); y=c(y1,(2*y1+y2)/3,(y1+y2)/2-(x2-x1)*sqrt(3)/6,(2*y2+y1)/3,y2); polygon(x,y,type="l",col=col) } } plotkoch(0,0,1,0,k) plotkoch(0.5,sqrt(3)/2,0,0,k) plotkoch(1,0,0.5,sqrt(3)/2,k)} plot.koch(3, col=3) 解决方案 Here's a method using spatial objects in R, with sp, rgeos and raster packages in the mix.Slight modifications to the function to return the x,y coordinates to the user (and in the correct order):koch <- function(k) { yy <- xx <- numeric(0) Koch <- function(x1, y1, x2, y2, n) { if (n > 1){ Koch(x1, y1, (2*x1+x2)/3, (2*y1+y2)/3, n-1); Koch((2*x1+x2)/3, (2*y1+y2)/3, (x1+x2)/2-(y1-y2)*sqrt(3)/6, (y1+y2)/2-(x2-x1) *sqrt(3)/6, n-1); Koch((x1+x2)/2-(y1-y2)*sqrt(3)/6, (y1+y2)/2-(x2-x1)*sqrt(3)/6, (2*x2+x1)/3, (2 *y2+y1)/3, n-1); Koch((2*x2+x1)/3, (2*y2+y1)/3, x2, y2, n-1) } else { x <- c(x1, (2*x1+x2)/3, (x1+x2)/2-(y1-y2)*sqrt(3)/6, (2*x2+x1)/3, x2); xx <<- c(xx, x) y <- c(y1, (2*y1+y2)/3, (y1+y2)/2-(x2-x1)*sqrt(3)/6, (2*y2+y1)/3, y2); yy <<- c(yy, y) } } Koch(0, 0, 1, 0, k) Koch(1, 0, 0.5, sqrt(3)/2, k) Koch(0.5, sqrt(3)/2, 0, 0, k) xy <- data.frame(x=xx, y=yy) rbind(unique(xy), xy[1, ])}Create a colour ramp:colr <- colorRampPalette(hcl(h=seq(0, 360, len=100), c=100))Use koch function to get vertices:xy <- koch(4)Load spatial packages and create SpatialPolygons object from fractal and plot it once to set up the plot area.library(sp)library(rgeos)library(raster)poly <- SpatialPolygons(list(Polygons(list(Polygon(xy)), 1)))plot(poly)Plot a series of segments with desired origin and large enough radius to cover the fractal polygon (here we use radius r <- 1). r <- 1mapply(function(theta, col) { segments(0.5, 0.3, 0.5 + r*cos(theta), 0.3 + r*sin(theta), lwd=3, col=col) }, seq(0, 360, length=1000)*pi/180, colr(1000)) Create a second polygon of the difference between the plot area and the fractal polygon, and plot this (with col='white') to mask out the unwanted gradient area.plot(gDifference(as(extent(par('usr')), 'SpatialPolygons'), poly), col='white', border='white', add=TRUE)Plot the polygon once more.plot(poly, add=TRUE) 这篇关于在R或Matlab中绘制一个像这样着色的多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-03 15:08