September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
40
Task/Fractal-tree/FreeBASIC/fractal-tree.freebasic
Normal file
40
Task/Fractal-tree/FreeBASIC/fractal-tree.freebasic
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
' version 17-03-2017
|
||||
' compile with: fbc -s gui
|
||||
|
||||
Const As Double deg2rad = Atn(1) / 45
|
||||
Dim Shared As Double scale = 0.76
|
||||
Dim Shared As Double spread = 25 * deg2rad ' convert degree's to rad's
|
||||
|
||||
Sub branch(x1 As ULong, y1 As ULong, size As ULong, angle As Double, depth As ULong)
|
||||
|
||||
Dim As ULong x2, y2
|
||||
|
||||
x2 = x1 + size * Cos(angle)
|
||||
y2 = y1 + size * Sin(angle)
|
||||
|
||||
Line (x1,y1) - (x2,y2), 2 ' palette color green
|
||||
If depth > 0 Then
|
||||
branch(x2, y2, size * scale, angle - spread, depth -1)
|
||||
branch(x2, y2, size * scale, angle + spread, depth -1)
|
||||
End If
|
||||
|
||||
End Sub
|
||||
|
||||
' ------=< MAIN >=-----
|
||||
|
||||
Dim As Double angle = -90 * deg2rad ' make sure that the tree grows up
|
||||
Dim As ULong SizeX = 800
|
||||
Dim As ULong SizeY = SizeX * 3 \ 4
|
||||
Dim As Double size = SizeY \ 4
|
||||
Dim As ULong depth = 11
|
||||
|
||||
ScreenRes SizeX, SizeY, 8
|
||||
WindowTitle ("Fractal Tree")
|
||||
|
||||
branch(SizeX\2, SizeY, size, angle, depth)
|
||||
|
||||
' empty keyboard buffer
|
||||
While InKey <> "" : Wend
|
||||
windowtitle ("Fractal Tree, hit any key to end program")
|
||||
Sleep
|
||||
End
|
||||
|
|
@ -5,4 +5,8 @@ type Model = [Picture -> Picture]
|
|||
fractal :: Int -> Model -> Picture -> Picture
|
||||
fractal n model pict = pictures $ take n $ iterate (mconcat model) pict
|
||||
|
||||
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) ]
|
||||
|
||||
main = animate (InWindow "Tree" (800, 800) (0, 0)) white $ tree1 . (* 60)
|
||||
|
|
|
|||
|
|
@ -1,3 +1,24 @@
|
|||
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) ]
|
||||
--animated tree
|
||||
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) ]
|
||||
|
||||
--animated fractal clock
|
||||
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) ]
|
||||
|
||||
--Pythagoras tree
|
||||
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
|
||||
|
||||
--Sierpinski pentagon
|
||||
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)
|
||||
|
|
|
|||
|
|
@ -1,4 +1,31 @@
|
|||
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) ]
|
||||
import Graphics.HGL.Window
|
||||
import Graphics.HGL.Run
|
||||
import Control.Arrow
|
||||
import Control.Monad
|
||||
import Data.List
|
||||
|
||||
enumBase :: Int -> Int -> [[Int]]
|
||||
enumBase n = mapM (enumFromTo 0). replicate n. pred
|
||||
|
||||
psPlus (a,b) (p,q) = (a+p, b+q)
|
||||
|
||||
toInt :: Double -> Int
|
||||
toInt = fromIntegral.round
|
||||
|
||||
intPoint = toInt *** toInt
|
||||
|
||||
pts n =
|
||||
map (map (intPoint.psPlus (100,0)). ((0,300):). scanl1 psPlus. ((r,300):). zipWith (\h a -> (h*cos a, h*sin a)) rs) hs
|
||||
where
|
||||
[r,h,sr,sh] = [50, pi/5, 0.9, 0.75]
|
||||
rs = take n $ map (r*) $ iterate(*sr) sr
|
||||
lhs = map (map (((-1)**).fromIntegral)) $ enumBase n 2
|
||||
rhs = take n $ map (h*) $ iterate(*sh) 1
|
||||
hs = map (scanl1 (+). zipWith (*)rhs) lhs
|
||||
|
||||
fractalTree :: Int -> IO ()
|
||||
fractalTree n =
|
||||
runWindow "Fractal Tree" (500,600)
|
||||
(\w -> setGraphic w (overGraphics ( map polyline $ pts (n-1))) >> getKey w)
|
||||
|
||||
main = fractalTree 10
|
||||
|
|
|
|||
|
|
@ -1,3 +0,0 @@
|
|||
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) ]
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
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
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
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)
|
||||
|
|
@ -1,29 +0,0 @@
|
|||
import Graphics.HGL.Window
|
||||
import Graphics.HGL.Run
|
||||
import Control.Arrow
|
||||
import Control.Monad
|
||||
import Data.List
|
||||
|
||||
enumBase :: Int -> Int -> [[Int]]
|
||||
enumBase n = mapM (enumFromTo 0). replicate n. pred
|
||||
|
||||
psPlus (a,b) (p,q) = (a+p, b+q)
|
||||
|
||||
toInt :: Double -> Int
|
||||
toInt = fromIntegral.round
|
||||
|
||||
intPoint = toInt *** toInt
|
||||
|
||||
pts n =
|
||||
map (map (intPoint.psPlus (100,0)). ((0,300):). scanl1 psPlus. ((r,300):). zipWith (\h a -> (h*cos a, h*sin a)) rs) hs
|
||||
where
|
||||
[r,h,sr,sh] = [50, pi/5, 0.9, 0.75]
|
||||
rs = take n $ map (r*) $ iterate(*sr) sr
|
||||
lhs = map (map (((-1)**).fromIntegral)) $ enumBase n 2
|
||||
rhs = take n $ map (h*) $ iterate(*sh) 1
|
||||
hs = map (scanl1 (+). zipWith (*)rhs) lhs
|
||||
|
||||
fractalTree :: Int -> IO ()
|
||||
fractalTree n =
|
||||
runWindow "Fractal Tree" (500,600)
|
||||
(\w -> setGraphic w (overGraphics ( map polyline $ pts (n-1))) >> getKey w)
|
||||
32
Task/Fractal-tree/Kotlin/fractal-tree.kotlin
Normal file
32
Task/Fractal-tree/Kotlin/fractal-tree.kotlin
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
// version 1.1.2
|
||||
|
||||
import java.awt.Color
|
||||
import java.awt.Graphics
|
||||
import javax.swing.JFrame
|
||||
|
||||
class FractalTree : JFrame("Fractal Tree") {
|
||||
init {
|
||||
background = Color.black
|
||||
setBounds(100, 100, 800, 600)
|
||||
isResizable = false
|
||||
defaultCloseOperation = EXIT_ON_CLOSE
|
||||
}
|
||||
|
||||
private fun drawTree(g: Graphics, x1: Int, y1: Int, angle: Double, depth: Int) {
|
||||
if (depth == 0) return
|
||||
val x2 = x1 + (Math.cos(Math.toRadians(angle)) * depth * 10.0).toInt()
|
||||
val y2 = y1 + (Math.sin(Math.toRadians(angle)) * depth * 10.0).toInt()
|
||||
g.drawLine(x1, y1, x2, y2)
|
||||
drawTree(g, x2, y2, angle - 20, depth - 1)
|
||||
drawTree(g, x2, y2, angle + 20, depth - 1)
|
||||
}
|
||||
|
||||
override fun paint(g: Graphics) {
|
||||
g.color = Color.white
|
||||
drawTree(g, 400, 500, -90.0, 9)
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
FractalTree().isVisible = true
|
||||
}
|
||||
37
Task/Fractal-tree/Lua/fractal-tree.lua
Normal file
37
Task/Fractal-tree/Lua/fractal-tree.lua
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
g, angle = love.graphics, 26 * math.pi / 180
|
||||
wid, hei = g.getWidth(), g.getHeight()
|
||||
function rotate( x, y, a )
|
||||
local s, c = math.sin( a ), math.cos( a )
|
||||
local a, b = x * c - y * s, x * s + y * c
|
||||
return a, b
|
||||
end
|
||||
function branches( a, b, len, ang, dir )
|
||||
len = len * .76
|
||||
if len < 5 then return end
|
||||
g.setColor( len * 16, 255 - 2 * len , 0 )
|
||||
if dir > 0 then ang = ang - angle
|
||||
else ang = ang + angle
|
||||
end
|
||||
local vx, vy = rotate( 0, len, ang )
|
||||
vx = a + vx; vy = b - vy
|
||||
g.line( a, b, vx, vy )
|
||||
branches( vx, vy, len, ang, 1 )
|
||||
branches( vx, vy, len, ang, 0 )
|
||||
end
|
||||
function createTree()
|
||||
local lineLen = 127
|
||||
local a, b = wid / 2, hei - lineLen
|
||||
g.setColor( 160, 40 , 0 )
|
||||
g.line( wid / 2, hei, a, b )
|
||||
branches( a, b, lineLen, 0, 1 )
|
||||
branches( a, b, lineLen, 0, 0 )
|
||||
end
|
||||
function love.load()
|
||||
canvas = g.newCanvas( wid, hei )
|
||||
g.setCanvas( canvas )
|
||||
createTree()
|
||||
g.setCanvas()
|
||||
end
|
||||
function love.draw()
|
||||
g.draw( canvas )
|
||||
end
|
||||
|
|
@ -1,4 +1,7 @@
|
|||
include ..\pGUI\pGUI.e
|
||||
--
|
||||
-- demo\rosetta\FractalTree.exw
|
||||
--
|
||||
include pGUI.e
|
||||
|
||||
Ihandle dlg, canvas
|
||||
cdCanvas cddbuffer, cdcanvas
|
||||
|
|
@ -38,7 +41,7 @@ function esc_close(Ihandle /*ih*/, atom c)
|
|||
end function
|
||||
|
||||
procedure main()
|
||||
IupOpen("..\\pGUI\\")
|
||||
IupOpen()
|
||||
|
||||
canvas = IupCanvas(NULL)
|
||||
IupSetAttribute(canvas, "RASTERSIZE", "640x480")
|
||||
|
|
|
|||
34
Task/Fractal-tree/R/fractal-tree.r
Normal file
34
Task/Fractal-tree/R/fractal-tree.r
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
## Recursive FT plotting
|
||||
plotftree <- function(x, y, a, d, c) {
|
||||
x2=y2=0; d2r=pi/180.0; a1 <- a*d2r; d1=0;
|
||||
if(d<=0) {return()}
|
||||
if(d>0)
|
||||
{ d1=d*10.0;
|
||||
x2=x+cos(a1)*d1;
|
||||
y2=y+sin(a1)*d1;
|
||||
segments(x*c, y*c, x2*c, y2*c, col='darkgreen');
|
||||
plotftree(x2,y2,a-20,d-1,c);
|
||||
plotftree(x2,y2,a+20,d-1,c);
|
||||
#return(2);
|
||||
}
|
||||
}
|
||||
## Plotting Fractal Tree. aev 3/27/17
|
||||
## ord - order/depth, c - scale, xsh - x-shift, fn - file name,
|
||||
## ttl - plot title.
|
||||
pFractalTree <- function(ord, c=1, xsh=0, fn="", ttl="") {
|
||||
cat(" *** START FRT:", date(), "\n");
|
||||
m=640;
|
||||
if(fn=="") {pf=paste0("FRTR", ord, ".png")} else {pf=paste0(fn, ".png")};
|
||||
if(ttl=="") {ttl=paste0("Fractal tree, order - ", ord)};
|
||||
cat(" *** Plot file -", pf, "title:", ttl, "\n");
|
||||
##plot(NA, xlim=c(0,m), ylim=c(-m,0), xlab="", ylab="", main=ttl);
|
||||
plot(NA, xlim=c(0,m), ylim=c(0,m), xlab="", ylab="", main=ttl);
|
||||
plotftree(m/2+xsh,100,90,ord,c);
|
||||
dev.copy(png, filename=pf, width=m, height=m);
|
||||
dev.off(); graphics.off();
|
||||
cat(" *** END FRT:",date(),"\n");
|
||||
}
|
||||
## Executing:
|
||||
pFractalTree(9);
|
||||
pFractalTree(12,0.6,210);
|
||||
pFractalTree(15,0.35,600);
|
||||
51
Task/Fractal-tree/Scheme/fractal-tree.ss
Normal file
51
Task/Fractal-tree/Scheme/fractal-tree.ss
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
(import (scheme base)
|
||||
(scheme file)
|
||||
(scheme inexact)
|
||||
(scheme write))
|
||||
|
||||
(define *scale* 10) ; controls overall size of tree
|
||||
(define *split* 20) ; controls angle of split (in degrees)
|
||||
|
||||
;; construct lines for tree as list of 5-tuples (x1 y1 x2 y2 depth)
|
||||
;; - x1 y1 is start point
|
||||
;; - angle of this line, in radians
|
||||
;; - depth, depth within tree (controls length of line)
|
||||
(define (create-tree x1 y1 angle depth)
|
||||
(define (degrees->radians d)
|
||||
(let ((pi 3.14159265358979323846264338327950288419716939937510582097))
|
||||
(* d pi 1/180)))
|
||||
;
|
||||
(if (zero? depth)
|
||||
'()
|
||||
(let ((x2 (+ x1 (* (cos (degrees->radians angle)) depth *scale*)))
|
||||
(y2 (+ y1 (* (sin (degrees->radians angle)) depth *scale*))))
|
||||
(append (list (map truncate (list x1 y1 x2 y2 depth)))
|
||||
(create-tree x2 y2 (- angle *split*) (- depth 1))
|
||||
(create-tree x2 y2 (+ angle *split*) (- depth 1))))))
|
||||
|
||||
;; output the tree to an eps file
|
||||
(define (output-tree-as-eps filename tree)
|
||||
(when (file-exists? filename) (delete-file filename))
|
||||
(with-output-to-file
|
||||
filename
|
||||
(lambda ()
|
||||
(display "%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 800 800\n")
|
||||
|
||||
;; add each line - sets linewidth based on depth in tree
|
||||
(for-each (lambda (line)
|
||||
(display
|
||||
(string-append "newpath\n"
|
||||
(number->string (list-ref line 0)) " "
|
||||
(number->string (list-ref line 1)) " "
|
||||
"moveto\n"
|
||||
(number->string (list-ref line 2)) " "
|
||||
(number->string (list-ref line 3)) " "
|
||||
"lineto\n"
|
||||
(number->string (truncate (/ (list-ref line 4) 2)))
|
||||
" setlinewidth\n"
|
||||
"stroke\n"
|
||||
)))
|
||||
tree)
|
||||
(display "\n%%EOF"))))
|
||||
|
||||
(output-tree-as-eps "fractal.eps" (create-tree 400 200 90 9))
|
||||
80
Task/Fractal-tree/Scilab/fractal-tree-1.scilab
Normal file
80
Task/Fractal-tree/Scilab/fractal-tree-1.scilab
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
trunk = 1; //trunk length
|
||||
ratio = 0.8; //size ratio between two consecutive branches
|
||||
depth = 9; //final number of branch levels
|
||||
orign = 0; //origin of the tree (should be complex)
|
||||
angle = 45*%pi/180; //angle between two branches [rad]
|
||||
trunk_angle = 90*%pi/180; //angle between trunk and X-axis [rad]
|
||||
|
||||
right_angle = angle/2; //angles to the right or to the left
|
||||
left_angle = 0.8*angle; //can be set independently or
|
||||
//as function of 'angle'
|
||||
|
||||
//L-system definition:
|
||||
//Alphabet: FBD[]+-
|
||||
//F: go forward B: go backwards
|
||||
//[: start new branch ]: end current branch
|
||||
//+: branch to the right -: branch to the left
|
||||
//D: double line (forward then backward)
|
||||
//Axiom: D
|
||||
//Rule: D -> F[+D-D]B
|
||||
|
||||
//L-system sentence generation
|
||||
sentence = 'D'
|
||||
rule = 'F[+D-D]B';
|
||||
for i=1:depth
|
||||
sentence = strsubst(sentence,'D',rule);
|
||||
end
|
||||
sentence = strsplit(sentence)';
|
||||
|
||||
//Empty tree
|
||||
tree_size = 1.0...
|
||||
+ length(find(sentence=='F'|sentence=='B'))...
|
||||
+ 2 * length(find(sentence=='D'));
|
||||
tree=zeros(tree_size,1);
|
||||
|
||||
//Drawing the tree
|
||||
branch_level = 0;
|
||||
curr_angle = trunk_angle;
|
||||
curr_pos = 1;
|
||||
|
||||
for ind = 1:size(sentence,'c')
|
||||
charac = sentence(ind);
|
||||
|
||||
select charac
|
||||
case 'F' then //Draw line forward
|
||||
tree(curr_pos+1) = tree(curr_pos)...
|
||||
+ trunk * ratio^branch_level * exp(curr_angle*%i);
|
||||
curr_pos = curr_pos + 1;
|
||||
|
||||
case 'B' then //Draw line backwards
|
||||
tree(curr_pos+1) = tree(curr_pos)...
|
||||
+ trunk * ratio^branch_level * exp((%pi+curr_angle)*%i);
|
||||
curr_pos = curr_pos + 1;
|
||||
|
||||
case '[' then //New branch
|
||||
branch_level = branch_level + 1;
|
||||
|
||||
case '+' then //Turn right
|
||||
curr_angle = curr_angle - right_angle;
|
||||
|
||||
case '-' then //Turn left
|
||||
curr_angle = curr_angle + right_angle + left_angle;
|
||||
|
||||
case ']' then //End of branch
|
||||
branch_level = branch_level - 1;
|
||||
curr_angle = curr_angle - left_angle;
|
||||
|
||||
case 'D' then //Double line
|
||||
tree(curr_pos+1) = tree(curr_pos)...
|
||||
+ trunk * ratio^branch_level * exp(curr_angle*%i);
|
||||
tree(curr_pos+2) = tree(curr_pos+1)...
|
||||
+ trunk * ratio^branch_level * exp((%pi+curr_angle)*%i);
|
||||
curr_pos = curr_pos + 2;
|
||||
end
|
||||
end
|
||||
|
||||
scf(); clf();
|
||||
xname('Fractal tree: '+string(depth)+' levels')
|
||||
plot2d(real(tree),imag(tree),14);
|
||||
set(gca(),'isoview','on');
|
||||
set(gca(),'axes_visible',['off','off','off']);
|
||||
17
Task/Fractal-tree/Scilab/fractal-tree-2.scilab
Normal file
17
Task/Fractal-tree/Scilab/fractal-tree-2.scilab
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
width = 512;
|
||||
height = 512;
|
||||
img=scf();
|
||||
set(img,'figure_size',[width,height]);
|
||||
|
||||
function drawTree(x1, y1, angle, depth)
|
||||
if depth ~= 0 then
|
||||
x2 = x1 + cos(angle * %pi/180) * depth * 10;
|
||||
y2 = y1 + sin(angle * %pi/180) * depth * 10;
|
||||
plot2d([x1 x2],[y1 y2],14);
|
||||
drawTree(x2, y2, angle - 20, depth - 1);
|
||||
drawTree(x2, y2, angle + 20, depth - 1);
|
||||
end
|
||||
endfunction
|
||||
|
||||
drawTree(width/2,height,90,10);
|
||||
set(gca(),'isoview','on');
|
||||
18
Task/Fractal-tree/Zkl/fractal-tree.zkl
Normal file
18
Task/Fractal-tree/Zkl/fractal-tree.zkl
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
fcn fractalTree(){
|
||||
scale:=0.76;
|
||||
sizeX:=400; sizeY:=300;
|
||||
bitmap:=PPM(sizeX*2,sizeY*2,0xFF|FF|FF);
|
||||
branch:='wrap(x1,y1,size,angle,depth){
|
||||
ar:=angle.toRad();
|
||||
x2:=x1 - size*ar.cos();
|
||||
y2:=y1 + size*ar.sin();
|
||||
color:=(0xff-depth*8).shiftLeft(16) + (depth*12+100).shiftLeft(8);
|
||||
bitmap.line(x1,y1, x2,y2, color);
|
||||
if(depth){
|
||||
self.fcn(x2,y2,scale*size,angle - 30,depth - 1,vm.pasteArgs(5));
|
||||
self.fcn(x2,y2,scale*size,angle + 8, depth - 1,vm.pasteArgs(5));
|
||||
}
|
||||
};
|
||||
branch(sizeX,0,sizeY/2,90.0,10);
|
||||
bitmap.write(File("foo.ppm","wb"));
|
||||
}();
|
||||
Loading…
Add table
Add a link
Reference in a new issue