Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import org.teachingextensions.logo.Turtle;
- public class RandomTriangles {
- public static void main(String[] args) {
- RandomTriangles myApp = new RandomTriangles();
- myApp.run();
- }
- // First, create a new Turtle object and tell it to show up
- Turtle turtle = new Turtle();
- public void run() {
- turtle.show();
- // #1 - Before you start, you can change the speed,
- // pen width and pen color of your turtle if you like
- turtle.setSpeed(8);
- turtle.setPenWidth(6);
- drawTriangle(60, 60, 60, Color.GRAY);
- drawTriangle(30, 155, 230, Color.CYAN);
- drawTriangle(95, 360, 140, Color.RED);
- drawTriangle(62, 190, 335, Color.YELLOW);
- drawTriangle(40, 450, 30, Color.GREEN);
- drawTriangle(16, 30, 310, Color.PINK);
- drawTriangle(22, 500, 205, Color.ORANGE);
- drawTriangle(73, 345, 270, Color.BLUE);
- drawTriangle(60, 490, 330, Color.MAGENTA);
- }
- public void drawTriangle(int length, int x, int y, Color color) {
- // #3 --------- Draw a triangle --------
- turtle.setX(x);
- turtle.setY(y);
- turtle.setPenColor(color);
- // #3.1 - Turn for 72 degrees
- turtle.turn(72);
- // #3.2 - Move for the length of the triangle side
- turtle.move(length);
- // #3.3 - Turn for 150 degrees
- turtle.turn(150);
- // #3.4 - Move for the length of the triangle side
- turtle.move(length);
- // #3.5 - Turn for 105 degrees
- turtle.turn(105);
- // #3.6 - Move for the length described by this formula:
- // triangle side multiplied by 2 * Math.cos(Math.toRadians(75))
- turtle.move(length * 2 * Math.cos(Math.toRadians(75)));
- // ------- End of Draw a triangle -------
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement