2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,6 +1,10 @@
|
|||
Generate and draw a fractal tree.
|
||||
|
||||
To draw a fractal tree is simple:
|
||||
# Draw the trunk
|
||||
# At the end of the trunk, split by some angle and draw two branches
|
||||
# Repeat at the end of each branch until a sufficient level of branching is reached
|
||||
|
||||
|
||||
;Related tasks
|
||||
* [[Pythagoras_tree|Pythagoras Tree]]
|
||||
<br><br>
|
||||
|
|
|
|||
79
Task/Fractal-tree/Frege/fractal-tree.frege
Normal file
79
Task/Fractal-tree/Frege/fractal-tree.frege
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
module FractalTree where
|
||||
|
||||
import Java.IO
|
||||
import Prelude.Math
|
||||
|
||||
data AffineTransform = native java.awt.geom.AffineTransform where
|
||||
native new :: () -> STMutable s AffineTransform
|
||||
native clone :: Mutable s AffineTransform -> STMutable s AffineTransform
|
||||
native rotate :: Mutable s AffineTransform -> Double -> ST s ()
|
||||
native scale :: Mutable s AffineTransform -> Double -> Double -> ST s ()
|
||||
native translate :: Mutable s AffineTransform -> Double -> Double -> ST s ()
|
||||
|
||||
data BufferedImage = native java.awt.image.BufferedImage where
|
||||
pure native type_3byte_bgr "java.awt.image.BufferedImage.TYPE_3BYTE_BGR" :: Int
|
||||
native new :: Int -> Int -> Int -> STMutable s BufferedImage
|
||||
native createGraphics :: Mutable s BufferedImage -> STMutable s Graphics2D
|
||||
|
||||
data Color = pure native java.awt.Color where
|
||||
pure native black "java.awt.Color.black" :: Color
|
||||
pure native green "java.awt.Color.green" :: Color
|
||||
pure native white "java.awt.Color.white" :: Color
|
||||
pure native new :: Int -> Color
|
||||
|
||||
data BasicStroke = pure native java.awt.BasicStroke where
|
||||
pure native new :: Float -> BasicStroke
|
||||
|
||||
data RenderingHints = native java.awt.RenderingHints where
|
||||
pure native key_antialiasing "java.awt.RenderingHints.KEY_ANTIALIASING" :: RenderingHints_Key
|
||||
pure native value_antialias_on "java.awt.RenderingHints.VALUE_ANTIALIAS_ON" :: Object
|
||||
|
||||
data RenderingHints_Key = pure native java.awt.RenderingHints.Key
|
||||
|
||||
data Graphics2D = native java.awt.Graphics2D where
|
||||
native drawLine :: Mutable s Graphics2D -> Int -> Int -> Int -> Int -> ST s ()
|
||||
native drawOval :: Mutable s Graphics2D -> Int -> Int -> Int -> Int -> ST s ()
|
||||
native fillRect :: Mutable s Graphics2D -> Int -> Int -> Int -> Int -> ST s ()
|
||||
native setColor :: Mutable s Graphics2D -> Color -> ST s ()
|
||||
native setRenderingHint :: Mutable s Graphics2D -> RenderingHints_Key -> Object -> ST s ()
|
||||
native setStroke :: Mutable s Graphics2D -> BasicStroke -> ST s ()
|
||||
native setTransform :: Mutable s Graphics2D -> Mutable s AffineTransform -> ST s ()
|
||||
|
||||
data ImageIO = mutable native javax.imageio.ImageIO where
|
||||
native write "javax.imageio.ImageIO.write" :: MutableIO BufferedImage -> String -> MutableIO File -> IO Bool throws IOException
|
||||
|
||||
drawTree :: Mutable s Graphics2D -> Mutable s AffineTransform -> Int -> ST s ()
|
||||
drawTree g t i = do
|
||||
let len = 10 -- ratio of length to thickness
|
||||
shrink = 0.75
|
||||
angle = 0.3 -- radians
|
||||
i' = i - 1
|
||||
g.setTransform t
|
||||
g.drawLine 0 0 0 len
|
||||
when (i' > 0) $ do
|
||||
t.translate 0 (fromIntegral len)
|
||||
t.scale shrink shrink
|
||||
rt <- t.clone
|
||||
t.rotate angle
|
||||
rt.rotate (-angle)
|
||||
drawTree g t i'
|
||||
drawTree g rt i'
|
||||
|
||||
main = do
|
||||
let width = 900
|
||||
height = 800
|
||||
initScale = 20
|
||||
halfWidth = fromIntegral width / 2
|
||||
buffy <- BufferedImage.new width height BufferedImage.type_3byte_bgr
|
||||
g <- buffy.createGraphics
|
||||
g.setRenderingHint RenderingHints.key_antialiasing RenderingHints.value_antialias_on
|
||||
g.setColor Color.black
|
||||
g.fillRect 0 0 width height
|
||||
g.setColor Color.green
|
||||
t <- AffineTransform.new ()
|
||||
t.translate halfWidth (fromIntegral height)
|
||||
t.scale initScale initScale
|
||||
t.rotate pi
|
||||
drawTree g t 16
|
||||
f <- File.new "FractalTreeFrege.png"
|
||||
void $ ImageIO.write buffy "png" f
|
||||
8
Task/Fractal-tree/Haskell/fractal-tree-1.hs
Normal file
8
Task/Fractal-tree/Haskell/fractal-tree-1.hs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
import Graphics.Gloss
|
||||
|
||||
type Model = [Picture -> Picture]
|
||||
|
||||
fractal :: Int -> Model -> Picture -> Picture
|
||||
fractal n model pict = pictures $ take n $ iterate (mconcat model) pict
|
||||
|
||||
main = animate (InWindow "Tree" (800, 800) (0, 0)) white $ tree1 . (* 60)
|
||||
3
Task/Fractal-tree/Haskell/fractal-tree-2.hs
Normal file
3
Task/Fractal-tree/Haskell/fractal-tree-2.hs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
tree1 _ = fractal 10 branches $ Line [(0,0),(0,100)]
|
||||
where branches = [ Translate 0 100 . Scale 0.75 0.75 . Rotate 30
|
||||
, Translate 0 100 . Scale 0.5 0.5 . Rotate (-30) ]
|
||||
4
Task/Fractal-tree/Haskell/fractal-tree-3.hs
Normal file
4
Task/Fractal-tree/Haskell/fractal-tree-3.hs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
tree2 t = fractal 8 branches $ Line [(0,0),(0,100)]
|
||||
where branches = [ Translate 0 100 . Scale 0.75 0.75 . Rotate t
|
||||
, Translate 0 100 . Scale 0.6 0.6 . Rotate 0
|
||||
, Translate 0 100 . Scale 0.5 0.5 . Rotate (-2*t) ]
|
||||
3
Task/Fractal-tree/Haskell/fractal-tree-4.hs
Normal file
3
Task/Fractal-tree/Haskell/fractal-tree-4.hs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
circles t = fractal 10 model $ Circle 100
|
||||
where model = [ Translate 0 50 . Scale 0.5 0.5 . Rotate t
|
||||
, Translate 0 (-50) . Scale 0.5 0.5 . Rotate (-2*t) ]
|
||||
4
Task/Fractal-tree/Haskell/fractal-tree-5.hs
Normal file
4
Task/Fractal-tree/Haskell/fractal-tree-5.hs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
pithagor _ = fractal 10 model $ rectangleWire 100 100
|
||||
where model = [ Translate 50 100 . Scale s s . Rotate 45
|
||||
, Translate (-50) 100 . Scale s s . Rotate (-45)]
|
||||
s = 1/sqrt 2
|
||||
6
Task/Fractal-tree/Haskell/fractal-tree-6.hs
Normal file
6
Task/Fractal-tree/Haskell/fractal-tree-6.hs
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
pentaflake _ = fractal 5 model $ pentagon
|
||||
where model = map copy [0,72..288]
|
||||
copy a = Scale s s . Rotate a . Translate 0 x
|
||||
pentagon = Line [ (sin a, cos a) | a <- [0,2*pi/5..2*pi] ]
|
||||
x = 2*cos(pi/5)
|
||||
s = 1/(1+x)
|
||||
33
Task/Fractal-tree/PARI-GP/fractal-tree.pari
Normal file
33
Task/Fractal-tree/PARI-GP/fractal-tree.pari
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
\\ Fractal tree (w/recursion)
|
||||
\\ 4/10/16 aev
|
||||
plotline(x1,y1,x2,y2)={plotmove(0, x1,y1);plotrline(0,x2-x1,y2-y1);}
|
||||
|
||||
plottree(x,y,a,d)={
|
||||
my(x2,y2,d2r=Pi/180.0,a1=a*d2r,d1);
|
||||
if(d<=0, return(););
|
||||
if(d>0, d1=d*10.0;
|
||||
x2=x+cos(a1)*d1;
|
||||
y2=y+sin(a1)*d1;
|
||||
plotline(x,y,x2,y2);
|
||||
plottree(x2,y2,a-20,d-1);
|
||||
plottree(x2,y2,a+20,d-1),
|
||||
return();
|
||||
);
|
||||
}
|
||||
|
||||
FractalTree(depth,size)={
|
||||
my(dx=1,dy=0,ttlb="Fractal Tree, depth ",ttl=Str(ttlb,depth));
|
||||
print1(" *** ",ttl); print(", size ",size);
|
||||
plotinit(0);
|
||||
plotcolor(0,6); \\green
|
||||
plotscale(0, -size,size, 0,size);
|
||||
plotmove(0, 0,0);
|
||||
plottree(0,0,90,depth);
|
||||
plotdraw([0,size,size]);
|
||||
}
|
||||
|
||||
{\\ Executing:
|
||||
FractalTree(9,500); \\FracTree1.png
|
||||
FractalTree(12,1100); \\FracTree2.png
|
||||
FractalTree(15,1500); \\FracTree3.png
|
||||
}
|
||||
31
Task/Fractal-tree/ZX-Spectrum-Basic/fractal-tree.zx
Normal file
31
Task/Fractal-tree/ZX-Spectrum-Basic/fractal-tree.zx
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
10 LET level=12: LET long=45
|
||||
20 LET x=127: LET y=0
|
||||
30 LET rotation=PI/2
|
||||
40 LET a1=PI/9: LET a2=PI/9
|
||||
50 LET c1=0.75: LET c2=0.75
|
||||
60 DIM x(level): DIM y(level)
|
||||
70 BORDER 0: PAPER 0: INK 4: CLS
|
||||
80 GO SUB 100
|
||||
90 STOP
|
||||
100 REM Tree
|
||||
110 LET x(level)=x: LET y(level)=y
|
||||
120 GO SUB 1000
|
||||
130 IF level=1 THEN GO TO 240
|
||||
140 LET level=level-1
|
||||
150 LET long=long*c1
|
||||
160 LET rotation=rotation-a1
|
||||
170 GO SUB 100
|
||||
180 LET long=long/c1*c2
|
||||
190 LET rotation=rotation+a1+a2
|
||||
200 GO SUB 100
|
||||
210 LET rotation=rotation-a2
|
||||
220 LET long=long/c2
|
||||
230 LET level=level+1
|
||||
240 LET x=x(level): LET y=y(level)
|
||||
250 RETURN
|
||||
1000 REM Draw
|
||||
1010 LET yn=-SIN rotation*long+y
|
||||
1020 LET xn=COS rotation*long+x
|
||||
1030 PLOT x,y: DRAW xn-x,y-yn
|
||||
1040 LET x=xn: LET y=yn
|
||||
1050 RETURN
|
||||
Loading…
Add table
Add a link
Reference in a new issue