Simple turtle graphics: Difference between revisions

Added an example implementation in Java
m (J: rearrange initialization lines)
(Added an example implementation in Java)
 
(24 intermediate revisions by 10 users not shown)
Line 5:
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]]
See image here: https://i.imgur.com/B7YbTbZ.png
 
;Task:
::* Create a function (or subroutine) that uses turtle graphics to draw a house of a specified size as described abovedepicted. 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.
Line 16:
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "D2:TURTLE.ACT" ;from the Action! Tool Kit
 
PROC Rectangle(INT w,h)
Line 104:
DO UNTIL CH#$FF OD
CH=$FF
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Simple_turtle_graphics.png Screenshot from Atari 8-bit computer]
 
=={{header|Ada}}==
<langsyntaxhighlight lang="ada">
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Characters; use Ada.Characters;
Line 279:
New_Line;
end main;
</syntaxhighlight>
</lang>
 
=={{header|EasyLang}}==
[https://easylang.dev/show/#cod=rVLBboMwDL3zFU/arVNpKEVaD3wJ4pCWtEGiSZWUsf79nEBQupbtspxsP/v5ObbtDwZSX0QCoBFnlGDO/CKj8NZ9tho9qIBf9KegpHuSJr76DcnV6CNO2gzcNFBIkY5E7yWO2nryFdRISTHbqjjWngJ/5lx6XavGFmSLzoopPHcm23ewnRBXsDQjLV7DrTcKPAhwPdYlOKFBpNS9FQH32dmHnyqo3xU/kVgb0US5OZtz9+w/gJHew/lSXbZ9gfhFuH3QdPOsB27Aq/ph2kU56/3TsJSFFjeNTihHFLbA3YXI9iyF8Q6v2npeyUKRpBKXh40vWU13RW8gZFdQ/LEgFhUpljH8hA6/ovIF9R93MP2r+8mKJCPPsWUMWc6cVyff Run it]
 
<syntaxhighlight lang="easylang">
subr home
deg = 0
x = 50
y = 50
down = 0
move x y
.
home
#
proc forward n . .
x += cos deg * n
y += sin deg * n
if down = 1
line x y
else
move x y
.
sleep 0.1
.
proc turn a . .
deg -= a
.
#
proc house . .
turn 180
forward 45
turn 180
down = 1
#
forward 30
turn 90
forward 30
turn 90
forward 30
turn 90
forward 30
#
turn 30
forward 30
turn 120
forward 30
home
.
house
#
proc bar a[] . .
turn 90
forward 30
turn -90
down = 1
for i to len a[]
max = higher max a[i]
.
for i to len a[]
h = a[i] / max * 50
w = 45 / len a[]
turn -90
forward h
turn 90
forward w
turn 90
forward h
turn -90
.
turn 180
forward 45
home
.
bar [ 50 33 200 130 50 ]
</syntaxhighlight>
 
=={{header|FreeBASIC}}==
===Library: Turtle_Graphics.bi===
<syntaxhighlight lang="vb">' 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</syntaxhighlight>
 
===Turtle test:===
<syntaxhighlight lang="vb">'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</syntaxhighlight>
{{out}}
[https://www.dropbox.com/s/tlmuza1o9foh8kt/Simple_turtle_graphics_FB.png?dl=0 Simple turtle graphics FreeBasic image]
 
=={{header|IS-BASIC}}==
<syntaxhighlight lang="is-basic">100 PROGRAM "Turtle.bas"
110 OPTION ANGLE DEGREES
120 GRAPHICS HIRES 4
130 CALL HOUSE(200,200,200)
140 SET INK 2:CALL CHART(450,200,1000,700)
150 DEF HOUSE(X,Y,L)
160 PLOT X,Y,ANGLE 0;
170 FOR I=1 TO 4
180 PLOT FORWARD 200,RIGHT 90;
190 NEXT
200 PLOT LEFT 60;FORWARD L;RIGHT 120;FORWARD L;X,Y,
210 END DEF
220 DEF CHART(X,Y,X1,Y1)
230 LET PC,MX=0
240 DO
250 READ IF MISSING EXIT DO:Z
260 LET PC=PC+1:LET MX=MAX(MX,Z)
270 LOOP
280 RESTORE
290 LET L=(X1-X)/PC:LET MX=(Y1-Y)/MX
300 PLOT X,Y,
310 DO
320 READ IF MISSING EXIT DO:Z
330 PLOT ANGLE 90;FORWARD Z*MX;RIGHT 90;FORWARD L;RIGHT 90;FORWARD Z*MX;RIGHT 90;FORWARD L
340 PLOT ANGLE 0;FORWARD L
350 LOOP
360 PLOT X,Y,
370 END DEF
380 DATA 90,60,300,200,90
</syntaxhighlight>
 
=={{header|J}}==
{{trans|Logo}}
 
Pre-requisitesPrerequisites (requires a network connection or, for a non-networked system, significant technical savvy), and assumes user is running the a recent version of J's qtide (perhaps J 9.3) qtide:
<langsyntaxhighlight Jlang="j"> ;install each cut 'gl2 gles github:zerowords/tgsjo'</langsyntaxhighlight>
 
Implementation (note that this is meant to be copied and pasted to a file which J will load, rather than being typed in line by line -- manual typing would work if no errors were made, but that would be a painstaking approach):
Implementation:
 
<langsyntaxhighlight Jlang="j">load'zerowords/tgsjo'
R_tgsjo_=:rotR 0 0 _90
EYE_tgsjo_=:translate 0 0 30_40
clearscreen ''
createTurtle 0 0 0
Line 344 ⟶ 649:
pen 0
left 90
backforward 10
left 270
right 90
pen 1</syntaxhighlight>
turtleScale 0</lang>
 
Opens a window with the indicated content. House is red, barchart is blue.
 
Note that we have used the Logo naming convention, which means that the height of our barchart is the <tt>width</tt> parameter in <tt>rectangle</tt> (and, likewise, the width of each bar is the <tt>height</tt> parameter in <tt>rectangle</tt>)
 
=={{header|Java}}==
Opens a javax.swing.JFrame, displaying the graphics shown in the screenshot:
 
[[File:Screenshot 001.png|alt=Screenshot of the Java implementation of task "Simple turtle graphics"|Screenshot of the Java implementation of task "Simple turtle graphics"]]
 
<syntaxhighlight lang="java">
 
/* Class Turtle starts here */
 
import java.awt.geom.AffineTransform;
 
public class Turtle extends java.lang.Object
{
private final java.awt.Graphics2D origin;
private java.awt.Graphics2D g2;
 
public Turtle(java.awt.Graphics2D origin)
{
this.origin = origin;
origin();
}
public void origin()
{
g2=(java.awt.Graphics2D)origin.create();
}
public void relativePosition(int xoff, int yoff)
{
AffineTransform at=g2.getTransform();
at.concatenate(AffineTransform.
getTranslateInstance(xoff,yoff));
g2.setTransform(at);
}
public void turnByDegrees(int thetaInDegrees)
{
AffineTransform at=g2.getTransform();
at.concatenate(AffineTransform.
getRotateInstance(Math.toRadians(thetaInDegrees)));
g2.setTransform(at);
}
public void forward(int len)
{
g2.drawLine(0,0,len,0);
relativePosition(len,0);
}
}
 
/* Class CanvasComponent starts here*/
 
import java.awt.Graphics;
import java.awt.Graphics2D;
 
public class CanvasComponent extends javax.swing.JComponent
{
protected void paintComponent(Graphics g)
{
Turtle turtle=new Turtle((Graphics2D)g);
 
turtle.origin();
 
turtle.relativePosition(50,50);
 
house(turtle,100,200,50);
 
turtle.origin();
 
turtle.relativePosition(200,50);
 
double[] numbers=new double[]{0.5, 0.33333, 2, 1.3, 0.5};
 
barchart(turtle,200,numbers);
}
private void barchart(Turtle turtle,int size,double[] numbers)
{
double max=0;
for(double d:numbers)
{
if(d>max)
max=d;
}
double width=size/ numbers.length;
int xpos=400;
for(double d:numbers)
{
int h=(int) (size * (d / max));
rectangle(turtle, (int)width, h);
xpos+=width;
turtle.relativePosition((int)width,0);
}
}
private void house(Turtle turtle,int width,
int height, int roofheight)
{
rectangle(turtle,width,height);
turtle.relativePosition(0,height);
double dist= Math.sqrt(roofheight*roofheight+width/2*width/2);
double angle= Math.toDegrees(Math.asin(roofheight/dist));
turtle.turnByDegrees((int)angle);
turtle.forward((int)dist);
turtle.turnByDegrees(-2*(int)angle);
turtle.forward((int)dist);
}
private void rectangle(Turtle turtle,int width, int height)
{
for(int i=0;i<2;++i)
{
turtle.forward(width);
turtle.turnByDegrees(90);
turtle.forward(height);
turtle.turnByDegrees(90);
}
 
}
}
 
/* Class MainClass starts here */
 
import javax.swing.*;
import java.awt.*;
 
public class MainClass
{
public static void main(String[] args)
{
CanvasComponent canvas=new CanvasComponent();
canvas.setPreferredSize(new Dimension(800,600));
JFrame f=new JFrame();
JPanel p=new JPanel(new BorderLayout());
p.add(canvas);
f.setContentPane(p);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
}
</syntaxhighlight>
 
=={{header|Julia}}==
{{trans|Wren}}
Outputs a PNG file.
<langsyntaxhighlight lang="julia">using Luxor, Colors
 
function house(🐢, x, y, siz)
Line 413 ⟶ 856:
 
testturtle()
</syntaxhighlight>
</lang>
 
=={{header|Logo}}==
Line 419 ⟶ 862:
{{trans|Quackery}}
 
<langsyntaxhighlight Logolang="logo">to rectangle :width :height
repeat 2 [
forward :height
Line 469 ⟶ 912:
pendown
barchart [ 0.5 0.33333 2 1.3 0.5 ] 200
left 90 back 10 right 90</langsyntaxhighlight>
 
{{out}}
 
https[[File://imgur.com/4V1UrcN.png]]
 
=={{header|Perl}}==
Added octangle window to house attic.
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Simple_turtle_graphics
Line 613 ⟶ 1,056:
pendown;
graph( 400, 2,7,4,5,1,8,6 );
}</langsyntaxhighlight>
 
=={{header|Phix}}==
{{libheader|Phix/pGUI}}
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/simpleturtlegraphics.htm here].<br>
I factored out some common code for the 2D and 3D versions (or started to, distributed version contains some additional diag aids)
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(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>
<!--</syntaxhighlight>-->
 
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Simple_turtle_graphics.exw
Line 625 ⟶ 1,086:
--</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUIturtle</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;">Ihandle</span> <span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">dlg</span>
<span style="color: #004080;">cdCanvas</span> <span style="color: #000000;">cdcanvas</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: #004080;">bool</span> <span style="color: #000000;">pen_down</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</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>
Line 654 ⟶ 1,112:
<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;">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>
<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>
Line 685 ⟶ 1,134:
// 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>
Line 704 ⟶ 1,154:
<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.</span>
-- 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>
Line 733 ⟶ 1,185:
<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: #000080;font-style:italic;">/*posx*/</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">/*posy*/</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;">qww2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">width</span><span style="color: #0000FF;">/</span><span style="color: #000000;">42</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>
Line 745 ⟶ 1,199:
<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;">-- (athouse in the currentleft x,y)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;">width</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2w2</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;">width</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2w2</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>
Line 764 ⟶ 1,218:
<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>
Line 775 ⟶ 1,230:
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</langsyntaxhighlight>-->
 
=={{header|Python}}==
{{trans|Quackery}}
 
<langsyntaxhighlight Pythonlang="python">from turtle import *
 
def rectangle(width, height):
Line 824 ⟶ 1,279:
penup()
back(10)
pendown()</langsyntaxhighlight>
 
{{out}}
 
[[File:Python house and bar chart.png]]
https://imgur.com/oBXTDem
 
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery"> [ $ "turtleduck.qky" loadfile ] now!
 
[ behead do
Line 883 ⟶ 1,338:
10 1 fly
' [ [ 1 2 ] [ 1 3 ] [ 2 1 ] [ 13 10 ] [ 1 2 ] ] 200 1 barchart
-10 1 fly</langsyntaxhighlight>
 
{{output}}
 
[[File:Turtle house and bar chart.png]]
https://imgur.com/B7YbTbZ
 
=={{header|Wren}}==
{{libheader|DOME}}
{{libheader|Wren-turtle}}
<langsyntaxhighlight ecmascriptlang="wren">import "dome" for Window
import "graphics" for Canvas, Color
import "./turtle" for Turtle
Line 977 ⟶ 1,432:
}
 
var Game = Main.new(600, 600)</langsyntaxhighlight>
 
{{out}}
Line 983 ⟶ 1,438:
Similar to Quackery image except that the house has a door and a single window.
</pre>
 
=={{header|XPL0}}==
[[File:TurtleXPL0.gif|right]]
<syntaxhighlight lang "XPL0">
def ScrW=320, ScrH=200;
def Pi = 3.141592654;
def D2R = Pi/180.0;
real Dir, PosX, PosY;
int Pen;
def \Pen\ Up, Down;
 
proc Turn(Ang);
real Ang;
Dir:= Dir + Ang*D2R;
 
proc MoveTo(Dist);
real Dist;
[PosX:= PosX + Dist*Cos(Dir);
PosY:= PosY + Dist*Sin(Dir);
if Pen = Down then
Line(ScrW/2+fix(PosX), ScrH/2-fix(PosY), $0F \white\)
else
Move(ScrW/2+fix(PosX), ScrH/2-fix(PosY));
];
 
proc Rectangle(Width, Height);
real Width, Height;
int N;
[for N:= 1 to 2 do
[MoveTo(Width);
Turn(90.0);
MoveTo(Height);
Turn(90.0);
];
];
 
proc BarGraph(List, Len, Size);
real List; int Len; real Size;
int N;
def BarWidth = 0.4;
[for N:= 0 to Len-1 do
[Rectangle(Size*BarWidth, List(N)*Size);
MoveTo(Size*BarWidth);
];
MoveTo(-Size*BarWidth*float(Len));
];
 
proc Triangle(Size);
real Size;
int N;
[for N:= 1 to 3 do
[MoveTo(Size);
Turn(-120.0);
];
];
 
proc Square(Size);
real Size;
Rectangle(Size, Size);
 
proc House(Size);
real Size;
[Turn(180.0);
Square(Size);
Triangle(Size);
Turn(180.0);
];
 
[SetVid($13); \set VGA graphics
Move(ScrW/2, ScrH/2); \start Line at center
Dir:= 0.0; PosX:= 0.0; PosY:= 0.0; Pen:= Down;
House(80.0);
Pen:= Up; MoveTo(10.0); Pen:= Down;
BarGraph([0.5, 1.0/3.0, 2.0, 1.3, 0.5], 5, 45.0);
]</syntaxhighlight>
 
=={{header|Yabasic}}==
{{trans|Python}}
<langsyntaxhighlight Yabasiclang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Simple_turtle_graphics
// Adapted from Python to Yabasic by Galileo, 01/2022
 
Line 1,053 ⟶ 1,583:
move(10)
pen(true)
barchart("0.5 0.333 2 1.3 0.5", 200)</langsyntaxhighlight>
3

edits