Advertisement
Guest User

Random triangles 2

a guest
Mar 29th, 2020
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.65 KB | None | 0 0
  1. import java.awt.Color;
  2.  
  3. import org.teachingextensions.logo.Turtle;
  4.  
  5. public class RandomTriangles {
  6.     public static void main(String[] args) {
  7.         RandomTriangles myApp = new RandomTriangles();
  8.         myApp.run();
  9.     }
  10.  
  11.     // First, create a new Turtle object and tell it to show up
  12.     Turtle turtle = new Turtle();
  13.  
  14.     public void run() {
  15.  
  16.         turtle.show();
  17.         // #1 - Before you start, you can change the speed,
  18.         // pen width and pen color of your turtle if you like
  19.         turtle.setSpeed(8);
  20.         turtle.setPenWidth(6);
  21.  
  22.         drawTriangle(60, 60, 60, Color.GRAY);
  23.  
  24.         drawTriangle(30, 155, 230, Color.CYAN);
  25.  
  26.         drawTriangle(95, 360, 140, Color.RED);
  27.  
  28.         drawTriangle(62, 190, 335, Color.YELLOW);
  29.  
  30.         drawTriangle(40, 450, 30, Color.GREEN);
  31.  
  32.         drawTriangle(16, 30, 310, Color.PINK);
  33.  
  34.         drawTriangle(22, 500, 205, Color.ORANGE);
  35.  
  36.         drawTriangle(73, 345, 270, Color.BLUE);
  37.  
  38.         drawTriangle(60, 490, 330, Color.MAGENTA);
  39.  
  40.     }
  41.  
  42.     public void drawTriangle(int length, int x, int y, Color color) {
  43.  
  44.         // #3 --------- Draw a triangle --------
  45.         turtle.setX(x);
  46.         turtle.setY(y);
  47.         turtle.setPenColor(color);
  48. //              #3.1 - Turn for 72 degrees
  49.         turtle.turn(72);
  50. //              #3.2 - Move for the length of the triangle side
  51.         turtle.move(length);
  52. //              #3.3 - Turn for 150 degrees
  53.         turtle.turn(150);
  54. //              #3.4 - Move for the length of the triangle side
  55.         turtle.move(length);
  56. //              #3.5 - Turn for 105 degrees
  57.         turtle.turn(105);
  58. //              #3.6 - Move for the length described by this formula:
  59. //                      triangle side multiplied by 2 * Math.cos(Math.toRadians(75))
  60.         turtle.move(length * 2 * Math.cos(Math.toRadians(75)));
  61.         // ------- End of Draw a triangle -------
  62.  
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement