Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
2
Task/Simple-turtle-graphics/00-META.yaml
Normal file
2
Task/Simple-turtle-graphics/00-META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Simple_turtle_graphics
|
||||
13
Task/Simple-turtle-graphics/00-TASK.txt
Normal file
13
Task/Simple-turtle-graphics/00-TASK.txt
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
The first turtle graphic discussed in [https://mindstorms.media.mit.edu Mindstorms: Children, Computers, and Powerful Ideas] by Seymour Papert is a simple drawing of a house. It is a square with a triangle on top for the roof.
|
||||
|
||||
For a slightly more advanced audience, a more practical introduction to turtle graphics might be to draw a bar chart.
|
||||
|
||||
[[File:Turtle house and bar chart.png|thumb]]
|
||||
|
||||
;Task:
|
||||
::* Create a function (or subroutine) that uses turtle graphics to draw a house of a specified size as depicted. Optionally make it lovely by adding details such as, for example, doors and windows.
|
||||
|
||||
::* Create a function (or subroutine) that takes a list (array, vector) of non-negative numbers and draws a bar chart from them, scaled to fit exactly in a square of a specified size. The enclosing square need not be drawn.
|
||||
|
||||
::* Both functions should return the turtle to the location it was at and facing in the same direction as it was immediately before the function was executed.
|
||||
|
||||
|
|
@ -0,0 +1,89 @@
|
|||
INCLUDE "D2:TURTLE.ACT" ;from the Action! Tool Kit
|
||||
|
||||
PROC Rectangle(INT w,h)
|
||||
BYTE i
|
||||
|
||||
FOR i=1 TO 2
|
||||
DO
|
||||
Forward(h)
|
||||
Left(90)
|
||||
Forward(w)
|
||||
Left(90)
|
||||
OD
|
||||
RETURN
|
||||
|
||||
PROC Square(INT w)
|
||||
Rectangle(w,w)
|
||||
RETURN
|
||||
|
||||
PROC Triangle(INT w)
|
||||
BYTE i
|
||||
|
||||
FOR i=1 TO 3
|
||||
DO
|
||||
Forward(w)
|
||||
Right(120)
|
||||
OD
|
||||
RETURN
|
||||
|
||||
PROC House(INT w)
|
||||
Left(90)
|
||||
Square(w)
|
||||
Triangle(w)
|
||||
Right(90)
|
||||
RETURN
|
||||
|
||||
INT FUNC GetMax(INT ARRAY a INT count)
|
||||
INT i,max
|
||||
|
||||
max=0
|
||||
FOR i=0 TO count-1
|
||||
DO
|
||||
IF a(i)>max THEN
|
||||
max=a(i)
|
||||
FI
|
||||
OD
|
||||
RETURN (max)
|
||||
|
||||
PROC BarChart(INT ARRAY a INT count,w)
|
||||
INT max,st,i
|
||||
|
||||
IF count=0 THEN RETURN FI
|
||||
max=GetMax(a,count)
|
||||
st=w/count
|
||||
Right(90)
|
||||
FOR i=0 TO count-1
|
||||
DO
|
||||
Rectangle(a(i)*w/max,st)
|
||||
Forward(st)
|
||||
OD
|
||||
Left(180)
|
||||
Forward(w)
|
||||
RETURN
|
||||
|
||||
PROC Main()
|
||||
BYTE CH=$02FC,COLOR1=$02C5,COLOR2=$02C6
|
||||
INT ARRAY a=[50 33 200 130 50]
|
||||
|
||||
Graphics(8+16)
|
||||
COLOR1=$0C
|
||||
COLOR2=$02
|
||||
|
||||
Color=1
|
||||
SetTurtle(150,110,90)
|
||||
House(75)
|
||||
|
||||
Color=0
|
||||
Right(90)
|
||||
Forward(5)
|
||||
Left(90)
|
||||
|
||||
Color=1
|
||||
BarChart(a,5,100)
|
||||
Right(90)
|
||||
Forward(5)
|
||||
Right(90)
|
||||
|
||||
DO UNTIL CH#$FF OD
|
||||
CH=$FF
|
||||
RETURN
|
||||
169
Task/Simple-turtle-graphics/Ada/simple-turtle-graphics.ada
Normal file
169
Task/Simple-turtle-graphics/Ada/simple-turtle-graphics.ada
Normal file
|
|
@ -0,0 +1,169 @@
|
|||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
with Ada.Characters; use Ada.Characters;
|
||||
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
|
||||
with Ada.Strings; use Ada.Strings;
|
||||
|
||||
-- procedure main - begins program execution
|
||||
procedure main is
|
||||
type Sketch_Pad is array(1 .. 50, 1 .. 50) of Character;
|
||||
thePen : Boolean := True; -- pen raised by default
|
||||
sketch : Sketch_Pad;
|
||||
ycorr, xcorr : Integer := 25;
|
||||
|
||||
-- specifications
|
||||
function penPosition(thePen : in out Boolean) return String;
|
||||
procedure initGrid(sketch : in out Sketch_Pad);
|
||||
procedure commandMenu(thePen : in out Boolean; xcorr : in out Integer;
|
||||
ycorr : in out Integer);
|
||||
procedure showMenu(xcorr : in out Integer; ycorr : in out Integer;
|
||||
thePen : in out Boolean; sketch : in Sketch_Pad);
|
||||
procedure moveCursor(thePen : in Boolean; sketch : in out Sketch_Pad;
|
||||
xcorr : in out Integer; ycorr : in out Integer;
|
||||
ch : in Integer);
|
||||
procedure showGrid(sketch : in Sketch_Pad);
|
||||
|
||||
-- procedure initGrid - creates the sketchpad and initializes elements
|
||||
procedure initGrid(sketch : in out Sketch_Pad) is
|
||||
begin
|
||||
sketch := (others => (others => ' '));
|
||||
end initGrid;
|
||||
|
||||
-- procedure showMenu - displays the menu for the application
|
||||
procedure showMenu(xcorr : in out Integer; ycorr : in out Integer;
|
||||
thePen : in out Boolean; sketch : in Sketch_Pad) is
|
||||
|
||||
choice : Integer := 0;
|
||||
begin
|
||||
while choice /= 4 loop
|
||||
Set_Col(15);
|
||||
Put("TURTLE GRAPHICS APPLICATION");
|
||||
Set_Col(15);
|
||||
Put("===========================");
|
||||
New_Line(2);
|
||||
|
||||
Put_Line("Enter 1 to print the grid map");
|
||||
Put_Line("Enter 2 for command menu");
|
||||
Put_Line("Enter 3 to raise pen up / down");
|
||||
Put_Line("Enter 4 to exit the application");
|
||||
choice := integer'value(Get_Line);
|
||||
|
||||
exit when choice = 4;
|
||||
|
||||
case choice is
|
||||
when 1 => showGrid(sketch);
|
||||
when 2 => commandMenu(thePen, xcorr, ycorr);
|
||||
when 3 => Put_Line("Pen is "
|
||||
& penPosition(thePen));
|
||||
when others => Put_Line("Invalid input");
|
||||
end case;
|
||||
end loop;
|
||||
end showMenu;
|
||||
|
||||
-- function penPosition - checks changes the state of whether the pen is
|
||||
-- raised up or down. If value is True, pen is rasied up
|
||||
function penPosition(thePen : in out Boolean) return String is
|
||||
str1 : constant String := "raised UP";
|
||||
str2 : constant String := "raised DOWN";
|
||||
begin
|
||||
if thePen = True then
|
||||
thePen := False;
|
||||
return str2;
|
||||
else
|
||||
thePen := True;
|
||||
end if;
|
||||
|
||||
return str1;
|
||||
end penPosition;
|
||||
|
||||
-- procedure command menu - provides a list of directions for the turtle
|
||||
-- to move along the grid
|
||||
procedure commandMenu(thePen : in out Boolean; xcorr : in out Integer;
|
||||
ycorr : in out Integer) is
|
||||
|
||||
choice : Integer := 0;
|
||||
begin
|
||||
while choice <= 0 or choice > 5 loop
|
||||
Set_Col(15);
|
||||
Put("Command Menu");
|
||||
Set_Col(15);
|
||||
Put("============");
|
||||
New_Line(2);
|
||||
|
||||
Put_Line("To move North enter 1");
|
||||
Put_Line("To move South enter 2");
|
||||
Put_Line("To move East enter 3");
|
||||
Put_Line("To move West enter 4");
|
||||
Put_Line("To return to previous menu enter 5");
|
||||
choice := integer'value(Get_Line);
|
||||
|
||||
case choice is
|
||||
when 1 => moveCursor(thePen, sketch, xcorr, ycorr, choice);
|
||||
when 2 => moveCursor(thePen, sketch, xcorr, ycorr, choice);
|
||||
when 3 => moveCursor(thePen, sketch, xcorr, ycorr, choice);
|
||||
when 4 => moveCursor(thePen, sketch, xcorr, ycorr, choice);
|
||||
when 5 => showMenu(xcorr, ycorr, thePen, sketch);
|
||||
when others => Put_Line("Invalid choice");
|
||||
end case;
|
||||
end loop;
|
||||
end commandMenu;
|
||||
|
||||
|
||||
-- procedure moveCursor - moves the cursor around the board by taking the
|
||||
-- x and y coordinates from the user. If the pen is down, a character is
|
||||
-- printed at that location. If the pen is up, nothing is printed but the
|
||||
-- cursor still moves to that position
|
||||
procedure moveCursor(thePen : in Boolean; sketch : in out Sketch_Pad;
|
||||
xcorr : in out Integer; ycorr : in out Integer;
|
||||
ch : in Integer) is
|
||||
|
||||
begin
|
||||
if thePen = True then -- pen up so move cursor but do not draw
|
||||
case ch is
|
||||
when 1 => xcorr := xcorr - 1; ycorr := ycorr;
|
||||
sketch(xcorr, ycorr) := ' ';
|
||||
when 2 => xcorr := xcorr + 1; ycorr := ycorr;
|
||||
sketch(xcorr, ycorr) := ' ';
|
||||
when 3 => xcorr := xcorr; ycorr := ycorr + 1;
|
||||
sketch(xcorr, ycorr) := ' ';
|
||||
when 4 => xcorr := xcorr; ycorr := ycorr - 1;
|
||||
sketch(xcorr, ycorr) := ' ';
|
||||
when others => Put("Unreachable Code");
|
||||
end case;
|
||||
|
||||
else -- pen is down so move cursor and draw
|
||||
case ch is
|
||||
when 1 => xcorr := xcorr - 1; ycorr := ycorr;
|
||||
sketch(xcorr, ycorr) := '#';
|
||||
when 2 => xcorr := xcorr + 1; ycorr := ycorr;
|
||||
sketch(xcorr, ycorr) := '#';
|
||||
when 3 => xcorr := xcorr; ycorr := ycorr + 1;
|
||||
sketch(xcorr, ycorr) := '#';
|
||||
when 4 => xcorr := xcorr; ycorr := ycorr - 1;
|
||||
sketch(xcorr, ycorr) := '#';
|
||||
when others => Put("Unreachable Code");
|
||||
end case;
|
||||
end if;
|
||||
end moveCursor;
|
||||
|
||||
-- procedure showGrid - prints the sketchpad showing the plotted moves
|
||||
procedure showGrid(sketch : in Sketch_Pad) is
|
||||
begin
|
||||
New_Line;
|
||||
|
||||
for I in sketch'Range(1) loop
|
||||
for J in sketch'Range(2) loop
|
||||
Put(character'image(sketch(I,J)));
|
||||
end loop;
|
||||
New_Line;
|
||||
end loop;
|
||||
New_Line;
|
||||
end showGrid;
|
||||
|
||||
begin
|
||||
New_Line;
|
||||
|
||||
initGrid(sketch);
|
||||
showMenu(xcorr, ycorr, thePen, sketch);
|
||||
|
||||
New_Line;
|
||||
end main;
|
||||
|
|
@ -0,0 +1,115 @@
|
|||
' Turtle Graphics Library by Angelo Rosina 1995
|
||||
' Ported to Freebasic by Angelo Rosina 2007
|
||||
'
|
||||
'(RIGH and LEF because RIGHT and LEFT are reserved keywords, of course)
|
||||
|
||||
Declare Sub LEF (gradi As Integer)
|
||||
Declare Sub SETCOL (C As Integer)
|
||||
Declare Sub SETSIZE (C As Integer)
|
||||
Declare Sub RIGH (gradi As Integer)
|
||||
Declare Sub FORW (passi As Integer)
|
||||
Declare Sub BACK (passi As Integer)
|
||||
Declare Sub TURTLE (numero As Integer)
|
||||
Declare Sub UP ()
|
||||
Declare Sub DOWN ()
|
||||
Declare Sub DTurtle ()
|
||||
|
||||
Dim Shared As Integer graph, dimen, angulo, N, dis, su, pasos
|
||||
Dim Shared As Integer colore(10), figura(5000,10), figura2()
|
||||
Dim Shared As Long X(10), Y(10)
|
||||
Dim Shared As String comando
|
||||
|
||||
Sub BACK (passi As Integer)
|
||||
pasos = passi
|
||||
comando = "d " & Str(passi)
|
||||
DTurtle
|
||||
comando = ""
|
||||
pasos = 0
|
||||
End Sub
|
||||
|
||||
Sub DOWN
|
||||
su = 0
|
||||
End Sub
|
||||
|
||||
Sub DTurtle Static
|
||||
Dim As Integer rifa, lavora, XX, YY, scher(), FX, FY
|
||||
Dim As String R
|
||||
|
||||
If rifa = 0 Then rifa = 1: Redim scher(5000)
|
||||
If lavora Then Put (XX - dimen * 4, YY - dimen * 2), scher, Pset
|
||||
'SCREEN graph
|
||||
If su Then comando = "b" & comando
|
||||
FX = X(N)
|
||||
FY = Y(N)
|
||||
R = "s" & Str(dimen) & "bm" & Str(FX) & "," & Str(FY) & "c" & Str(colore(N)) & "Ta" & Str(angulo) & comando
|
||||
Draw R
|
||||
X(N) = X(N) + Sin(angulo / 57.2957) * dimen / 4 * pasos
|
||||
Y(N) = Y(N) + Cos(angulo / 57.2957) * dimen / 4 * pasos
|
||||
XX = X(N)
|
||||
YY = Y(N)
|
||||
Get (X(N) - dimen * 4, Y(N) - dimen * 2)-(X(N) + dimen * 4, Y(N) + dimen * 2), scher: lavora = 1
|
||||
If dis = 0 Then
|
||||
R = "bm" & Str(XX) & "," & Str(YY) & "Ta" & Str(angulo) & "r1 ta " & Str(angulo + 15 + 360 * (angulo + 15 > 360)) & "u7 ta " & Str(angulo + 165 + 360 * (angulo + 165 > 360)) & "u7 ta" & Str(angulo) & " m" & Str(XX) & "," & Str(YY)
|
||||
Draw R
|
||||
Else
|
||||
Put (XX, YY), figura2, Or
|
||||
End If
|
||||
End Sub
|
||||
|
||||
Sub FORW (passi As Integer)
|
||||
pasos = -passi
|
||||
comando = "u " & Str(passi)
|
||||
DTurtle
|
||||
comando = ""
|
||||
pasos = 0
|
||||
End Sub
|
||||
|
||||
Sub LEF (gradi As Integer)
|
||||
angulo += gradi
|
||||
Do Until angulo <= 360
|
||||
angulo -= 360
|
||||
Loop
|
||||
DTurtle
|
||||
End Sub
|
||||
|
||||
Sub RIGH (gradi As Integer)
|
||||
angulo -= gradi
|
||||
Do Until angulo >= 0
|
||||
angulo += 360
|
||||
Loop
|
||||
DTurtle
|
||||
End Sub
|
||||
|
||||
Sub SETCOL (C As Integer)
|
||||
colore(N) = C
|
||||
DTurtle
|
||||
End Sub
|
||||
|
||||
Sub SETSIZE (C As Integer)
|
||||
dimen = C
|
||||
DTurtle
|
||||
End Sub
|
||||
|
||||
Sub TURTLE (numero As Integer) Static
|
||||
Dim As Integer i, NN, j
|
||||
Dim As Integer ang()
|
||||
|
||||
Redim figura2(dis)
|
||||
If dis <> 0 And graph <> 0 Then
|
||||
For i = 0 To dis
|
||||
figura2(i) = figura(i, numero)
|
||||
Next i
|
||||
End If
|
||||
NN = N
|
||||
N = numero
|
||||
If j = 0 Then j = 1: Redim ang(10): dimen = 4
|
||||
If X(numero) = 0 Then X(numero) = 320
|
||||
If Y(numero) = 0 Then Y(numero) = 100: colore(numero) = 15
|
||||
ang(NN) = angulo
|
||||
angulo = ang(N)
|
||||
DTurtle
|
||||
End Sub
|
||||
|
||||
Sub UP
|
||||
su = 1
|
||||
End Sub
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
'Rosetta Code problem: https://rosettacode.org/wiki/Simple_turtle_graphics
|
||||
|
||||
'by Jjuanhdez, 03/2023
|
||||
|
||||
#include "Turtle_Graphics.bi"
|
||||
|
||||
Sub rectangle(ancho As Short, alto As Short)
|
||||
For i As Short = 1 To 2
|
||||
FORW alto
|
||||
LEF 90
|
||||
FORW ancho
|
||||
LEF 90
|
||||
Next i
|
||||
End Sub
|
||||
|
||||
Sub square(lado As Short)
|
||||
rectangle(lado, lado)
|
||||
End Sub
|
||||
|
||||
Sub triangle(lado As Short)
|
||||
For i As Short = 1 To 3
|
||||
FORW lado
|
||||
RIGH 120
|
||||
Next i
|
||||
End Sub
|
||||
|
||||
Sub house(lado As Short)
|
||||
LEF 90
|
||||
square lado
|
||||
triangle lado
|
||||
RIGH 90
|
||||
End Sub
|
||||
|
||||
Sub barchart(lst() As Double, lado As Short)
|
||||
Dim As Short i, min, max
|
||||
max = lst(Ubound(lst))
|
||||
|
||||
For i = Lbound(lst) To Ubound(lst)
|
||||
If lst(i) > max Then max = lst(i)
|
||||
Next i
|
||||
|
||||
Dim As Short escala = lado/max
|
||||
Dim As Short ancho = lado/Ubound(lst)
|
||||
|
||||
For i = Lbound(lst) To Ubound(lst)
|
||||
rectangle lst(i), ancho
|
||||
UP
|
||||
FORW ancho
|
||||
DOWN
|
||||
Next i
|
||||
UP
|
||||
BACK lado
|
||||
DOWN
|
||||
End Sub
|
||||
|
||||
Screenres 800,600
|
||||
Windowtitle "Simple turtle graphics"
|
||||
Cls
|
||||
|
||||
TURTLE 1
|
||||
SETSIZE 3
|
||||
SETCOL 0 'black
|
||||
|
||||
DOWN
|
||||
BACK 200
|
||||
SETCOL 2 'green
|
||||
|
||||
house 150
|
||||
UP
|
||||
RIGH 90
|
||||
FORW 50
|
||||
|
||||
Dim As Double s(1 To 6) = {1, 50, 33, 200, 130, 50}
|
||||
barchart s(), 200
|
||||
SETCOL 0
|
||||
Sleep
|
||||
1
Task/Simple-turtle-graphics/J/simple-turtle-graphics-1.j
Normal file
1
Task/Simple-turtle-graphics/J/simple-turtle-graphics-1.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
;install each cut 'gl2 gles github:zerowords/tgsjo'
|
||||
58
Task/Simple-turtle-graphics/J/simple-turtle-graphics-2.j
Normal file
58
Task/Simple-turtle-graphics/J/simple-turtle-graphics-2.j
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
load'zerowords/tgsjo'
|
||||
rotR 0 0 _90
|
||||
translate 0 0 _40
|
||||
clearscreen ''
|
||||
createTurtle 0 0 0
|
||||
|
||||
rectangle=: {{
|
||||
2 repeats {{ 'width height'=. y
|
||||
forward height
|
||||
left 90
|
||||
forward width
|
||||
left 90 }} y
|
||||
}}
|
||||
|
||||
square=: {{size=. y
|
||||
rectangle size,size
|
||||
}}
|
||||
|
||||
triangle=:{{
|
||||
3 repeats {{size=. y
|
||||
forward size
|
||||
right 120}} y
|
||||
}}
|
||||
|
||||
house=:{{size=. y
|
||||
left 90
|
||||
square size
|
||||
triangle size
|
||||
right 90
|
||||
}}
|
||||
|
||||
barchart=: {{'lst size'=. y
|
||||
if.#lst do.
|
||||
scale=. size%>./lst
|
||||
width=. size%#lst
|
||||
right 90
|
||||
for_j. lst do.
|
||||
rectangle (j * scale),width
|
||||
forward width
|
||||
end.
|
||||
back size
|
||||
left 90
|
||||
end.
|
||||
}}
|
||||
|
||||
penColor Red
|
||||
house 150
|
||||
pen 0
|
||||
right 90
|
||||
forward 10
|
||||
left 90
|
||||
pen 1 [ penColor Blue
|
||||
barchart 0.5 0.3333 2 1.3 0.5; 200
|
||||
pen 0
|
||||
left 90
|
||||
forward 10
|
||||
left 270
|
||||
pen 1
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
using Luxor, Colors
|
||||
|
||||
function house(🐢, x, y, siz)
|
||||
oldorientation = 🐢.orientation
|
||||
xpos, ypos = 🐢.xpos, 🐢.ypos
|
||||
# house wall
|
||||
Reposition(🐢, x, y)
|
||||
Rectangle(🐢, siz, siz)
|
||||
# roof
|
||||
Reposition(🐢, x - siz / 2, y - siz / 2)
|
||||
Turn(🐢, -60)
|
||||
Forward(🐢, siz)
|
||||
Turn(🐢, 120)
|
||||
Forward(🐢, siz)
|
||||
# turtle_demo
|
||||
doorheight, doorwidth = siz / 2, siz / 4
|
||||
Pencolor(🐢, 0, 0, 0)
|
||||
Reposition(🐢, x, y + doorheight / 2)
|
||||
Rectangle(🐢, doorwidth, doorheight)
|
||||
# window
|
||||
windowheight, windowwidth = siz /3, siz / 4
|
||||
Reposition(🐢, x + siz / 4, y - siz / 4)
|
||||
Rectangle(🐢, windowwidth, windowheight)
|
||||
Reposition(🐢, x - siz / 4, y - siz / 4)
|
||||
Rectangle(🐢, windowwidth, windowheight)
|
||||
Orientation(🐢, oldorientation)
|
||||
Reposition(🐢, xpos, ypos)
|
||||
end
|
||||
|
||||
function barchart(🐢, data, x, y, siz)
|
||||
oldorientation = 🐢.orientation
|
||||
xpos, ypos = 🐢.xpos, 🐢.ypos
|
||||
maxdata = maximum(data)
|
||||
# scale to fit within a square with sides `siz` and draw bars of chart
|
||||
barwidth = siz / length(data)
|
||||
Pencolor(🐢, 1.0, 0.0, 0.5)
|
||||
Reposition(🐢, x, y)
|
||||
for n in data # draw each bar in chart
|
||||
barheight = n * siz / maxdata
|
||||
Reposition(🐢, x, y - barheight / 2)
|
||||
Rectangle(🐢, barwidth, barheight)
|
||||
x += barwidth
|
||||
end
|
||||
Orientation(🐢, oldorientation)
|
||||
Reposition(🐢, xpos, ypos)
|
||||
end
|
||||
|
||||
function testturtle(width = 400, height = 600)
|
||||
dra = Drawing(600, 400, "turtle_demo.png")
|
||||
origin()
|
||||
background("midnightblue")
|
||||
🐢 = Turtle()
|
||||
Pencolor(🐢, "cyan")
|
||||
Penwidth(🐢, 1.5)
|
||||
house(🐢, -width / 3, height / 7, width / 2)
|
||||
barchart(🐢, [15, 10, 50, 35, 20], width / 8, height / 8, width / 2)
|
||||
finish()
|
||||
end
|
||||
|
||||
testturtle()
|
||||
51
Task/Simple-turtle-graphics/Logo/simple-turtle-graphics.logo
Normal file
51
Task/Simple-turtle-graphics/Logo/simple-turtle-graphics.logo
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
to rectangle :width :height
|
||||
repeat 2 [
|
||||
forward :height
|
||||
left 90
|
||||
forward :width
|
||||
left 90 ]
|
||||
end
|
||||
|
||||
to square :size
|
||||
rectangle size size
|
||||
end
|
||||
|
||||
to triangle :size
|
||||
repeat 3 [
|
||||
forward size
|
||||
right 120 ]
|
||||
end
|
||||
|
||||
to house :size
|
||||
left 90
|
||||
square size
|
||||
triangle size
|
||||
right 90
|
||||
end
|
||||
|
||||
to max :lst
|
||||
if equalp count lst 1 [ output first lst ]
|
||||
make "x max butfirst lst
|
||||
if x > first lst [ output x ]
|
||||
output first lst
|
||||
end
|
||||
|
||||
to barchart :lst :size
|
||||
right 90
|
||||
if emptyp lst [ stop ]
|
||||
make "scale size / (max lst)
|
||||
make "width size / count lst
|
||||
foreach lst [
|
||||
rectangle ? * scale width
|
||||
forward width ]
|
||||
back size
|
||||
left 90
|
||||
end
|
||||
|
||||
clearscreen hideturtle
|
||||
house 150
|
||||
penup
|
||||
right 90 forward 10 left 90
|
||||
pendown
|
||||
barchart [ 0.5 0.33333 2 1.3 0.5 ] 200
|
||||
left 90 back 10 right 90
|
||||
137
Task/Simple-turtle-graphics/Perl/simple-turtle-graphics.pl
Normal file
137
Task/Simple-turtle-graphics/Perl/simple-turtle-graphics.pl
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
#!/usr/bin/perl
|
||||
|
||||
use strict; # https://rosettacode.org/wiki/Simple_turtle_graphics
|
||||
use warnings;
|
||||
use Tk;
|
||||
use List::Util qw( max );
|
||||
|
||||
my $c; # the canvas
|
||||
|
||||
# turtle routines
|
||||
|
||||
my $pen = 1; # true for pendown, false for penup
|
||||
my @location = (0, 0); # upper left corner
|
||||
my $direction = 0; # 0 for East, increasing clockwise
|
||||
my @stack;
|
||||
my $radian = 180 / atan2 0, -1;
|
||||
sub dsin { sin $_[0] / $radian }
|
||||
sub dcos { cos $_[0] / $radian }
|
||||
sub save { push @stack, [ $direction, @location ] }
|
||||
sub restore { ($direction, @location) = @{ pop @stack } }
|
||||
sub turn { $direction += shift }
|
||||
sub right { turn shift }
|
||||
sub left { turn -shift }
|
||||
sub forward
|
||||
{
|
||||
my $x = $location[0] + $_[0] * dcos $direction;
|
||||
my $y = $location[1] + $_[0] * dsin $direction;
|
||||
$pen and $c->createLine( @location, $x, $y, -width => 3 );
|
||||
@location = ($x, $y);
|
||||
}
|
||||
sub back { turn 180; forward shift; turn 180 }
|
||||
sub penup { $pen = 0 }
|
||||
sub pendown { $pen = 1 }
|
||||
sub text { $c->createText( @location, -text => shift ) }
|
||||
|
||||
# make window
|
||||
|
||||
my $mw = MainWindow->new;
|
||||
$c = $mw->Canvas(
|
||||
-width => 900, -height => 900,
|
||||
)->pack;
|
||||
$mw->Button(-text => 'Exit', -command => sub {$mw->destroy},
|
||||
)->pack(-fill => 'x');
|
||||
$mw->after(0, \&run);
|
||||
MainLoop;
|
||||
-M $0 < 0 and exec $0;
|
||||
|
||||
sub box
|
||||
{
|
||||
my ($w, $h) = @_;
|
||||
for (1 .. 2)
|
||||
{
|
||||
forward $w;
|
||||
left 90;
|
||||
forward $h;
|
||||
left 90;
|
||||
}
|
||||
}
|
||||
|
||||
sub house
|
||||
{
|
||||
my $size = shift;
|
||||
box $size, $size;
|
||||
right 90;
|
||||
for ( 1 .. 3 )
|
||||
{
|
||||
right 120;
|
||||
forward $size;
|
||||
}
|
||||
penup;
|
||||
left 90;
|
||||
forward $size;
|
||||
left 90;
|
||||
save;
|
||||
forward $size * 1 / 4;
|
||||
pendown;
|
||||
box $size / 4, $size / 2;
|
||||
penup;
|
||||
forward $size * 3 / 8;
|
||||
left 90;
|
||||
forward $size / 4;
|
||||
right 90;
|
||||
pendown;
|
||||
box $size / 4, $size / 4;
|
||||
penup;
|
||||
restore;
|
||||
save;
|
||||
forward $size / 2;
|
||||
left 90;
|
||||
forward $size + 40;
|
||||
right 90;
|
||||
pendown;
|
||||
for (1 .. 8)
|
||||
{
|
||||
forward 15;
|
||||
left 45;
|
||||
forward 15;
|
||||
}
|
||||
restore;
|
||||
penup;
|
||||
}
|
||||
|
||||
sub graph
|
||||
{
|
||||
save;
|
||||
my $size = shift;
|
||||
my $width = $size / @_;
|
||||
my $hscale = $size / max @_;
|
||||
for ( @_ )
|
||||
{
|
||||
box $width, $hscale * $_;
|
||||
save;
|
||||
penup;
|
||||
forward $width / 2;
|
||||
left 90;
|
||||
forward 10;
|
||||
text $_;
|
||||
pendown;
|
||||
restore;
|
||||
forward $width;
|
||||
}
|
||||
restore;
|
||||
}
|
||||
|
||||
sub run
|
||||
{
|
||||
penup;
|
||||
forward 50;
|
||||
right 90;
|
||||
forward 400;
|
||||
pendown;
|
||||
house(300);
|
||||
penup;
|
||||
forward 400;
|
||||
pendown;
|
||||
graph( 400, 2,7,4,5,1,8,6 );
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #000080;font-style:italic;">-- demo\rosetta\turtle.e</span>
|
||||
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
|
||||
<span style="color: #008080;">global</span> <span style="color: #004080;">Ihandle</span> <span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">dlg</span>
|
||||
<span style="color: #008080;">global</span> <span style="color: #004080;">cdCanvas</span> <span style="color: #000000;">cdcanvas</span>
|
||||
<span style="color: #008080;">global</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">pen_down</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
|
||||
|
||||
<span style="color: #008080;">global</span> <span style="color: #008080;">procedure</span> <span style="color: #000000;">pendown</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">colour</span><span style="color: #0000FF;">=</span><span style="color: #004600;">CD_BLACK</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">pen_down</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
|
||||
<span style="color: #7060A8;">cdCanvasSetForeground</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cdcanvas</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">colour</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
|
||||
|
||||
<span style="color: #008080;">global</span> <span style="color: #008080;">procedure</span> <span style="color: #000000;">penup</span><span style="color: #0000FF;">()</span>
|
||||
<span style="color: #000000;">pen_down</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
|
||||
<!--
|
||||
151
Task/Simple-turtle-graphics/Phix/simple-turtle-graphics-2.phix
Normal file
151
Task/Simple-turtle-graphics/Phix/simple-turtle-graphics-2.phix
Normal file
|
|
@ -0,0 +1,151 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #000080;font-style:italic;">--
|
||||
-- demo\rosetta\Simple_turtle_graphics.exw
|
||||
-- =======================================
|
||||
--</span>
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #008080;">include</span> <span style="color: #000000;">turtle</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> <span style="color: #000080;font-style:italic;">-- (common code for 2D and 3D versions)</span>
|
||||
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">y</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">direction</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
|
||||
|
||||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">walk</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">distance</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000080;font-style:italic;">//
|
||||
// Move forward by distance pixels.
|
||||
//</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">start_x</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">start_y</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">angle</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">direction</span><span style="color: #0000FF;">*</span><span style="color: #004600;">PI</span><span style="color: #0000FF;">/</span><span style="color: #000000;">180</span>
|
||||
<span style="color: #000000;">x</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">distance</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">sin</span><span style="color: #0000FF;">(</span><span style="color: #000000;">angle</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">y</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">distance</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">cos</span><span style="color: #0000FF;">(</span><span style="color: #000000;">angle</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">pen_down</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #7060A8;">cdCanvasLine</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cdcanvas</span><span style="color: #0000FF;">,</span><span style="color: #000000;">start_x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">start_y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
|
||||
|
||||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">right</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">angle</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">direction</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">direction</span><span style="color: #0000FF;">+</span><span style="color: #000000;">angle</span><span style="color: #0000FF;">,</span><span style="color: #000000;">360</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
|
||||
|
||||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">left</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">angle</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">right</span><span style="color: #0000FF;">(</span><span style="color: #000000;">360</span><span style="color: #0000FF;">-</span><span style="color: #000000;">angle</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
|
||||
|
||||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">move</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000080;font-style:italic;">-- s is a list of angles (odd elements)
|
||||
-- and distances (even elements)</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">odd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">right</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
|
||||
<span style="color: #008080;">else</span>
|
||||
<span style="color: #000000;">walk</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
|
||||
|
||||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">rectangle</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">width</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">height</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">move</span><span style="color: #0000FF;">({</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">height</span><span style="color: #0000FF;">,</span><span style="color: #000000;">90</span><span style="color: #0000FF;">,</span><span style="color: #000000;">width</span><span style="color: #0000FF;">,</span><span style="color: #000000;">90</span><span style="color: #0000FF;">,</span><span style="color: #000000;">height</span><span style="color: #0000FF;">,</span><span style="color: #000000;">90</span><span style="color: #0000FF;">,</span><span style="color: #000000;">width</span><span style="color: #0000FF;">,</span><span style="color: #000000;">90</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
|
||||
|
||||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">draw_house</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">width</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">height</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000080;font-style:italic;">//
|
||||
// Draw a house at the current x,y
|
||||
// direction must be 0 for house to be upright
|
||||
-- left(10) -- (deliberately wonky works fine too)
|
||||
// -- (as long as you undo it at the end)
|
||||
// house walls</span>
|
||||
<span style="color: #000000;">rectangle</span><span style="color: #0000FF;">(</span><span style="color: #000000;">width</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">height</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000080;font-style:italic;">// door (maybe some windows too would be nice...)</span>
|
||||
<span style="color: #000000;">penup</span><span style="color: #0000FF;">()</span>
|
||||
<span style="color: #000000;">move</span><span style="color: #0000FF;">({</span><span style="color: #000000;">90</span><span style="color: #0000FF;">,</span><span style="color: #000000;">width</span><span style="color: #0000FF;">/</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">90</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #000000;">pendown</span><span style="color: #0000FF;">(</span><span style="color: #004600;">CD_BLUE</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">rectangle</span><span style="color: #0000FF;">(</span><span style="color: #000000;">width</span><span style="color: #0000FF;">/</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">height</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2.5</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">penup</span><span style="color: #0000FF;">()</span>
|
||||
<span style="color: #000000;">move</span><span style="color: #0000FF;">({-</span><span style="color: #000000;">90</span><span style="color: #0000FF;">,</span><span style="color: #000000;">width</span><span style="color: #0000FF;">/</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">90</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #000080;font-style:italic;">// roof</span>
|
||||
<span style="color: #000000;">walk</span><span style="color: #0000FF;">(</span><span style="color: #000000;">height</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">pendown</span><span style="color: #0000FF;">(</span><span style="color: #004600;">CD_RED</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">a</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">width</span><span style="color: #0000FF;">/</span><span style="color: #000000;">height</span><span style="color: #0000FF;">)*</span><span style="color: #004600;">CD_RAD2DEG</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">width</span><span style="color: #0000FF;">*</span><span style="color: #000000;">width</span><span style="color: #0000FF;">+</span><span style="color: #000000;">height</span><span style="color: #0000FF;">*</span><span style="color: #000000;">height</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">2</span>
|
||||
<span style="color: #000000;">move</span><span style="color: #0000FF;">({</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">,</span><span style="color: #000000;">180</span><span style="color: #0000FF;">-</span><span style="color: #000000;">a</span><span style="color: #0000FF;">*</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #000000;">penup</span><span style="color: #0000FF;">()</span>
|
||||
<span style="color: #000080;font-style:italic;">// return to origin({qw,qh}) and direction 0:</span>
|
||||
<span style="color: #000000;">move</span><span style="color: #0000FF;">({</span><span style="color: #000000;">90</span><span style="color: #0000FF;">+</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">width</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">90</span><span style="color: #0000FF;">,</span><span style="color: #000000;">height</span><span style="color: #0000FF;">,</span><span style="color: #000000;">180</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #000080;font-style:italic;">-- right(10)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
|
||||
|
||||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">draw_barchart</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">nums</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">w</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">h</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000080;font-style:italic;">// draw a barchart occupying the middle 60% of w,h
|
||||
// nums can contain +ve and/or -ve values.
|
||||
-- right(10) -- (ditto)</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">nums</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">mx</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #000000;">nums</span><span style="color: #0000FF;">),</span><span style="color: #000000;">0</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">mn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">nums</span><span style="color: #0000FF;">),</span><span style="color: #000000;">0</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">mx</span><span style="color: #0000FF;">-</span><span style="color: #000000;">mn</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- range</span>
|
||||
<span style="color: #000000;">zl</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">abs</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mn</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">r</span><span style="color: #0000FF;">*</span><span style="color: #000000;">h</span><span style="color: #0000FF;">*</span><span style="color: #000000;">0.6</span><span style="color: #0000FF;">+</span><span style="color: #000000;">h</span><span style="color: #0000FF;">/</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- zero line</span>
|
||||
<span style="color: #000000;">bw</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">w</span><span style="color: #0000FF;">*</span><span style="color: #000000;">0.6</span><span style="color: #0000FF;">/</span><span style="color: #000000;">n</span> <span style="color: #000080;font-style:italic;">-- bar width</span>
|
||||
<span style="color: #000000;">move</span><span style="color: #0000FF;">({</span><span style="color: #000000;">90</span><span style="color: #0000FF;">,</span><span style="color: #000000;">w</span><span style="color: #0000FF;">/</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">90</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zl</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #000000;">pendown</span><span style="color: #0000FF;">()</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">ni</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">nums</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]/</span><span style="color: #000000;">r</span><span style="color: #0000FF;">*</span><span style="color: #000000;">h</span><span style="color: #0000FF;">*</span><span style="color: #000000;">0.6</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">ni</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">rectangle</span><span style="color: #0000FF;">(</span><span style="color: #000000;">bw</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ni</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">else</span>
|
||||
<span style="color: #000000;">pendown</span><span style="color: #0000FF;">(</span><span style="color: #004600;">CD_RED</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">right</span><span style="color: #0000FF;">(</span><span style="color: #000000;">90</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">rectangle</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">ni</span><span style="color: #0000FF;">,</span><span style="color: #000000;">bw</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">left</span><span style="color: #0000FF;">(</span><span style="color: #000000;">90</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">pendown</span><span style="color: #0000FF;">(</span><span style="color: #004600;">CD_BLACK</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #000000;">move</span><span style="color: #0000FF;">({</span><span style="color: #000000;">90</span><span style="color: #0000FF;">,</span><span style="color: #000000;">bw</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">90</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #000000;">penup</span><span style="color: #0000FF;">()</span>
|
||||
<span style="color: #000080;font-style:italic;">// return to origin({w/2,0}) and direction 0:</span>
|
||||
<span style="color: #000000;">move</span><span style="color: #0000FF;">({</span><span style="color: #000000;">180</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zl</span><span style="color: #0000FF;">,</span><span style="color: #000000;">90</span><span style="color: #0000FF;">,</span><span style="color: #000000;">w</span><span style="color: #0000FF;">/</span><span style="color: #000000;">5</span><span style="color: #0000FF;">+</span><span style="color: #000000;">bw</span><span style="color: #0000FF;">*</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">90</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #000080;font-style:italic;">-- left(10)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">redraw_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">width</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">height</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupGetIntInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"DRAWSIZE"</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">cdCanvasActivate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cdcanvas</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">cdCanvasClear</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cdcanvas</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">w2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">width</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">qw</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">width</span><span style="color: #0000FF;">/</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">qh</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">height</span><span style="color: #0000FF;">/</span><span style="color: #000000;">4</span>
|
||||
<span style="color: #000000;">penup</span><span style="color: #0000FF;">()</span>
|
||||
<span style="color: #000000;">move</span><span style="color: #0000FF;">({</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">qh</span><span style="color: #0000FF;">,</span><span style="color: #000000;">90</span><span style="color: #0000FF;">,</span><span style="color: #000000;">qw</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">90</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #000000;">pendown</span><span style="color: #0000FF;">()</span>
|
||||
|
||||
<span style="color: #000000;">draw_house</span><span style="color: #0000FF;">(</span><span style="color: #000000;">qw</span><span style="color: #0000FF;">,</span><span style="color: #000000;">qh</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- house in the left half</span>
|
||||
|
||||
<span style="color: #000000;">penup</span><span style="color: #0000FF;">()</span>
|
||||
<span style="color: #000000;">move</span><span style="color: #0000FF;">({</span><span style="color: #000000;">180</span><span style="color: #0000FF;">,</span><span style="color: #000000;">qh</span><span style="color: #0000FF;">,</span><span style="color: #000000;">90</span><span style="color: #0000FF;">,</span><span style="color: #000000;">qw</span><span style="color: #0000FF;">,</span><span style="color: #000000;">90</span><span style="color: #0000FF;">})</span> <span style="color: #000080;font-style:italic;">-- return to {0,0}</span>
|
||||
|
||||
<span style="color: #000000;">move</span><span style="color: #0000FF;">({</span><span style="color: #000000;">90</span><span style="color: #0000FF;">,</span><span style="color: #000000;">w2</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">90</span><span style="color: #0000FF;">})</span> <span style="color: #000080;font-style:italic;">-- barchart in the right half</span>
|
||||
|
||||
<span style="color: #000000;">draw_barchart</span><span style="color: #0000FF;">({</span><span style="color: #000000;">0.5</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">4</span><span style="color: #0000FF;">/</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1.3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.5</span><span style="color: #0000FF;">},</span><span style="color: #000000;">width</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">height</span><span style="color: #0000FF;">)</span>
|
||||
|
||||
<span style="color: #000000;">move</span><span style="color: #0000FF;">({-</span><span style="color: #000000;">90</span><span style="color: #0000FF;">,</span><span style="color: #000000;">w2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">90</span><span style="color: #0000FF;">})</span> <span style="color: #000080;font-style:italic;">-- return to {0,0}
|
||||
|
||||
-- sanity checks</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">round</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">round</span><span style="color: #0000FF;">(</span><span style="color: #000000;">y</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">round</span><span style="color: #0000FF;">(</span><span style="color: #000000;">direction</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
|
||||
<span style="color: #7060A8;">cdCanvasFlush</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cdcanvas</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span>
|
||||
<span style="color: #000000;">canvas</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupCanvas</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"redraw_cb"</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"RASTERSIZE=600x400"</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">dlg</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupDialog</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span><span style="color: #008000;">`TITLE="Simple turtle graphics"`</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">IupMap</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">cdcanvas</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cdCreateCanvas</span><span style="color: #0000FF;">(</span><span style="color: #004600;">CD_IUP</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">canvas</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">IupShow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"RASTERSIZE"</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">NULL</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- release the minimum limitation</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #7060A8;">IupMainLoop</span><span style="color: #0000FF;">()</span>
|
||||
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<!--
|
||||
45
Task/Simple-turtle-graphics/Python/simple-turtle-graphics.py
Normal file
45
Task/Simple-turtle-graphics/Python/simple-turtle-graphics.py
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
from turtle import *
|
||||
|
||||
def rectangle(width, height):
|
||||
for _ in range(2):
|
||||
forward(height)
|
||||
left(90)
|
||||
forward(width)
|
||||
left(90)
|
||||
|
||||
def square(size):
|
||||
rectangle(size, size)
|
||||
|
||||
def triangle(size):
|
||||
for _ in range(3):
|
||||
forward(size)
|
||||
right(120)
|
||||
|
||||
def house(size):
|
||||
right(180)
|
||||
square(size)
|
||||
triangle(size)
|
||||
right(180)
|
||||
|
||||
def barchart(lst, size):
|
||||
scale = size/max(lst)
|
||||
width = size/len(lst)
|
||||
for i in lst:
|
||||
rectangle(i*scale, width)
|
||||
penup()
|
||||
forward(width)
|
||||
pendown()
|
||||
penup()
|
||||
back(size)
|
||||
pendown()
|
||||
|
||||
clearscreen()
|
||||
hideturtle()
|
||||
house(150)
|
||||
penup()
|
||||
forward(10)
|
||||
pendown()
|
||||
barchart([0.5, (1/3), 2, 1.3, 0.5], 200)
|
||||
penup()
|
||||
back(10)
|
||||
pendown()
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
[ $ "turtleduck.qky" loadfile ] now!
|
||||
|
||||
[ behead do
|
||||
rot witheach
|
||||
[ do 2over 2over
|
||||
v< if 2swap
|
||||
2drop ] ] is largest ( [ --> n/d )
|
||||
|
||||
[ 2 times
|
||||
[ 2dup walk
|
||||
-1 4 turn
|
||||
2over walk
|
||||
-1 4 turn ]
|
||||
2drop 2drop ] is rectangle ( n/d n/d --> )
|
||||
|
||||
[ 2dup rectangle ] is square ( n/d --> )
|
||||
|
||||
[ 3 times
|
||||
[ 2dup walk
|
||||
1 3 turn ]
|
||||
2drop ] is triangle ( n/d --> )
|
||||
|
||||
[ 1 2 turn
|
||||
2dup square triangle
|
||||
1 2 turn ] is house ( n/d --> )
|
||||
|
||||
[ stack ] is bar.width ( --> s )
|
||||
|
||||
[ stack ] is bar.scale ( --> s )
|
||||
|
||||
[ join temp put
|
||||
dup size n->v
|
||||
temp share do v/ 1/v
|
||||
join bar.width put
|
||||
dup largest
|
||||
temp share do v/
|
||||
join bar.scale put
|
||||
witheach
|
||||
[ do
|
||||
bar.scale share do v/
|
||||
bar.width share do
|
||||
rectangle
|
||||
bar.width share do fly ]
|
||||
temp take do -v fly
|
||||
bar.width release
|
||||
bar.scale release ] is barchart ( [ n/d --> )
|
||||
|
||||
turtle
|
||||
150 1 house
|
||||
10 1 fly
|
||||
' [ [ 1 2 ] [ 1 3 ] [ 2 1 ] [ 13 10 ] [ 1 2 ] ] 200 1 barchart
|
||||
-10 1 fly
|
||||
86
Task/Simple-turtle-graphics/Wren/simple-turtle-graphics.wren
Normal file
86
Task/Simple-turtle-graphics/Wren/simple-turtle-graphics.wren
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
import "dome" for Window
|
||||
import "graphics" for Canvas, Color
|
||||
import "./turtle" for Turtle
|
||||
|
||||
class Main {
|
||||
construct new(width, height) {
|
||||
Window.resize(width, height)
|
||||
Canvas.resize(width, height)
|
||||
Window.title = "Simple turtle graphics"
|
||||
_w = width
|
||||
_h = height
|
||||
}
|
||||
|
||||
init() {
|
||||
Canvas.cls(Color.white)
|
||||
_t = Turtle.new()
|
||||
drawHouse(_w/4)
|
||||
barChart([15, 10, 50, 35, 20], _w/3)
|
||||
}
|
||||
|
||||
drawHouse(size) {
|
||||
// save initial turtle position and direction
|
||||
var saveX = _t.x
|
||||
var saveY = _t.y
|
||||
var saveD = _t.dir
|
||||
|
||||
_t.pen.width = 2
|
||||
|
||||
// draw house
|
||||
_t.drawRect(_w/4, _h/2, size, size)
|
||||
|
||||
// draw roof
|
||||
_t.right(30)
|
||||
_t.walk(size)
|
||||
_t.right(120)
|
||||
_t.walk(size)
|
||||
|
||||
// draw door
|
||||
var doorWidth = (size/4).floor
|
||||
var doorHeight = (size/2).floor
|
||||
_t.drawRect(_w/4 + doorWidth/2, _h/2 + doorHeight, doorWidth, doorHeight)
|
||||
|
||||
// draw window
|
||||
var windWidth = (size/3).floor
|
||||
var windHeight = (size/4).floor
|
||||
_t.drawRect(_w/4 + size/2, _h/2 + size/2, windWidth, windHeight)
|
||||
|
||||
// restore initial turtle position and direction
|
||||
_t.x = saveX
|
||||
_t.y = saveY
|
||||
_t.dir = saveD
|
||||
}
|
||||
|
||||
// nums assumed to be all non-negative
|
||||
barChart(nums, size) {
|
||||
// save intial turtle position and direction
|
||||
var saveX = _t.x
|
||||
var saveY = _t.y
|
||||
var saveD = _t.dir
|
||||
|
||||
// find maximum
|
||||
var max = 0
|
||||
for (n in nums) if (n > max) max = n
|
||||
|
||||
// scale to fit within a square with sides 'size' and draw chart
|
||||
var barWidth = (size / nums.count).floor
|
||||
var startX = _w / 2 + 20
|
||||
var startY = _h / 2
|
||||
for (i in 0...nums.count) {
|
||||
var barHeight = (nums[i] * size / max).round
|
||||
_t.drawRect(startX, startY - barHeight, barWidth, barHeight)
|
||||
startX = startX + barWidth
|
||||
}
|
||||
|
||||
// restore intial turtle position and direction
|
||||
_t.x = saveX
|
||||
_t.y = saveY
|
||||
_t.dir = saveD
|
||||
}
|
||||
|
||||
update() {}
|
||||
|
||||
draw(alpha) {}
|
||||
}
|
||||
|
||||
var Game = Main.new(600, 600)
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
// Rosetta Code problem: http://rosettacode.org/wiki/Simple_turtle_graphics
|
||||
// Adapted from Python to Yabasic by Galileo, 01/2022
|
||||
|
||||
import turtle
|
||||
|
||||
sub rectang(width, height)
|
||||
local i
|
||||
|
||||
for i = 1 to 2
|
||||
move(height)
|
||||
turn(-90)
|
||||
move(width)
|
||||
turn(-90)
|
||||
next
|
||||
end sub
|
||||
|
||||
sub square(size)
|
||||
rectang(size, size)
|
||||
end sub
|
||||
|
||||
sub triang(size)
|
||||
local i
|
||||
|
||||
for i = 1 to 3
|
||||
move(size)
|
||||
turn(120)
|
||||
next
|
||||
end sub
|
||||
|
||||
sub house(size)
|
||||
turn(180)
|
||||
square(size)
|
||||
triang(size)
|
||||
turn(180)
|
||||
end sub
|
||||
|
||||
sub barchart(lst$, size)
|
||||
local t$(1), t(1), n, m, i, scale, width
|
||||
|
||||
n = token(lst$, t$())
|
||||
redim t(n)
|
||||
|
||||
for i = 1 to n
|
||||
t(i) = val(t$(i))
|
||||
if t(i) > m m = t(i)
|
||||
next
|
||||
|
||||
scale = size/m
|
||||
width = size/n
|
||||
for i = 1 to n
|
||||
rectang(t(i)*scale, width)
|
||||
pen(false)
|
||||
move(width)
|
||||
pen(true)
|
||||
next
|
||||
pen(false)
|
||||
move(-size)
|
||||
pen(true)
|
||||
end sub
|
||||
|
||||
startTurtle()
|
||||
color 255, 255, 255
|
||||
turn(90)
|
||||
house(150)
|
||||
pen(false)
|
||||
move(10)
|
||||
pen(true)
|
||||
barchart("0.5 0.333 2 1.3 0.5", 200)
|
||||
Loading…
Add table
Add a link
Reference in a new issue