Data update

This commit is contained in:
Ingy döt Net 2026-02-01 16:33:20 -08:00
parent 5150844a7d
commit 4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions

View file

@ -1,2 +1,3 @@
---
from: http://rosettacode.org/wiki/Pythagoras_tree
note: Fractals

View file

@ -1,86 +0,0 @@
with SDL.Video.Windows.Makers;
with SDL.Video.Renderers.Makers;
with SDL.Video.Rectangles;
with SDL.Events.Events;
procedure Pythagoras_Tree is
Width : constant := 600;
Height : constant := 600;
Level : constant := 7;
type Point is record X, Y : Float; end record;
B1 : constant Point := (X => 250.0, Y => 550.0);
B2 : constant Point := (X => 350.0, Y => 550.0);
Window : SDL.Video.Windows.Window;
Renderer : SDL.Video.Renderers.Renderer;
Event : SDL.Events.Events.Events;
procedure Draw_Pythagoras_Tree (Level : in Natural;
P1, P2 : in Point)
is
use SDL.Video.Rectangles;
Dx : constant Float := P2.X - P1.X;
Dy : constant Float := P1.Y - P2.Y;
R : constant Point := (X => P2.X - Dy, Y => P2.Y - Dx);
L : constant Point := (X => P1.X - Dy, Y => P1.Y - Dx);
M : constant Point := (X => L.X + (Dx - Dy) / 2.0,
Y => L.Y - (Dx + Dy) / 2.0);
CP1 : constant SDL.Video.Rectangles.Point := (C.int (P1.X), C.int (P1.Y));
CP2 : constant SDL.Video.Rectangles.Point := (C.int (P2.X), C.int (P2.Y));
CL : constant SDL.Video.Rectangles.Point := (C.int (L.X), C.int (L.Y));
CR : constant SDL.Video.Rectangles.Point := (C.int (R.X), C.int (R.Y));
CM : constant SDL.Video.Rectangles.Point := (C.int (M.X), C.int (M.Y));
Square : constant SDL.Video.Rectangles.Line_Arrays :=
((CP1, CP2), (CP2, CR), (CR, CL), (CL, CP1));
Triang : constant SDL.Video.Rectangles.Line_Arrays :=
((CR, CL), (CL, CM), (CM, CR));
begin
if Level > 0 then
Renderer.Set_Draw_Colour (Colour => (0, 220, 0, 255));
Renderer.Draw (Lines => Square);
Renderer.Draw (Lines => Triang);
Draw_Pythagoras_Tree (Level - 1, L, M);
Draw_Pythagoras_Tree (Level - 1, M, R);
end if;
end Draw_Pythagoras_Tree;
procedure Wait is
use type SDL.Events.Event_Types;
begin
loop
while SDL.Events.Events.Poll (Event) loop
if Event.Common.Event_Type = SDL.Events.Quit then
return;
end if;
end loop;
delay 0.100;
end loop;
end Wait;
begin
if not SDL.Initialise (Flags => SDL.Enable_Screen) then
return;
end if;
SDL.Video.Windows.Makers.Create (Win => Window,
Title => "Pythagoras tree",
Position => SDL.Natural_Coordinates'(X => 10, Y => 10),
Size => SDL.Positive_Sizes'(Width, Height),
Flags => 0);
SDL.Video.Renderers.Makers.Create (Renderer, Window.Get_Surface);
Renderer.Set_Draw_Colour ((0, 0, 0, 255));
Renderer.Fill (Rectangle => (0, 0, Width, Height));
Renderer.Set_Draw_Colour ((0, 220, 0, 255));
Draw_Pythagoras_Tree (Level, B1, B2);
Window.Update_Surface;
Wait;
Window.Finalize;
SDL.Finalise;
end Pythagoras_Tree;

View file

@ -28,7 +28,7 @@ Pythagoras_tree(x1, y1, x2, y2, depth){
; draw box/triangle
Gdip_FillPolygon(G, Brush%depth%, x1 "," y1 "|" x2 "," y2 "|" x3 "," y3 "|" x4 "," y4 "|" x1 "," y1)
Gdip_FillPolygon(G, Brush%depth%, x4 "," y4 "|" x5 "," y5 "|" x3 "," y3 "|" x4 "," y4)
; draw outline
Gdip_DrawLines(G, Pen, x1 "," y1 "|" x2 "," y2 "|" x3 "," y3 "|" x4 "," y4 "|" x1 "," y1)
Gdip_DrawLines(G, Pen, x4 "," y4 "|" x5 "," y5 "|" x3 "," y3 "|" x4 "," y4)

View file

@ -1,17 +1,17 @@
Subroutine pythagoras_tree(x1, y1, x2, y2, depth)
If depth > 10 Then Return
If depth > 10 Then Return
dx = x2 - x1 : dy = y1 - y2
x3 = x2 - dy : y3 = y2 - dx
x4 = x1 - dy : y4 = y1 - dx
x5 = x4 + (dx - dy) / 2
y5 = y4 - (dx + dy) / 2
#draw the box
Line x1, y1, x2, y2 : Line x2, y2, x3, y3
Line x3, y3, x4, y4 : Line x4, y4, x1, y1
dx = x2 - x1 : dy = y1 - y2
x3 = x2 - dy : y3 = y2 - dx
x4 = x1 - dy : y4 = y1 - dx
x5 = x4 + (dx - dy) / 2
y5 = y4 - (dx + dy) / 2
#draw the box
Line x1, y1, x2, y2 : Line x2, y2, x3, y3
Line x3, y3, x4, y4 : Line x4, y4, x1, y1
Call pythagoras_tree(x4, y4, x5, y5, depth +1)
Call pythagoras_tree(x5, y5, x3, y3, depth +1)
Call pythagoras_tree(x4, y4, x5, y5, depth +1)
Call pythagoras_tree(x5, y5, x3, y3, depth +1)
End Subroutine
w = 800 : h = w * 11 \ 16

View file

@ -4,64 +4,64 @@
#include<time.h>
typedef struct{
double x,y;
double x,y;
}point;
void pythagorasTree(point a,point b,int times){
point c,d,e;
c.x = b.x - (a.y - b.y);
c.y = b.y - (b.x - a.x);
d.x = a.x - (a.y - b.y);
d.y = a.y - (b.x - a.x);
e.x = d.x + ( b.x - a.x - (a.y - b.y) ) / 2;
e.y = d.y - ( b.x - a.x + a.y - b.y ) / 2;
if(times>0){
setcolor(rand()%15 + 1);
line(a.x,a.y,b.x,b.y);
line(c.x,c.y,b.x,b.y);
line(c.x,c.y,d.x,d.y);
line(a.x,a.y,d.x,d.y);
pythagorasTree(d,e,times-1);
pythagorasTree(e,c,times-1);
}
point c,d,e;
c.x = b.x - (a.y - b.y);
c.y = b.y - (b.x - a.x);
d.x = a.x - (a.y - b.y);
d.y = a.y - (b.x - a.x);
e.x = d.x + ( b.x - a.x - (a.y - b.y) ) / 2;
e.y = d.y - ( b.x - a.x + a.y - b.y ) / 2;
if(times>0){
setcolor(rand()%15 + 1);
line(a.x,a.y,b.x,b.y);
line(c.x,c.y,b.x,b.y);
line(c.x,c.y,d.x,d.y);
line(a.x,a.y,d.x,d.y);
pythagorasTree(d,e,times-1);
pythagorasTree(e,c,times-1);
}
}
int main(){
point a,b;
double side;
int iter;
time_t t;
printf("Enter initial side length : ");
scanf("%lf",&side);
printf("Enter number of iterations : ");
scanf("%d",&iter);
a.x = 6*side/2 - side/2;
a.y = 4*side;
b.x = 6*side/2 + side/2;
b.y = 4*side;
initwindow(6*side,4*side,"Pythagoras Tree ?");
srand((unsigned)time(&t));
pythagorasTree(a,b,iter);
getch();
closegraph();
return 0;
point a,b;
double side;
int iter;
time_t t;
printf("Enter initial side length : ");
scanf("%lf",&side);
printf("Enter number of iterations : ");
scanf("%d",&iter);
a.x = 6*side/2 - side/2;
a.y = 4*side;
b.x = 6*side/2 + side/2;
b.y = 4*side;
initwindow(6*side,4*side,"Pythagoras Tree ?");
srand((unsigned)time(&t));
pythagorasTree(a,b,iter);
getch();
closegraph();
return 0;
}

View file

@ -8,7 +8,7 @@ proc tree x1 y1 x2 y2 depth .
y4 = y1 + dx
x5 = x4 + 0.5 * (dx + dy)
y5 = y4 + 0.5 * (dx - dy)
gcolor3 30 20 + depth * 6 10
gcolor3 0.3 0.2 + depth * 0.06 0.1
gpolygon [ x1 y1 x2 y2 x3 y3 x4 y4 ]
gpolygon [ x3 y3 x4 y4 x5 y5 ]
tree x4 y4 x5 y5 depth + 1

View file

@ -1,86 +1,86 @@
package main
import (
"image"
"image/color"
"image/draw"
"image/png"
"log"
"os"
"image"
"image/color"
"image/draw"
"image/png"
"log"
"os"
)
const (
width, height = 800, 600
maxDepth = 11 // how far to recurse, between 1 and 20 is reasonable
colFactor = uint8(255 / maxDepth) // adjusts the colour so leaves get greener further out
fileName = "pythagorasTree.png"
width, height = 800, 600
maxDepth = 11 // how far to recurse, between 1 and 20 is reasonable
colFactor = uint8(255 / maxDepth) // adjusts the colour so leaves get greener further out
fileName = "pythagorasTree.png"
)
func main() {
img := image.NewNRGBA(image.Rect(0, 0, width, height)) // create new image
bg := image.NewUniform(color.RGBA{255, 255, 255, 255}) // prepare white for background
draw.Draw(img, img.Bounds(), bg, image.ZP, draw.Src) // fill the background
img := image.NewNRGBA(image.Rect(0, 0, width, height)) // create new image
bg := image.NewUniform(color.RGBA{255, 255, 255, 255}) // prepare white for background
draw.Draw(img, img.Bounds(), bg, image.ZP, draw.Src) // fill the background
drawSquares(340, 550, 460, 550, img, 0) // start off near the bottom of the image
drawSquares(340, 550, 460, 550, img, 0) // start off near the bottom of the image
imgFile, err := os.Create(fileName)
if err != nil {
log.Fatal(err)
}
defer imgFile.Close()
if err := png.Encode(imgFile, img); err != nil {
imgFile.Close()
log.Fatal(err)
}
imgFile, err := os.Create(fileName)
if err != nil {
log.Fatal(err)
}
defer imgFile.Close()
if err := png.Encode(imgFile, img); err != nil {
imgFile.Close()
log.Fatal(err)
}
}
func drawSquares(ax, ay, bx, by int, img *image.NRGBA, depth int) {
if depth > maxDepth {
return
}
dx, dy := bx-ax, ay-by
x3, y3 := bx-dy, by-dx
x4, y4 := ax-dy, ay-dx
x5, y5 := x4+(dx-dy)/2, y4-(dx+dy)/2
col := color.RGBA{0, uint8(depth) * colFactor, 0, 255}
drawLine(ax, ay, bx, by, img, col)
drawLine(bx, by, x3, y3, img, col)
drawLine(x3, y3, x4, y4, img, col)
drawLine(x4, y4, ax, ay, img, col)
drawSquares(x4, y4, x5, y5, img, depth+1)
drawSquares(x5, y5, x3, y3, img, depth+1)
if depth > maxDepth {
return
}
dx, dy := bx-ax, ay-by
x3, y3 := bx-dy, by-dx
x4, y4 := ax-dy, ay-dx
x5, y5 := x4+(dx-dy)/2, y4-(dx+dy)/2
col := color.RGBA{0, uint8(depth) * colFactor, 0, 255}
drawLine(ax, ay, bx, by, img, col)
drawLine(bx, by, x3, y3, img, col)
drawLine(x3, y3, x4, y4, img, col)
drawLine(x4, y4, ax, ay, img, col)
drawSquares(x4, y4, x5, y5, img, depth+1)
drawSquares(x5, y5, x3, y3, img, depth+1)
}
func drawLine(x0, y0, x1, y1 int, img *image.NRGBA, col color.RGBA) {
dx := abs(x1 - x0)
dy := abs(y1 - y0)
var sx, sy int = -1, -1
if x0 < x1 {
sx = 1
}
if y0 < y1 {
sy = 1
}
err := dx - dy
for {
img.Set(x0, y0, col)
if x0 == x1 && y0 == y1 {
break
}
e2 := 2 * err
if e2 > -dy {
err -= dy
x0 += sx
}
if e2 < dx {
err += dx
y0 += sy
}
}
dx := abs(x1 - x0)
dy := abs(y1 - y0)
var sx, sy int = -1, -1
if x0 < x1 {
sx = 1
}
if y0 < y1 {
sy = 1
}
err := dx - dy
for {
img.Set(x0, y0, col)
if x0 == x1 && y0 == y1 {
break
}
e2 := 2 * err
if e2 > -dy {
err -= dy
x0 += sx
}
if e2 < dx {
err += dx
y0 += sy
}
}
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
if x < 0 {
return -x
}
return x
}

View file

@ -37,14 +37,14 @@ def PythagorasTree:
else ($x2 - $x1) as $dx
| ($y1 - $y2) as $dy
| ($x2 - $dy) as $x3
| ($y2 - $dx) as $y3
| ($y2 - $dx) as $y3
| ($x1 - $dy) as $x4
| ($y1 - $dx) as $y4
| ($x4 + 0.5 * ($dx - $dy)) as $x5
| ($x4 + 0.5 * ($dx - $dy)) as $x5
| ($y4 - 0.5 * ($dx + $dy)) as $y5
# draw a square
| "rgb(\(256 - $depth * 20), 0, 0)" as $col
| "rgb(\(256 - $depth * 20), 0, 0)" as $col
| Square([$x1, $y1]; [$x2, $y2]; [$x3, $y3] ; [$x4, $y4] ; $col; "lightgray")
# draw a triangle

View file

@ -1,29 +1,29 @@
MODULE Pythagoras_tree {
CLS 5, 0 ' MAGENTA, NO SPLIT SCREEN
PEN 14 ' YELLOW
\\ code from zkl/Free Basic
LET w = scale.x, h = w * 11 div 16
LET w2 = w div 2, diff = w div 12
LET TreeOrder = 6
pythagoras_tree(w2 - diff, h -10, w2 + diff, h -10, 0)
SUB pythagoras_tree(x1, y1, x2, y2, depth)
IF depth > TreeOrder THEN EXIT SUB
LOCAL dx = x2 - x1, dy = y1 - y2
LOCAL x3 = x2 - dy, y3 = y2 - dx
LOCAL x4 = x1 - dy, y4 = y1 - dx
LOCAL x5 = x4 + (dx - dy) / 2
LOCAL y5 = y4 - (dx + dy) / 2
MOVE x1, y1
DRAW TO x2, y2
DRAW TO x3, y3
DRAW TO x4, y4
DRAW TO x1, y1
pythagoras_tree(x4, y4, x5, y5, depth +1)
pythagoras_tree(x5, y5, x3, y3, depth +1)
END SUB
CLS 5, 0 ' MAGENTA, NO SPLIT SCREEN
PEN 14 ' YELLOW
\\ code from zkl/Free Basic
LET w = scale.x, h = w * 11 div 16
LET w2 = w div 2, diff = w div 12
LET TreeOrder = 6
pythagoras_tree(w2 - diff, h -10, w2 + diff, h -10, 0)
SUB pythagoras_tree(x1, y1, x2, y2, depth)
IF depth > TreeOrder THEN EXIT SUB
LOCAL dx = x2 - x1, dy = y1 - y2
LOCAL x3 = x2 - dy, y3 = y2 - dx
LOCAL x4 = x1 - dy, y4 = y1 - dx
LOCAL x5 = x4 + (dx - dy) / 2
LOCAL y5 = y4 - (dx + dy) / 2
MOVE x1, y1
DRAW TO x2, y2
DRAW TO x3, y3
DRAW TO x4, y4
DRAW TO x1, y1
pythagoras_tree(x4, y4, x5, y5, depth +1)
pythagoras_tree(x5, y5, x3, y3, depth +1)
END SUB
}
Pythagoras_tree

View file

@ -1,44 +1,44 @@
MODULE Pythagoras_Example{
CLS 5, 0 ' MAGENTA, split line = 0
PEN 14 ' YELLOW
\\ Linux smoothing not work (we can use the statement but without effect)
IF ISWINE ELSE SMOOTH ON
\\ PYTHAGORAS TREE
\\ by definition all variables ar type of a double
GLOBAL p=7, p4=PI/4, p2=PI/2, s2=SQRT(2)/2
MODULE center_p (r, t){
MODULE pythagoras_tree (r, dx, depth) {
r2=r-p2
DRAW ANGLE r, dx
DRAW ANGLE r2, dx
DRAW ANGLE r, -dx
DRAW ANGLE r2, -dx
IF depth>10 THEN EXIT
s3=dx*s2
depth++
STEP ANGLE r+p4, s3*2
CALL pythagoras_tree r-p4, s3, depth
STEP ANGLE r, -dx-s3
STEP ANGLE r, s3
STEP ANGLE r+p4, -s3
CALL pythagoras_tree r+p4, s3, depth
STEP ANGLE r-p4, s3
}
MOVE SCALE.X/2, SCALE.Y/2
STEP ANGLE PI-p4+r, t*s2
CALL pythagoras_tree r, t, 1
}
r=PI/3
pixels=100
center_p r, 100*TWIPSX
center_p r+PI, 100*TWIPSX
CopyImageToClipboard()
Sub CopyImageToClipboard()
LOCAL Scr$=""
MOVE 0,0
COPY SCALE.X, SCALE.Y TO Scr$
CLIPBOARD Scr$
END SUB
CLS 5, 0 ' MAGENTA, split line = 0
PEN 14 ' YELLOW
\\ Linux smoothing not work (we can use the statement but without effect)
IF ISWINE ELSE SMOOTH ON
\\ PYTHAGORAS TREE
\\ by definition all variables ar type of a double
GLOBAL p=7, p4=PI/4, p2=PI/2, s2=SQRT(2)/2
MODULE center_p (r, t){
MODULE pythagoras_tree (r, dx, depth) {
r2=r-p2
DRAW ANGLE r, dx
DRAW ANGLE r2, dx
DRAW ANGLE r, -dx
DRAW ANGLE r2, -dx
IF depth>10 THEN EXIT
s3=dx*s2
depth++
STEP ANGLE r+p4, s3*2
CALL pythagoras_tree r-p4, s3, depth
STEP ANGLE r, -dx-s3
STEP ANGLE r, s3
STEP ANGLE r+p4, -s3
CALL pythagoras_tree r+p4, s3, depth
STEP ANGLE r-p4, s3
}
MOVE SCALE.X/2, SCALE.Y/2
STEP ANGLE PI-p4+r, t*s2
CALL pythagoras_tree r, t, 1
}
r=PI/3
pixels=100
center_p r, 100*TWIPSX
center_p r+PI, 100*TWIPSX
CopyImageToClipboard()
Sub CopyImageToClipboard()
LOCAL Scr$=""
MOVE 0,0
COPY SCALE.X, SCALE.Y TO Scr$
CLIPBOARD Scr$
END SUB
}
Pythagoras_Example

View file

@ -2,19 +2,19 @@ class Square {
has Complex ($.position, $.edge);
method size { $!edge.abs }
method svg-polygon {
qq[<polygon points="{join ' ', map]
{ ($!position + $_ * $!edge).reals.join(',') },
0, 1, 1+1i, 1i}" style="fill:lime;stroke=black" />]
qq[<polygon points="{join ' ', map]
{ ($!position + $_ * $!edge).reals.join(',') },
0, 1, 1+1i, 1i}" style="fill:lime;stroke=black" />]
}
method left-child {
self.new:
position => $!position + i*$!edge,
edge => sqrt(2)/2*cis(pi/4)*$!edge;
self.new:
position => $!position + i*$!edge,
edge => sqrt(2)/2*cis(pi/4)*$!edge;
}
method right-child {
self.new:
position => $!position + i*$!edge + self.left-child.edge,
edge => sqrt(2)/2*cis(-pi/4)*$!edge;
self.new:
position => $!position + i*$!edge + self.left-child.edge,
edge => sqrt(2)/2*cis(-pi/4)*$!edge;
}
}