Simple turtle graphics: Difference between revisions

Added an example implementation in Java
(Added an example implementation in Java)
 
(2 intermediate revisions by 2 users not shown)
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