Jump to content

Pythagoras tree: Difference between revisions

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

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.