Pythagoras tree: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 13: | Line 13: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
{{libheader|SDLAda}} |
{{libheader|SDLAda}} |
||
< |
<syntaxhighlight lang="ada">with SDL.Video.Windows.Makers; |
||
with SDL.Video.Renderers.Makers; |
with SDL.Video.Renderers.Makers; |
||
with SDL.Video.Rectangles; |
with SDL.Video.Rectangles; |
||
Line 98: | Line 98: | ||
Window.Finalize; |
Window.Finalize; |
||
SDL.Finalise; |
SDL.Finalise; |
||
end Pythagoras_Tree;</ |
end Pythagoras_Tree;</syntaxhighlight> |
||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
Requires [https://www.autohotkey.com/boards/viewtopic.php?t=6517 Gdip Library] |
Requires [https://www.autohotkey.com/boards/viewtopic.php?t=6517 Gdip Library] |
||
< |
<syntaxhighlight lang="autohotkey">pToken := Gdip_Startup() |
||
gdip1() |
gdip1() |
||
Pythagoras_tree(600, 600, 712, 600, 1) |
Pythagoras_tree(600, 600, 712, 600, 1) |
||
Line 172: | Line 172: | ||
gdip2() |
gdip2() |
||
ExitApp |
ExitApp |
||
return</ |
return</syntaxhighlight> |
||
=={{header|BASIC256}}== |
=={{header|BASIC256}}== |
||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
<syntaxhighlight lang="basic256"> |
|||
<lang BASIC256> |
|||
Subroutine pythagoras_tree(x1, y1, x2, y2, depth) |
Subroutine pythagoras_tree(x1, y1, x2, y2, depth) |
||
If depth > 10 Then Return |
If depth > 10 Then Return |
||
Line 204: | Line 204: | ||
ImgSave "pythagoras_tree.jpg", "jpg" |
ImgSave "pythagoras_tree.jpg", "jpg" |
||
End |
End |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|C}}== |
=={{header|C}}== |
||
Line 210: | Line 210: | ||
Requires the [http://www.cs.colorado.edu/~main/bgi/cs1300/ WinBGIm] library. |
Requires the [http://www.cs.colorado.edu/~main/bgi/cs1300/ WinBGIm] library. |
||
<syntaxhighlight lang="c"> |
|||
<lang C> |
|||
#include<graphics.h> |
#include<graphics.h> |
||
#include<stdlib.h> |
#include<stdlib.h> |
||
Line 277: | Line 277: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
Line 283: | Line 283: | ||
Windows version |
Windows version |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="cpp">#include <windows.h> |
||
#include <string> |
#include <string> |
||
#include <iostream> |
#include <iostream> |
||
Line 407: | Line 407: | ||
t.save( "?:/pt.bmp" ); |
t.save( "?:/pt.bmp" ); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|EasyLang}}== |
=={{header|EasyLang}}== |
||
Line 413: | Line 413: | ||
[https://easylang.online/apps/_pythagoras-tree.html Run it] |
[https://easylang.online/apps/_pythagoras-tree.html Run it] |
||
<lang>func tree x1 y1 x2 y2 depth . . |
<syntaxhighlight lang="text">func tree x1 y1 x2 y2 depth . . |
||
if depth < 8 |
if depth < 8 |
||
dx = x2 - x1 |
dx = x2 - x1 |
||
Line 431: | Line 431: | ||
. |
. |
||
color3 0.3 0 0.1 |
color3 0.3 0 0.1 |
||
call tree 41 90 59 90 0</ |
call tree 41 90 59 90 0</syntaxhighlight> |
||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
<p>Creating an HTML file with an inline SVG. The generation of the tree is done breadth first.</p> |
<p>Creating an HTML file with an inline SVG. The generation of the tree is done breadth first.</p> |
||
< |
<syntaxhighlight lang="fsharp">type Point = { x:float; y:float } |
||
type Line = { left : Point; right : Point } |
type Line = { left : Point; right : Point } |
||
Line 493: | Line 493: | ||
generate [{ left = { x = 275.; y = 500. }; right = { x = 375.; y = 500. } }] depth |
generate [{ left = { x = 275.; y = 500. }; right = { x = 375.; y = 500. } }] depth |
||
out draw_end_html |
out draw_end_html |
||
0</ |
0</syntaxhighlight> |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
{{trans|zkl}} |
{{trans|zkl}} |
||
< |
<syntaxhighlight lang="freebasic">' version 03-12-2016 |
||
' compile with: fbc -s gui |
' compile with: fbc -s gui |
||
' or fbc -s console |
' or fbc -s console |
||
Line 534: | Line 534: | ||
Print : Print "hit any key to end program" |
Print : Print "hit any key to end program" |
||
Sleep |
Sleep |
||
End</ |
End</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 622: | Line 622: | ||
} |
} |
||
return x |
return x |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
Line 630: | Line 630: | ||
Firstly, we define a function <code>mkBranches</code> that produces a pair of minor squares based on a given square. Each square is represented as a list of points. |
Firstly, we define a function <code>mkBranches</code> that produces a pair of minor squares based on a given square. Each square is represented as a list of points. |
||
< |
<syntaxhighlight lang="haskell">mkBranches :: [(Float,Float)] -> [[(Float,Float)]] |
||
mkBranches [a, b, c, d] = let d = 0.5 <*> (b <+> (-1 <*> a)) |
mkBranches [a, b, c, d] = let d = 0.5 <*> (b <+> (-1 <*> a)) |
||
l1 = d <+> orth d |
l1 = d <+> orth d |
||
Line 640: | Line 640: | ||
(a, b) <+> (c, d) = (a+c, b+d) |
(a, b) <+> (c, d) = (a+c, b+d) |
||
n <*> (a, b) = (a*n, b*n) |
n <*> (a, b) = (a*n, b*n) |
||
orth (a, b) = (-b, a)</ |
orth (a, b) = (-b, a)</syntaxhighlight> |
||
We then create <code>squares</code> using <code>mkBranches</code> to build a list representing the set of squares. In order to apply this function iteratively to form a 10-generation tree, we also have to define the monadic iteration <code>iterateM</code> within <code>squares</code>. |
We then create <code>squares</code> using <code>mkBranches</code> to build a list representing the set of squares. In order to apply this function iteratively to form a 10-generation tree, we also have to define the monadic iteration <code>iterateM</code> within <code>squares</code>. |
||
< |
<syntaxhighlight lang="haskell">squares = concat $ take 10 $ iterateM mkBranches start |
||
where start = [(0,100),(100,100),(100,0),(0,0)] |
where start = [(0,100),(100,100),(100,0),(0,0)] |
||
iterateM f x = iterate (>>= f) (pure x)</ |
iterateM f x = iterate (>>= f) (pure x)</syntaxhighlight> |
||
The raw result returned by <code>squares</code> should be used in the <code>main</code> function in order to be displayed in a new window, saved directly to a SVG file, or printed to a bitmap file. |
The raw result returned by <code>squares</code> should be used in the <code>main</code> function in order to be displayed in a new window, saved directly to a SVG file, or printed to a bitmap file. |
||
Line 652: | Line 652: | ||
'''Window output''' |
'''Window output''' |
||
{{libheader|Gloss}} |
{{libheader|Gloss}} |
||
< |
<syntaxhighlight lang="haskell">--import should go to the top of the code |
||
import Graphics.Gloss |
import Graphics.Gloss |
||
main = display (InWindow "Pithagoras tree" (400, 400) (0, 0)) white tree |
main = display (InWindow "Pithagoras tree" (400, 400) (0, 0)) white tree |
||
where tree = foldMap lineLoop squares</ |
where tree = foldMap lineLoop squares</syntaxhighlight> |
||
'''SVG file''' |
'''SVG file''' |
||
< |
<syntaxhighlight lang="haskell">main = writeFile "pith.svg" svg |
||
where svg = "<svg " ++ attrs ++ foldMap (mkLine . close) squares ++ "</svg>" |
where svg = "<svg " ++ attrs ++ foldMap (mkLine . close) squares ++ "</svg>" |
||
attrs = "fill='none' stroke='black' height='400' width='600'>" |
attrs = "fill='none' stroke='black' height='400' width='600'>" |
||
mkLine path = "<polyline points ='" ++ foldMap mkPoint path ++ "'/>" |
mkLine path = "<polyline points ='" ++ foldMap mkPoint path ++ "'/>" |
||
mkPoint (x,y) = show (250+x) ++ "," ++ show (400-y) ++ " " |
mkPoint (x,y) = show (250+x) ++ "," ++ show (400-y) ++ " " |
||
close lst = lst ++ [head lst]</ |
close lst = lst ++ [head lst]</syntaxhighlight> |
||
'''Bitmap image''' |
'''Bitmap image''' |
||
{{libheader|easyplot}} |
{{libheader|easyplot}} |
||
< |
<syntaxhighlight lang="haskell">--import should go to the top of the code |
||
import Graphics.EasyPlot |
import Graphics.EasyPlot |
||
Line 674: | Line 674: | ||
main = plot (PNG "pith.png") $ map (mkLine . close) squares |
main = plot (PNG "pith.png") $ map (mkLine . close) squares |
||
where mkLine = Data2D [Style Lines, Color Black,Title ""] [] |
where mkLine = Data2D [Style Lines, Color Black,Title ""] [] |
||
close lst = lst ++ [head lst]</ |
close lst = lst ++ [head lst]</syntaxhighlight> |
||
=={{header|IS-BASIC}}== |
=={{header|IS-BASIC}}== |
||
< |
<syntaxhighlight lang="is-basic">100 PROGRAM "Pythagor.bas" |
||
110 OPTION ANGLE DEGREES |
110 OPTION ANGLE DEGREES |
||
120 LET SQ2=SQR(2) |
120 LET SQ2=SQR(2) |
||
Line 702: | Line 702: | ||
330 PLOT FORWARD X;RIGHT 90; |
330 PLOT FORWARD X;RIGHT 90; |
||
340 NEXT |
340 NEXT |
||
350 END DEF</ |
350 END DEF</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
Line 711: | Line 711: | ||
</pre> |
</pre> |
||
<syntaxhighlight lang="j"> |
|||
<lang J> |
|||
NB. use on linux: gnuplot --persist -e 'plot"< ijconsole /tmp/pt.ijs"w l' |
NB. use on linux: gnuplot --persist -e 'plot"< ijconsole /tmp/pt.ijs"w l' |
||
Line 758: | Line 758: | ||
petri 1 |
petri 1 |
||
exit 0 |
exit 0 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Java}}== |
=={{header|Java}}== |
||
[[File:pythagoras_tree.png|300px|thumb|right]] |
[[File:pythagoras_tree.png|300px|thumb|right]] |
||
{{works with|Java|8}} |
{{works with|Java|8}} |
||
< |
<syntaxhighlight lang="java">import java.awt.*; |
||
import java.awt.geom.Path2D; |
import java.awt.geom.Path2D; |
||
import javax.swing.*; |
import javax.swing.*; |
||
Line 837: | Line 837: | ||
}); |
}); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="javascript"><!DOCTYPE html> |
||
<html lang="en"> |
<html lang="en"> |
||
Line 945: | Line 945: | ||
</body> |
</body> |
||
</html></ |
</html></syntaxhighlight> |
||
=={{header|jq}}== |
=={{header|jq}}== |
||
Line 956: | Line 956: | ||
Notice that the SVG viewBox dimensions are computed dynamically. |
Notice that the SVG viewBox dimensions are computed dynamically. |
||
<syntaxhighlight lang="jq"> |
|||
<lang jq> |
|||
# viewBox = <min-x> <min-y> <width> <height> |
# viewBox = <min-x> <min-y> <width> <height> |
||
# Input: {svg, minx, miny, maxx, maxy} |
# Input: {svg, minx, miny, maxx, maxy} |
||
Line 1,017: | Line 1,017: | ||
PythagorasTree | svg |
PythagorasTree | svg |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,023: | Line 1,023: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{trans|PARI/GP}} |
{{trans|PARI/GP}} |
||
< |
<syntaxhighlight lang="julia">using Gadfly |
||
using DataFrames |
using DataFrames |
||
Line 1,066: | Line 1,066: | ||
end |
end |
||
pythagorastree(275.,500.,375.,500.,640., 9)</ |
pythagorastree(275.,500.,375.,500.,640., 9)</syntaxhighlight> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="scala">// version 1.1.2 |
||
import java.awt.* |
import java.awt.* |
||
Line 1,149: | Line 1,149: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|M2000 Interpreter}}== |
=={{header|M2000 Interpreter}}== |
||
===Cartesian Coordinates=== |
===Cartesian Coordinates=== |
||
{{trans|zkl}} |
{{trans|zkl}} |
||
<syntaxhighlight lang="m2000 interpreter"> |
|||
<lang M2000 Interpreter> |
|||
MODULE Pythagoras_tree { |
MODULE Pythagoras_tree { |
||
CLS 5, 0 ' MAGENTA, NO SPLIT SCREEN |
CLS 5, 0 ' MAGENTA, NO SPLIT SCREEN |
||
Line 1,184: | Line 1,184: | ||
} |
} |
||
Pythagoras_tree |
Pythagoras_tree |
||
</syntaxhighlight> |
|||
</lang> |
|||
===Polar Coordinates=== |
===Polar Coordinates=== |
||
<syntaxhighlight lang="m2000 interpreter"> |
|||
<lang M2000 Interpreter> |
|||
MODULE Pythagoras_Example{ |
MODULE Pythagoras_Example{ |
||
CLS 5, 0 ' MAGENTA, split line = 0 |
CLS 5, 0 ' MAGENTA, split line = 0 |
||
Line 1,231: | Line 1,231: | ||
} |
} |
||
Pythagoras_Example |
Pythagoras_Example |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">n = 7; |
||
colors = Blend[{Orange, Yellow, Green}, #] & /@ Subdivide[n - 1]; |
colors = Blend[{Orange, Yellow, Green}, #] & /@ Subdivide[n - 1]; |
||
ClearAll[NextConfigs, NewConfig] |
ClearAll[NextConfigs, NewConfig] |
||
Line 1,250: | Line 1,250: | ||
config = <|"quads" -> {nc["quad"]}, "triangs" -> {nc["triang"]}|>; |
config = <|"quads" -> {nc["quad"]}, "triangs" -> {nc["triang"]}|>; |
||
config = NestList[NextConfigs, config, n - 1]; |
config = NestList[NextConfigs, config, n - 1]; |
||
Graphics[MapThread[{EdgeForm[Black], FaceForm[#2], #1["quads"], #1["triangs"]} &, {config, colors}]]</ |
Graphics[MapThread[{EdgeForm[Black], FaceForm[#2], #1["quads"], #1["triangs"]} &, {config, colors}]]</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
Line 1,256: | Line 1,256: | ||
{{libheader|imageman}} |
{{libheader|imageman}} |
||
Using Perl algorithm with some changes: background is black and there is no color variation according to depth. |
Using Perl algorithm with some changes: background is black and there is no color variation according to depth. |
||
< |
<syntaxhighlight lang="nim">import imageman |
||
const |
const |
||
Line 1,292: | Line 1,292: | ||
var image = initImage[ColorRGBU](Width, Height) |
var image = initImage[ColorRGBU](Width, Height) |
||
image.drawTree(int(Width / 2.3), Height - 1, int(Width / 1.8), Height - 1, MaxDepth) |
image.drawTree(int(Width / 2.3), Height - 1, int(Width / 1.8), Height - 1, MaxDepth) |
||
image.savePNG("pythagoras_tree.png", compression = 9)</ |
image.savePNG("pythagoras_tree.png", compression = 9)</syntaxhighlight> |
||
=={{header|Ol}}== |
=={{header|Ol}}== |
||
< |
<syntaxhighlight lang="scheme"> |
||
(import (lib gl)) |
(import (lib gl)) |
||
(import (OpenGL version-1-0)) |
(import (OpenGL version-1-0)) |
||
Line 1,338: | Line 1,338: | ||
)) |
)) |
||
)) |
)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
Line 1,350: | Line 1,350: | ||
{{Works with|PARI/GP|2.7.4 and above}} |
{{Works with|PARI/GP|2.7.4 and above}} |
||
< |
<syntaxhighlight lang="parigp">\\ Pythagoras Tree (w/recursion) |
||
\\ 4/11/16 aev |
\\ 4/11/16 aev |
||
plotline(x1,y1,x2,y2)={plotmove(0, x1,y1);plotrline(0,x2-x1,y2-y1);} |
plotline(x1,y1,x2,y2)={plotmove(0, x1,y1);plotrline(0,x2-x1,y2-y1);} |
||
Line 1,383: | Line 1,383: | ||
{\\ Executing: |
{\\ Executing: |
||
PythagorTree(275,500,375,500,9,640); \\PythTree1.png |
PythagorTree(275,500,375,500,9,640); \\PythTree1.png |
||
}</ |
}</syntaxhighlight> |
||
{{Output}} |
{{Output}} |
||
<pre> *** Pythagoras Tree, depth 9, size 640 |
<pre> *** Pythagoras Tree, depth 9, size 640 |
||
Line 1,390: | Line 1,390: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{trans|Sidef}} |
{{trans|Sidef}} |
||
< |
<syntaxhighlight lang="perl">use Imager; |
||
sub tree { |
sub tree { |
||
Line 1,436: | Line 1,436: | ||
$img->box(filled => 1, color => 'white'); |
$img->box(filled => 1, color => 'white'); |
||
tree($img, $width/2.3, $height, $width/1.8, $height, 10); |
tree($img, $width/2.3, $height, $width/1.8, $height, 10); |
||
$img->write(file => 'pythagoras_tree.png');</ |
$img->write(file => 'pythagoras_tree.png');</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 1,443: | Line 1,443: | ||
{{libheader|Phix/online}} |
{{libheader|Phix/online}} |
||
You can run this online [http://phix.x10.mx/p2js/PythagorasTree.htm here]. |
You can run this online [http://phix.x10.mx/p2js/PythagorasTree.htm here]. |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #000080;font-style:italic;">-- demo\rosetta\PythagorasTree.exw</span> |
<span style="color: #000080;font-style:italic;">-- demo\rosetta\PythagorasTree.exw</span> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
Line 1,528: | Line 1,528: | ||
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span> |
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
=={{header|Processing}}== |
=={{header|Processing}}== |
||
{{trans|Sidef}} |
{{trans|Sidef}} |
||
< |
<syntaxhighlight lang="java">void tree(float x1, float y1, float x2, float y2, int depth) { |
||
if (depth <= 0) { |
if (depth <= 0) { |
||
Line 1,576: | Line 1,576: | ||
stroke(0, 255, 0); |
stroke(0, 255, 0); |
||
tree(width/2.3, height, width/1.8, height, 10); |
tree(width/2.3, height, width/1.8, height, 10); |
||
}</ |
}</syntaxhighlight> |
||
==={{header|Processing Python mode}}=== |
==={{header|Processing Python mode}}=== |
||
< |
<syntaxhighlight lang="python">def setup(): |
||
size(800, 400) |
size(800, 400) |
||
background(255) |
background(255) |
||
Line 1,618: | Line 1,618: | ||
tree(x4, y4, x5, y5, depth - 1) |
tree(x4, y4, x5, y5, depth - 1) |
||
tree(x5, y5, x3, y3, depth - 1)</ |
tree(x5, y5, x3, y3, depth - 1)</syntaxhighlight> |
||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
{{trans|FreeBasic}} |
{{trans|FreeBasic}} |
||
< |
<syntaxhighlight lang="purebasic">EnableExplicit |
||
DisableDebugger |
DisableDebugger |
||
Line 1,682: | Line 1,682: | ||
Repeat : Until WaitWindowEvent(50)=#PB_Event_CloseWindow |
Repeat : Until WaitWindowEvent(50)=#PB_Event_CloseWindow |
||
EndIf |
EndIf |
||
End</ |
End</syntaxhighlight> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
Using [https://docs.python.org/3/library/turtle.html turtle graphics] and the Zkl example for the calculations. |
Using [https://docs.python.org/3/library/turtle.html turtle graphics] and the Zkl example for the calculations. |
||
< |
<syntaxhighlight lang="python">from turtle import goto, pu, pd, color, done |
||
def level(ax, ay, bx, by, depth=0): |
def level(ax, ay, bx, by, depth=0): |
||
Line 1,705: | Line 1,705: | ||
pu() |
pu() |
||
level(-100, 500, 100, 500, depth=8) |
level(-100, 500, 100, 500, depth=8) |
||
done()</ |
done()</syntaxhighlight> |
||
=={{header|R}}== |
=={{header|R}}== |
||
Line 1,712: | Line 1,712: | ||
[[File:PYTHTR9.png|200px|right|thumb|Output PYTHTR9.png]] |
[[File:PYTHTR9.png|200px|right|thumb|Output PYTHTR9.png]] |
||
[[File:PYTHTR7.png|200px|right|thumb|Output PYTHTR7.png]] |
[[File:PYTHTR7.png|200px|right|thumb|Output PYTHTR7.png]] |
||
< |
<syntaxhighlight lang="r">## Recursive PT plotting |
||
pythtree <- function(ax,ay,bx,by,d) { |
pythtree <- function(ax,ay,bx,by,d) { |
||
if(d<0) {return()}; clr="darkgreen"; |
if(d<0) {return()}; clr="darkgreen"; |
||
Line 1,743: | Line 1,743: | ||
## Executing: |
## Executing: |
||
pPythagorasT(275,500,375,500,9) |
pPythagorasT(275,500,375,500,9) |
||
pPythagorasT(275,500,375,500,7)</ |
pPythagorasT(275,500,375,500,7)</syntaxhighlight> |
||
{{Output}} |
{{Output}} |
||
<pre>> pPythagorasT(275,500,375,500,9) |
<pre>> pPythagorasT(275,500,375,500,9) |
||
Line 1,755: | Line 1,755: | ||
=={{header|QB64}}== |
=={{header|QB64}}== |
||
< |
<syntaxhighlight lang="qb64">_Title "Pythagoras Tree" |
||
Dim As Integer sw, sh |
Dim As Integer sw, sh |
||
Line 1,790: | Line 1,790: | ||
Call pythTree(ex, ey, dx, dy, depth + 1) |
Call pythTree(ex, ey, dx, dy, depth + 1) |
||
End If |
End If |
||
End Sub</ |
End Sub</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket">#lang racket |
||
(require racket/draw pict) |
(require racket/draw pict) |
||
Line 1,824: | Line 1,824: | ||
(send the-dc set-pen old-pen))) |
(send the-dc set-pen old-pen))) |
||
(dc (draw-pythagoras-tree 7 (+ 200 32) 255 (- 200 32) 255) 400 256)</ |
(dc (draw-pythagoras-tree 7 (+ 200 32) 255 (- 200 32) 255) 400 256)</syntaxhighlight> |
||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
We'll generate a SVG image. |
We'll generate a SVG image. |
||
<lang |
<syntaxhighlight lang="raku" line>class Square { |
||
has Complex ($.position, $.edge); |
has Complex ($.position, $.edge); |
||
method size { $!edge.abs } |
method size { $!edge.abs } |
||
Line 1,859: | Line 1,859: | ||
} |
} |
||
tree Square.new: :position(250+0i), :edge(60+0i);</ |
tree Square.new: :position(250+0i), :edge(60+0i);</syntaxhighlight> |
||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"># Project : Pythagoras tree |
||
load "guilib.ring" |
load "guilib.ring" |
||
Line 1,929: | Line 1,929: | ||
paint.drawline(x4,y4,x1,y1) |
paint.drawline(x4,y4,x1,y1) |
||
pythagorastree(x4, y4, x5, y5, depth +1) |
pythagorastree(x4, y4, x5, y5, depth +1) |
||
pythagorastree(x5, y5, x3, y3, depth +1)</ |
pythagorastree(x5, y5, x3, y3, depth +1)</syntaxhighlight> |
||
Output: |
Output: |
||
https://www.dropbox.com/s/a1gtue7tvmaj2je/PythagorasTree.jpg?dl=0 |
https://www.dropbox.com/s/a1gtue7tvmaj2je/PythagorasTree.jpg?dl=0 |
||
Line 1,937: | Line 1,937: | ||
{{libheader|JRubyArt}} |
{{libheader|JRubyArt}} |
||
A clone of processing version |
A clone of processing version |
||
< |
<syntaxhighlight lang="ruby"> |
||
# frozen_string_literal: true |
# frozen_string_literal: true |
||
Line 1,984: | Line 1,984: | ||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
Creates a [http://gist.githubusercontent.com/vvshard/833bd69acfa9160350cdbc9b57bbefe4/raw/pythagoras_tree.svg '''pythagoras_tree.svg file (12 levels)'''] that can be opened in a browser |
Creates a [http://gist.githubusercontent.com/vvshard/833bd69acfa9160350cdbc9b57bbefe4/raw/pythagoras_tree.svg '''pythagoras_tree.svg file (12 levels)'''] that can be opened in a browser |
||
< |
<syntaxhighlight lang="fsharp">/* add to file Cargo.toml: |
||
[dependencies] |
[dependencies] |
||
svg = "0.10.0" |
svg = "0.10.0" |
||
Line 2,018: | Line 2,018: | ||
Err(e) => println!("failed to write {file}: {e}"), |
Err(e) => println!("failed to write {file}: {e}"), |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
===Java Swing Interoperability=== |
===Java Swing Interoperability=== |
||
< |
<syntaxhighlight lang="scala">import java.awt._ |
||
import java.awt.geom.Path2D |
import java.awt.geom.Path2D |
||
Line 2,078: | Line 2,078: | ||
}) |
}) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Scilab}}== |
=={{header|Scilab}}== |
||
===L-System approach=== |
===L-System approach=== |
||
This solution uses complex numbers to represent vectors, and it draws the contour of the tree. By "uncommenting" the six commented lines inside the <code>select</code> structure, it will also draw the triangles between the squares. The output is a new graphic window. |
This solution uses complex numbers to represent vectors, and it draws the contour of the tree. By "uncommenting" the six commented lines inside the <code>select</code> structure, it will also draw the triangles between the squares. The output is a new graphic window. |
||
<lang>side = 1; //side length of the square |
<syntaxhighlight lang="text">side = 1; //side length of the square |
||
depth = 8; //final number of branch levels |
depth = 8; //final number of branch levels |
||
Line 2,174: | Line 2,174: | ||
plot2d(real(tree),imag(tree),14); |
plot2d(real(tree),imag(tree),14); |
||
set(gca(),'isoview','on'); |
set(gca(),'isoview','on'); |
||
set(gca(),'axes_visible',['off','off','off']);</ |
set(gca(),'axes_visible',['off','off','off']);</syntaxhighlight> |
||
===Recursive approach=== |
===Recursive approach=== |
||
A minor change was made so that the final depth of the tree is an argument of <code>fcn</code>, and not a condition set within itself. |
A minor change was made so that the final depth of the tree is an argument of <code>fcn</code>, and not a condition set within itself. |
||
{{trans|zkl}} |
{{trans|zkl}} |
||
<lang>function []=fcn(bitmap,ax,ay,bx,by,depth) |
<syntaxhighlight lang="text">function []=fcn(bitmap,ax,ay,bx,by,depth) |
||
if depth < 0 then |
if depth < 0 then |
||
return |
return |
||
Line 2,206: | Line 2,206: | ||
xname('Pythagoras tree: '+string(final_depth)+' levels'); |
xname('Pythagoras tree: '+string(final_depth)+' levels'); |
||
set(gca(),'isoview','on'); |
set(gca(),'isoview','on'); |
||
set(gca(),'axes_visible',['off','off','off']);</ |
set(gca(),'axes_visible',['off','off','off']);</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="ruby">require('Imager') |
||
func tree(img, x1, y1, x2, y2, depth) { |
func tree(img, x1, y1, x2, y2, depth) { |
||
Line 2,255: | Line 2,255: | ||
img.box(filled => 1, color => 'white') |
img.box(filled => 1, color => 'white') |
||
tree(img, width/2.3, height, width/1.8, height, 10) |
tree(img, width/2.3, height, width/1.8, height, 10) |
||
img.write(file => 'pythagoras_tree.png')</ |
img.write(file => 'pythagoras_tree.png')</syntaxhighlight> |
||
Output image: [https://github.com/trizen/rc/blob/master/img/pythagoras-tree-sidef.png Pythagoras tree] |
Output image: [https://github.com/trizen/rc/blob/master/img/pythagoras-tree-sidef.png Pythagoras tree] |
||
Line 2,262: | Line 2,262: | ||
{{libheader|DOME}} |
{{libheader|DOME}} |
||
{{libheader|Wren-polygon}} |
{{libheader|Wren-polygon}} |
||
< |
<syntaxhighlight lang="ecmascript">import "graphics" for Canvas, Color |
||
import "dome" for Window |
import "dome" for Window |
||
import "./polygon" for Polygon |
import "./polygon" for Polygon |
||
Line 2,314: | Line 2,314: | ||
} |
} |
||
var Game = PythagorasTree.new(640, 640)</ |
var Game = PythagorasTree.new(640, 640)</syntaxhighlight> |
||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
< |
<syntaxhighlight lang="yabasic">Sub pythagoras_tree(x1, y1, x2, y2, depth) |
||
local dx, dy, x3, y3, x4, y4, x5, y5 |
local dx, dy, x3, y3, x4, y4, x5, y5 |
||
Line 2,348: | Line 2,348: | ||
clear window |
clear window |
||
pythagoras_tree(w2 - diff, h -10 , w2 + diff , h -10 , 1)</ |
pythagoras_tree(w2 - diff, h -10 , w2 + diff , h -10 , 1)</syntaxhighlight> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
Line 2,355: | Line 2,355: | ||
Uses the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl |
Uses the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl |
||
[[File:PythagorasTreeWithLeafs.zkl.jpg|300px|thumb|right]] |
[[File:PythagorasTreeWithLeafs.zkl.jpg|300px|thumb|right]] |
||
< |
<syntaxhighlight lang="zkl">fcn pythagorasTree{ |
||
bitmap:=PPM(640,640,0xFF|FF|FF); // White background |
bitmap:=PPM(640,640,0xFF|FF|FF); // White background |
||
Line 2,373: | Line 2,373: | ||
bitmap.writeJPGFile("pythagorasTree.jpg",True); |
bitmap.writeJPGFile("pythagorasTree.jpg",True); |
||
}();</ |
}();</syntaxhighlight> |