/*----------------------------------------------------------------------*/ /* A demonstration of a geometrical proof */ /*----------------------------------------------------------------------*/ /* Haven2 - is the main object that controls all the events for this Pythogorean Proof display - it extensively uses Btex for all text (buttons and the step explanations) diagram - handles all the picture steps of the proofs (some are animated) */ /*----------------------------------------------------------------------*/ /* To remove a lot of flicker problems all of the drawing occurs off */ /* the screen. Also if a process calls for a redraw when another is */ /* drawing the process says skip me in that redraw. */ /*----------------------------------------------------------------------*/ /* Jim Morey - morey@math.ubc.ca - Aug 30 */ /*----------------------------------------------------------------------*/ /* ** Warning this code is a complete mess! check http://www.math.ubc.ca/~morey/morey.html for much better coding -- I'll clean this up when I get some time */ /*----------------------------------------------------------------------*/ import java.io.InputStream; import awt.*; import browser.*; import browser.audio.AudioData; import net.www.html.*; import Btex; import fillTriangle; /*----------------------------------------------------------------------*/ class Diag implements Runnable{ Color foreground,background; int x,y,time,pause,delay,step,a,b,mousex,mousey; boolean keep_going; private static double pi = 3.14159265; private int W,H,xb,yb; private Graphics offscreen; private Image im; private Applet parent; private boolean ready; private float scaling; /* - - - - - - - - - - - - - - - - - - - - - - */ Diag(Applet parent,int wid, int heig) { this.parent = parent; time = 1000; foreground = awt.Color.black; background = awt.Color.white; delay = 50; x = 0; y = 0; step = 0; pause = 0; W = wid; H = heig; a = 70; b = 150; /* .. set xb,yb at the upper corner of the triangle .. */ xb = (W - 2*b - a)/2+b; yb = (H - 2*a - b)/2+a; mousex = a+x+xb; mousey = b+y+yb; im = parent.createImage(W,H); offscreen = new Graphics(im); offscreen.setForeground(background); offscreen.fillRect(0, 0, W, H); ready = true; } /* - - - - - - - - - - - - - - - - - - - - - - */ public void run(){ fillTriangle b_triangle, a_triangle, init_triangle; boolean three_squares = true, b_tri=false,a_tri=false,line=false; boolean init_tri=true,vert=false; Thread.currentThread().setPriority(Thread.MIN_PRIORITY); b_triangle = new fillTriangle(offscreen); a_triangle = new fillTriangle(offscreen); init_triangle = new fillTriangle(offscreen); int c2 = a*a+b*b; init_triangle.vect[0][0] = xb; init_triangle.vect[0][1] = yb; init_triangle.vect[1][0] = xb; init_triangle.vect[1][1] = yb+b; init_triangle.vect[2][0] = xb+a; init_triangle.vect[2][1] = yb+b; b_triangle.vect[0][0] = xb-b; b_triangle.vect[0][1] = yb; b_triangle.vect[1][0] = xb; b_triangle.vect[1][1] = yb; b_triangle.vect[2][0] = xb-b; b_triangle.vect[2][1] = yb+b; a_triangle.vect[0][0] = xb+a; a_triangle.vect[0][1] = yb+a+b; a_triangle.vect[1][0] = xb+a; a_triangle.vect[1][1] = yb+b; a_triangle.vect[2][0] = xb; a_triangle.vect[2][1] = yb+a+b; offscreen.setForeground(background); offscreen.fillRect(0, 0, W, H); offscreen.setForeground(foreground); int t0 = System.nowMillis()+pause; keep_going = true; while(keep_going){ int t = System.nowMillis() - t0; double done =(double)(t)/(double)time; if (t>time) { keep_going = false; done = 1.0; } if (t<0) done = 0.0; /* .. break done the steps .. */ switch (step){ default: case 0: /* .. let user define the triangle .. */ vert = true; a = mousex-x-xb; b = mousey-y-yb; init_triangle.vect[0][0] = xb; init_triangle.vect[0][1] = yb; init_triangle.vect[1][0] = xb; init_triangle.vect[1][1] = yb+b; init_triangle.vect[2][0] = xb+a; init_triangle.vect[2][1] = yb+b; three_squares = false; keep_going = false; break; case 1: /* .. centre and scale the triangle and add squares .. */ if (b<0) b*=-1; if (a<0) a*=-1; xb = (W - 2*b - a)/2+b; if (xb < b) { scaling = (float)(2*b+a+10)/(float)W; a = (int)(a/scaling); b = (int)(b/scaling); xb = (W - 2*b - a)/2+b; } yb = (H - 2*a - b)/2+a; if (yb < a) { scaling = (float)(2*a+b+10)/(float)H; a = (int)(a/scaling); b = (int)(b/scaling); xb = (W - 2*b - a)/2+b; yb = (H - 2*a - b)/2+a; } init_triangle.vect[0][0] = xb; init_triangle.vect[0][1] = yb; init_triangle.vect[1][0] = xb; init_triangle.vect[1][1] = yb+b; init_triangle.vect[2][0] = xb+a; init_triangle.vect[2][1] = yb+b; keep_going = false; break; case 2: /* .. put on half the squares .. */ b_tri = true; keep_going = false; a_tri = true; break; case 3: /* .. shear the blue triangle over to the hypotenuse .. */ b_tri = true; b_triangle.vect[2][0] = xb-b + (int)(done*(a+b)); a_tri = true; break; case 4: /* .. rotate it out of it's original square .. */ b_tri = true; b_triangle.vect[2][0] = xb + (int)(b*Math.sin(pi/2*done)+a*Math.cos(pi/2*done)); b_triangle.vect[2][1] = yb + (int)(-a*Math.sin(pi/2*done)+b*Math.cos(pi/2*done)); b_triangle.vect[0][0] = xb + (int)(-b*Math.cos(pi/2*done)); b_triangle.vect[0][1] = yb + (int)(+b*Math.sin(pi/2*done)); a_tri = true; break; case 5: /* .. draw in the hieght .. */ b_tri = true; b_triangle.vect[2][0] = xb +b; b_triangle.vect[2][1] = yb-a; b_triangle.vect[0][0] = xb; b_triangle.vect[0][1] = yb +b; line = true; a_tri = true; keep_going = false; break; case 6: /* .. shear it into the large square .. */ b_tri = true; b_triangle.vect[2][0] = xb +b; b_triangle.vect[2][1] = yb-a; b_triangle.vect[0][0] = xb +(int)(done*(b+(b*b*a)/c2)); b_triangle.vect[0][1] = yb +b -(int)(done*(a+(a*a*b)/c2)); line = true; a_tri = true; break; case 7: /* .. shear the green triangle over to the hypotenuse .. */ b_tri = true; b_triangle.vect[2][0] = xb +b; b_triangle.vect[2][1] = yb-a; b_triangle.vect[0][0] = xb +b +(b*b*a)/c2; b_triangle.vect[0][1] = yb-a+b -(a*a*b)/c2; line = true; a_tri = true; a_triangle.vect[2][1] = yb+a+b -(int)(done*(a+b)); break; case 8: /* .. rotate it out of it's original square .. */ b_tri = true; b_triangle.vect[2][0] = xb +b; b_triangle.vect[2][1] = yb-a; b_triangle.vect[0][0] = xb +b +(b*b*a)/c2; b_triangle.vect[0][1] = yb -a+b -(a*a*b)/c2; line = true; a_tri = true; a_triangle.vect[2][0] = xb+a+ (int)(b*Math.sin(pi/2*done)-a*Math.cos(pi/2*done)); a_triangle.vect[2][1] = yb+b+ (int)(-a*Math.sin(pi/2*done)-b*Math.cos(pi/2*done)); a_triangle.vect[0][0] = xb+a+ (int)(-a*Math.sin(pi/2*done)); a_triangle.vect[0][1] = yb+b+ (int)(+a*Math.cos(pi/2*done)); break; case 9: /* .. shear it into the large square .. */ b_tri = true; b_triangle.vect[2][0] = xb +b; b_triangle.vect[2][1] = yb-a; b_triangle.vect[0][0] = xb +b +(b*b*a)/c2; b_triangle.vect[0][1] = yb-a+b -(a*a*b)/c2; line = true; a_tri = true; a_triangle.vect[2][0] = xb+b+a; a_triangle.vect[2][1] = yb-a+b; a_triangle.vect[0][0] = xb+ (int)(done*(b+(b*b*a)/c2)); a_triangle.vect[0][1] = yb+b -(int)(done*(a+(a*a*b)/c2)); break; case 10: /* .. leave everything .. */ b_tri = true; b_triangle.vect[2][0] = xb +b; b_triangle.vect[2][1] = yb-a; b_triangle.vect[0][0] = xb +b +(b*b*a)/c2; b_triangle.vect[0][1] = yb-a+b -(a*a*b)/c2; line = true; a_tri = true; a_triangle.vect[2][0] = xb+b+a; a_triangle.vect[2][1] = yb-a+b; a_triangle.vect[0][0] = xb+ (b+(b*b*a)/c2); a_triangle.vect[0][1] = yb+b- (a+(a*a*b)/c2); keep_going = false; break; } /* .. draw the stuff .. */ ready = false; /* .. to prevent flick .. */ offscreen.setForeground(background); offscreen.fillRect(0, 0, W, H); offscreen.setForeground(foreground); if (init_tri){ offscreen.setForeground(Color.lightGray); init_triangle.run(); offscreen.setForeground(foreground); } if (b_tri){ offscreen.setForeground(Color.blue); b_triangle.run(); offscreen.setForeground(foreground); } if (a_tri){ offscreen.setForeground(Color.green); a_triangle.run(); offscreen.setForeground(foreground); } if (three_squares){ offscreen.drawRect(xb-b, yb, b, b); offscreen.drawRect(xb, yb+b, a, a); offscreen.drawLine(xb, yb, xb+b, yb-a); offscreen.drawLine(xb+b+a, yb-a+b, xb+b, yb-a); offscreen.drawLine(xb+b+a, yb-a+b, xb+a, yb+b); offscreen.drawLine(xb, yb, xb+a, yb+b); } if (line){ offscreen.setForeground(Color.red); offscreen.drawLine(xb, yb+b, xb+b+(b*b*a)/c2, yb-a+b-(a*a*b)/c2); offscreen.setForeground(foreground); } if (vert){ offscreen.setForeground(Color.red); offscreen.fillRect(xb+a-3, yb+b-3, 6,6); offscreen.setForeground(foreground); } ready = true; parent.repaint(); Thread.sleep(delay); } } /* - - - - - - - - - - - - - - - - - - - - - - */ public void paint(Graphics g) { if (ready) g.drawImage(im, x, y); } } /*----------------------------------------------------------------------*/ /* Pythagoras -- this runs the show. */ /*----------------------------------------------------------------------*/ public class Haven2 extends Applet implements Runnable{ private Btex caption, restart_but, next_but, back_but; private Diag picture; private Thread kicker; private int delay = 100, step=0,nstep, steps = 10,pause; private boolean caption_ready=false, pic_ready=false; private boolean restart_ready=false, next_ready=false, back_ready=false; private int y_button, y_picture, y_caption, y_max=500; private int x_button1, x_button2, x_max=500; private int styles[] = {3,7,1,5,0,6,2,4,5,3,4,7,7}; private int amplitudes[] = {0,0,0,0,3,0,0,3,0,0,0,0,0}; private String caps[] = { "Start with a generic right angled triangle. &font3Each time you click&font0 on the picture window the vertex labeled by the &colour3red square&colour0 will be moved to the mouse position. After you are done the choosing your triangle it will be centred (and possibly flipped so that the right angle is in the bottom left corner of the triangle).", "&subfont2Now we draw squares off each side of the triangle. We need to show that the sum of the areas of the smaller squares equals the area of the large one: &{&font3a&^2/ + b&^2/ = c&^2//, Pythagoras' Theorem.", "&subfont2Instead of showing that the areas of the smaller squares add up to the area of the big one, we will show that half the sum of the areas of the small ones add up to half the area of the big one: &{&font3&{&colour2&fraca&^2//2// + &{&colour1&fracb&^2//2// = &fracc&^2//2//.", "First we will move the &colour1blue area&colour0 over to the big square. Consider the top of the &colour1blue triangle&colour0 the base, and the side is the height. Since the area of the triangle only depends on the base and the height, we can move the bottom vertex without changing the area, provided the height stays fixed. That movement is called a &{&font1shear&font0.", "Now we will rotate the triangle. This also does not affect the area of the &colour1blue triangle&colour0.", "Now if we consider the side of the &colour1blue triangle&colour0 that shares a side with the big square as the base, the &colour3red line&colour0 is the same height as the height of the triangle.", "As before, we leave the height and base fixed and move, or &font1shear&font0, the blue region into the big square (the area of the &colour1blue region&colour0 is still equal to half of the area of the square it came from).", "Similarly, we move the &colour2green region&colour0 to the big square: first with a &font1shear&font0,", "then rotate,", "and another &font1shear&font0.", "&subfont2Since half of each of the rectangles that make up the big square are filled, we can conclude that the sum of the &colour1blue&colour0 and &colour2green&colour0 regions equals half the big square; therefore, the sum of the areas of the small squares equals the area of the big square: &{&font3a&^2/ + b&^2/ = c&^2//. &font1QED"}; /* - - - - - - - - - - - - - - - - - - - - - - */ public void init() { /* .. setup window layout .. */ y_button=44; y_picture=394; y_caption=y_max; x_button1=133; x_button2=x_max-233; try{ pause = Integer.parseInt(getAttribute("pause")); } catch(Exception e) { pause=1000; } resize(x_max,y_max); kicker = new Thread(this); new Thread(kicker).start(); } /* - - - - - - - - - - - - - - - - - - - - - - */ public void run(){ Thread.currentThread().setPriority(Thread.MIN_PRIORITY); Thread.sleep(pause); step = 0; caption = new Btex(this,x_max,y_caption-y_picture-2); caption.x = 0; caption.y = y_picture+2; caption.num_fonts = 4; caption.fontName = new String[caption.num_fonts]; caption.fontName[0] = "TimesRoman16i"; caption.fontName[1] = "TimesRoman24b"; caption.fontName[2] = "TimesRoman10"; caption.fontName[3] = "TimesRoman16"; caption.num_colours = 4; caption.foreground = new Color[caption.num_colours]; caption.foreground[0] = awt.Color.black; caption.foreground[1] = awt.Color.blue; caption.foreground[2] = awt.Color.green; caption.foreground[3] = awt.Color.red; caption.background = awt.Color.lightGray; caption.centre = true; caption.time = 1000; caption.box = true; caption.y_space = 3; caption.style = styles[step]; caption.amplitude = amplitudes[step]; caption.setup_words(caps[step]); new Thread(caption).start(); caption_ready = true; restart_but = new Btex(this,x_max-x_button2-2,y_button); restart_but.x = x_button2+2; restart_but.y = 0; restart_but.num_fonts = 1; restart_but.fontName = new String[restart_but.num_fonts]; restart_but.fontName[0] = "TimesRoman24b"; restart_but.num_colours = 1; restart_but.foreground = new Color[restart_but.num_colours]; restart_but.foreground[0] = awt.Color.lightGray; restart_but.background = awt.Color.black; restart_but.centre = true; restart_but.setup_words("RESTART"); restart_but.time = 500; restart_but.box = true; restart_but.style = 3; new Thread(restart_but).start(); restart_ready = true; next_but = new Btex(this,x_button1,y_button); next_but.x = 0; next_but.y = 0; next_but.num_fonts = 1; next_but.fontName = new String[next_but.num_fonts]; next_but.fontName[0] = "TimesRoman24b"; next_but.num_colours = 1; next_but.foreground = new Color[next_but.num_colours]; next_but.foreground[0] = awt.Color.lightGray; next_but.background = awt.Color.black; next_but.centre = true; next_but.setup_words("NEXT"); next_but.time = 500; next_but.box = true; next_but.style = 2; new Thread(next_but).start(); next_ready = true; back_but = new Btex(this,x_button2-x_button1-2,y_button); back_but.x = x_button1+2; back_but.y = 0; back_but.num_fonts = 1; back_but.fontName = new String[back_but.num_fonts]; back_but.fontName[0] = "TimesRoman24b"; back_but.num_colours = 1; back_but.foreground = new Color[back_but.num_colours]; back_but.foreground[0] = awt.Color.gray; back_but.background = awt.Color.black; back_but.centre = true; back_but.setup_words("BACK"); back_but.time = 0; back_but.box = true; back_but.style = 1; new Thread(back_but).start(); back_ready = true; picture = new Diag(this,x_max,y_picture-y_button-2); picture.x = 0; picture.y = y_button+2; picture.step = step; picture.pause = 1100; new Thread(picture).start(); pic_ready = true; } /* - - - - - - - - - - - - - - - - - - - - - - */ public void paint(Graphics g) { update(g); } public void update(Graphics g) { /* .. these if statements only stop errors when starting .. */ if (caption_ready) caption.paint(g); if (restart_ready) restart_but.paint(g); if (next_ready) next_but.paint(g); if (back_ready) back_but.paint(g); if (pic_ready) picture.paint(g); } /* - - - - - - - - - - - - - - - - - - - - - - */ public void mouseDown(int x, int y) { /* .. respond to a mouse click in the Haven2 window .. */ boolean cap = false, pic = false; if (y steps) nstep = step; if (nstep ==steps) next_but.foreground[0] = awt.Color.gray; else next_but.foreground[0] = awt.Color.lightGray; if (nstep == 1){ back_but.foreground[0] = awt.Color.lightGray; back_but.time = 0; new Thread(back_but).start(); } next_but.time = 500; new Thread(next_but).start(); } else if (x