Math Art Questions

1. Midpoint - Code Follows

(a) What does this program do?

(b) Run the program three times. Change the x and y co-ordinates at the beginning each time to create three different lines. Why is it useful to use variables throughout the program instead of actual numbers?

(c) Add a blue line that connects the dots and the midpoints.

import java.applet.Applet;
   import java.awt.*;
public class midPoint extends Applet
   {
   public void paint (Graphics g)
   {
   int x1 = 100;
   int y1 = 50;
   int x2 = 200;
   int y2 = 100;
 //calculate the midpoints
   int midx = (x1 + x2) / 2;
   int midy = (y1 + y2) / 2;
 //draw point 1
   g.fillOval (x1, y1, 4, 4);
   g.drawString ("(" + x1 + ", " + y1 + ")", x1,    y1);
   //draw point 2
   g.fillOval (x2, y2, 4, 4);
   g.drawString ("(" + x2 + ", " + y2 + ")", x2,    y2);
   //draw midpoint
   g.setColor (Color.red);
   g.fillOval (midx, midy, 4, 4);
   g.drawString ("Midpoint:(" + midx + ", " + midy + ")",    midx, midy);
   }
   }
 

2. Triangles in Triangles - Code Follows

(a) What does this program do?

(b) Run the program three times. Change the x and y co-ordinates at the beginning each time to create three different traingles. Why is it useful to use variables throughout the program instead of actual numbers?

(b) Change the code so that it creates the picture shown (add the colours). What do you have to add and where?

(c) Add in three more layers of traingles - use cut and paste skills!

import java.applet.Applet;
   import java.awt.*;
public class trianglesINtriangles1 extends Applet
   {
   public void paint (Graphics g)
   {
   int x1 = 200;
   int y1 = 200;
   int x2 = 100;
   int y2 = 100;
   int x3 = 300;
   int y3 = 100;
 int XArray [] = {x1, x2, x3};
   int YArray [] = {y1, y2, y3};
   g.drawPolygon (XArray, YArray, 3);
 //calculate the midpoints
   int mx12 = (x1 + x2) / 2;
   int my12 = (y1 + y2) / 2;
   int mx13 = (x1 + x3) / 2;
   int my13 = (y1 + y3) / 2;
   int mx32 = (x3 + x2) / 2;
   int my32 = (y3 + y2) / 2;
 int XArray1 [] = {mx12, mx13, mx32};
   int YArray1 [] = {my12, my13, my32};
   g.drawPolygon (XArray1, YArray1, 3);
 //calculate the midpoints for the second time
   x1 = (mx12 + mx13) / 2;
   y1 = (my12 + my13) / 2;
   x2 = (mx12 + mx32) / 2;
   y2 = (my12 + my32) / 2;
   x3 = (mx13 + mx32) / 2;
   y3 = (my13 + my32) / 2;
 int XArray2 [] = {x1, x2, x3};
   int YArray2 [] = {y1, y2, y3};
   g.drawPolygon (XArray2, YArray2, 3);
 //calculate the midpoints for the third time
   mx12 = (x1 + x2) / 2;
   my12 = (y1 + y2) / 2;
   mx13 = (x1 + x3) / 2;
   my13 = (y1 + y3) / 2;
   mx32 = (x3 + x2) / 2;
   my32 = (y3 + y2) / 2;
 int XArray3 [] = {mx12, mx13, mx32};
   int YArray3 [] = {my12, my13, my32};
   g.drawPolygon (XArray3, YArray3, 3);
   }
   }