Barnsley fern: Difference between revisions
Content added Content deleted
ReeceGoding (talk | contribs) m (→{{header|R}}: Syntax highlighting.) |
ReeceGoding (talk | contribs) m (→'Obvious' solution: Improved syntax.) |
||
Line 2,416: | Line 2,416: | ||
==='Obvious' solution=== |
==='Obvious' solution=== |
||
The matrix solution above is a clever approach, but the following solution is more readable if you're unfamiliar with linear algebra. This is very much a blind "just do what the task says" solution. It's so simple that it probably runs unadapted in S. I suspect that there is room for an interesting use of R's ifelse function somewhere, but I couldn't find a clean way. |
The matrix solution above is a clever approach, but the following solution is more readable if you're unfamiliar with linear algebra. This is very much a blind "just do what the task says" solution. It's so simple that it probably runs unadapted in S. I suspect that there is room for an interesting use of R's ifelse function somewhere, but I couldn't find a clean way. |
||
<lang rsplus>fernOfNPoints<-function(n) |
<lang rsplus>fernOfNPoints <- function(n) |
||
{ |
{ |
||
currentX<-currentY<-newX<-newY<-0 |
currentX <- currentY <- newX <- newY <- 0 |
||
plot(0,0,xlim=c(-2,3),ylim=c(0,10),xlab="",ylab="",pch=20,col="darkgreen",cex=0.1) |
plot(0, 0, xlim = c(-2, 3), ylim = c(0, 10), xlab = "", ylab = "", pch = 20, col = "darkgreen", cex = 0.1) |
||
f1<-function()#ran 1% of the time |
f1 <- function()#ran 1% of the time |
||
{ |
{ |
||
newX<<-0 |
newX <<- 0 |
||
newY<<-0.16*currentY |
newY <<- 0.16 * currentY |
||
} |
} |
||
f2<-function()#ran 85% of the time |
f2 <- function()#ran 85% of the time |
||
{ |
{ |
||
newX<<-0.85*newX+0.04*newY |
newX <<- 0.85 * newX + 0.04 * newY |
||
newY<<--0.04*newX+0.85*newY+1.6 |
newY <<- -0.04 * newX + 0.85 * newY + 1.6 |
||
} |
} |
||
f3<-function()#ran 7% of the time |
f3 <- function()#ran 7% of the time |
||
{ |
{ |
||
newX<<-0.2*newX-0.26*newY |
newX <<- 0.2 * newX - 0.26 * newY |
||
newY<<-0.23*newX+0.22*newY+1.6 |
newY <<- 0.23 * newX + 0.22 * newY + 1.6 |
||
} |
} |
||
f4<-function()#ran 7% of the time |
f4 <- function()#ran 7% of the time |
||
{ |
{ |
||
newX<<--0.15*newX+0.28*newY |
newX <<- -0.15 * newX + 0.28 * newY |
||
newY<<-0.26*newX+0.24*newY+0.44 |
newY <<- 0.26 * newX + 0.24 * newY + 0.44 |
||
} |
|||
⚫ | |||
{ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
} |
||
⚫ | |||
{ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
|||
return(invisible()) |
return(invisible()) |
||
} |
} |