import java.lang.Math.{hypot, atan, cos, sin, pow}
abstract class Tree[+T] {
def forEach (f :(T,Int) => Unit):Unit = forEach (f, 0)
def forEach (f :(T,Int) => Unit, level:Int):Unit = {}
}
case class TreeNode[+T] (value:T, left:Tree[T], right:Tree[T]) extends Tree[T] {
override def forEach (f : (T,Int) => Unit, level:Int) = {
f(value, level)
left.forEach(f, level+1)
right.forEach(f, level+1)
}
}
case object EmptyTree extends Tree[Nothing]
case class Point (x:Double, y:Double)
case class Square (topLeft:Point, topRight:Point, bottomLeft:Point, bottomRight:Point, angle:Double)
val base = Square(Point(0,0), Point(100,0), Point(0,100), Point(100,100), 0)
val skew = 1
val halfPi = java.lang.Math.PI / 2
val quarterPi = java.lang.Math.PI / 4
val sqrt2 = java.lang.Math.pow(2, 0.5)
def makeTree (base:Square, goFor:Int):Tree[Square] = {
if (goFor > 0) {
val sideLength = hypot (base.topLeft.x - base.topRight.x, base.topLeft.y - base.topRight.y)
val newSideLength = (sideLength / sqrt2) * skew
val otherSideLength = pow(pow(sideLength,2) - pow(newSideLength,2), 0.5)
val angle = base.angle + atan(otherSideLength/newSideLength)
val otherAngle = angle - halfPi
val midPointX = base.topLeft.x + newSideLength * cos(angle)
val midPointY = base.topLeft.y - newSideLength * sin(angle)
val midPoint = Point(midPointX, midPointY)
TreeNode(base, makeSubTree(newSideLength, base.topLeft, midPoint, angle, goFor),
makeSubTree(otherSideLength, midPoint, base.topRight, otherAngle, goFor))
} else {
EmptyTree
}
}
def makeSubTree (length:Double, start:Point, end:Point, angle:Double, goFor:Int):Tree[Square] = {
val newSquare = Square (
Point(start.x - length*sin(angle), start.y - length * cos(angle)),
Point(end.x - length*sin(angle), end.y - length * cos(angle)),
start, end, angle)
makeTree(newSquare, goFor-1)
}
val maxLevel = 10
val newRoot = makeTree(base, maxLevel)
var count = 0
def showLine(start:Point, end:Point,color:String) = {
println("""<line id="%d" style="stroke:rgb(%s);stroke-width:1;fill:none;" x1="%f" y1="%f" x2="%f" y2="%f" />""".format
(count, color, start.x+300, start.y+300, end.x+300, end.y+300))
count += 1
}
println("""<svg x="0" y="0" width="800" height="600">""")
newRoot forEach {(square,level) =>
val color =
if (level == maxLevel - 1) "255,0,255"
else "0,0,128"
showLine(square.topLeft, square.topRight, color)
showLine(square.topLeft, square.bottomLeft, color)
showLine(square.bottomRight, square.topRight, color)
showLine(square.bottomLeft, square.bottomRight, color)
}
println("</svg>")
This is just awesome! I always knew that you could create a beautiful things with the help of Maths! Best regards, essay writer.
Great post and the way you described it makes me want to subscribe to your blog.
vitamin supplements
I have seen this fractal models used to simulate things like the spread of bacteria or even the life in a biosphere. In fact, a dental marketing firm used a similar model on their website to show how tooth decay can spread from one tooth to another. I love it when programming can be used to illustrate real world situations.
Superb illustration i like it :)
Hi there,
Really nice job,There are many people searching about that now they will find enough sources by your tips.
Also looking forward for more tips about that
How did you do this? Such a cool illustration! Hope you could come up for more and with tips! ;)
Macrocyclic metal super texture, light touching, stamping the brand LOGO, is not only unique, but also enhance the noble qualities of the moment
http://www.salecheapbags.com
I love it when programming can be used.You can always help people trough visiting this site that can find enough sources thorugh the tips.It is interesting to visit your site like used car parts in chicago in north shore auto parts.
It is really a thrill building your own thing and you can value it more because you spend more effort than buying it.It is how you can prove to your self that you can have by your own ideas and knowledge.
It is more likely from car parts used in chicago that quitely great and good products to be purchased cause it is
from the effort of the owner to make it done for the consumer.
www.louisvuitton30.com
You can see the opening of the jerseys on nikenfljerseysstore.com