Simple turtle graphics: Difference between revisions

Added an example implementation in Java
(Added an example implementation in Java)
 
(3 intermediate revisions by 3 users not shown)
Line 282:
 
=={{header|EasyLang}}==
[https://easylang.dev/show/#cod=rVLBboMwDL3zFU/arVNpKEVaD3wJ4pCWtEGiSZWUsf79nEBQupbtspxsP/v5ObbtDwZSX0QCoBFnlGDO/CKj8NZ9tho9qIBf9KegpHuSJr76DcnV6CNO2gzcNFBIkY5E7yWO2nryFdRISTHbqjjWngJ/5lx6XavGFmSLzoopPHcm23ewnRBXsDQjLV7DrTcKPAhwPdYlOKFBpNS9FQH32dmHnyqo3xU/kVgb0US5OZtz9+w/gJHew/lSXbZ9gfhFuH3QdPOsB27Aq/ph2kU56/3TsJSFFjeNTihHFLbA3YXI9iyF8Q6v2npeyUKRpBKXh40vWU13RW8gZFdQ/LEgFhUpljH8hA6/ovIF9R93MP2r+8mKJCPPsWUMWc6cVyff Run it]
[https://easylang.dev/show/#cod=rVLLbsMgELz7K1bqLVVcHMdSc/CXWD4QmwQkAhHEdfP3XbBBTkjbVCqnZXd2Zl922Bvg+sQyAOjZEWogzvxEo/LWNVq9HlWIn/QHQ9A1y7OOSjlRvEB2NrqDgzYjNT0oyCGf2F5r6LT1CitQEy/6rFBLnzgEkcJ98UmhJh20mbRsdkd5tL2ClYydgeQFFuRruAxGAQ0FOI11DRSjoUiuB8tC3PfgU4p3Eh2hj231ELMsFVnvs0pym7VLif8TsqjAA8vfuYrNjxi/07hfnFac3Z4aoE2bTu+JBta7ZHgIBQEXDZIpxxuWTN0VcnHkzPgPbUQbN/5NEscUh4M3n7KabxffiJFthf7bhKSy+wZ4AnyMG5/EpXyz8N8ucbkdt48GO4WyhA0hUJTE/drsCw== Run it]
 
<syntaxhighlight lang="easylang">
Line 292:
move x y
.
call home
#
proc forward n . .
Line 309:
#
proc house . .
call turn 180
call forward 45
call turn 180
down = 1
#
call forward 30
call turn 90
call forward 30
call turn 90
call forward 30
call turn 90
call forward 30
#
call turn 30
call forward 30
call turn 120
call forward 30
call home
.
call house
#
proc bar a[] . .
call turn 90
call forward 30
call turn -90
down = 1
for i to len a[]
Line 341:
h = a[i] / max * 50
w = 45 / len a[]
call turn -90
call forward h
call turn 90
call forward w
call turn 90
call forward h
call turn -90
.
call turn 180
call forward 45
call home
.
call bar [ 50 33 200 130 50 ]
</syntaxhighlight>
 
Line 558:
110 OPTION ANGLE DEGREES
120 GRAPHICS HIRES 4
130 CALL HOUSE(0200,200,200)
140 SET INK 2:CALL CHART(250450,200,601000,700)
150 DEF HOUSE(X,Y,L)
160 PLOT X,Y,ANGLE 0;
Line 566:
190 NEXT
200 PLOT LEFT 60;FORWARD L;RIGHT 120;FORWARD L;X,Y,
210 END DEF
220 DEF CHART(X,Y,LX1,Y1)
230 PLOTLET X,YPC,MX=0
240 DO
250 READ IF MISSING EXIT DO:Z
260 LET PC=PC+1:LET MX=MAX(MX,Z)
260 PLOT ANGLE 90;FORWARD Z;RIGHT 90;FORWARD L;RIGHT 90;FORWARD Z;RIGHT 90;FORWARD L
270 LOOP
270 PLOT ANGLE 0;FORWARD L
280 LOOPRESTORE
290 PLOTLET L=(X1-X,)/PC:LET MX=(Y1-Y,)/MX
300 END DEF PLOT X,Y,
310 DO
310 DATA 90,60,300,200,90
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>
 
Line 649 ⟶ 656:
 
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}}==
Line 1,204 ⟶ 1,347:
{{libheader|DOME}}
{{libheader|Wren-turtle}}
<syntaxhighlight lang="ecmascriptwren">import "dome" for Window
import "graphics" for Canvas, Color
import "./turtle" for Turtle
3

edits