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 my attempt at solving your question. Currently it draws the color also outside of the snowflake. If you can figure out if points are inside or outside the snowflake, you should be able to just remove outside points in the
df_fill
. Here I'm first creating thedata.frame
used for plotting the polygon. Then I'm creating thedata.frame
for the background color. And finally I'm usingggplot2
to plot the data.It's my solution with grid package.
I would do it like this:
x,y
compute the
angle=atan2(y-y0,x-x0)
where
x0,y0
is the koch's snowflake mid positioncompute the color based on angle
if you use HSV then
hue=angle
and compute the target color value (I assume RGB). If you want the visible spectra colors you can try mine:Just convert the angle range
angle=<0,2*Pi> [rad]
to wavelengthl=<400,700> [nm]
so:render the pixel
[Notes]
not using R nor Matlab so you need to code it yourself. The angle may need some shifting to match your coordinate system for example:
If you drawing this as polygon then you need to compute color per vertex not per pixel but then you can get to problems because this is not convex polygon. So how to ensure the mid point color ??? I am afraid you will need to use some sort of triangulation because simple triangle fan will fail ...
The only thing that is obvious is to fill the color for whole space and then draw the outline with black color then flood fill all non black pixels from outside with white color
Here's a method using spatial objects in R, with
sp
,rgeos
andraster
packages in the mix.Slight modifications to the function to return the
x
,y
coordinates to the user (and in the correct order):Create a colour ramp:
Use
koch
function to get vertices:Load spatial packages and create
SpatialPolygons
object from fractal and plot it once to set up the plot area.Plot a series of segments with desired origin and large enough radius to cover the fractal polygon (here we use radius
r <- 1
).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 the polygon once more.