CS 100: Lecture L25
April 27
Demo for the point, triangle, and rightTriangle classes:
import java.io.*; import java.awt.*; public class L25Show extends Frame { public static void ShowTriangle(Graphics g,Triangle T, Color c) { double[][] V = T.getVertices(); int[] h = new int[3]; int[] v = new int[3]; h[0] = 400 + (int)V[0][0]; v[0] = 300 - (int)V[0][1]; h[1] = 400 + (int)V[1][0]; v[1] = 300 - (int)V[1][1]; h[2] = 400 + (int)V[2][0]; v[2] = 300 - (int)V[2][1]; g.setColor(c); g.drawLine(h[0],v[0],h[1],v[1]); g.drawLine(h[2],v[2],h[1],v[1]); g.drawLine(h[0],v[0],h[2],v[2]); g.fillPolygon(h,v,3); } public void paint(Graphics g) { point p0 = new point(-120,-100); point p1 = new point(-100,-50); point p2 = new point(-200,0); Triangle T0 = new Triangle(p0,p1,p2); ShowTriangle(g,T0,Color.blue); Triangle T1 = new Triangle(new point(-200,100), 100.); ShowTriangle(g,T1,Color.red); rightTriangle T2 = new rightTriangle(new point(-200,-200),100,70); ShowTriangle(g,T2,Color.green); } } public class L25 { public static void main(String args[]) { L25Show d = new L25Show(); d.resize(800,600); d.move(0,75); d.setTitle("Drawing"); d.show(); d.toFront(); } }
The point class.
// An instance of this class is a point in the xy-plane. public class point { private double x; private double y; // The Constructors: public point(double xVal, double yVal) { x = xVal; y = yVal; } public point(point P) { x = P.x; y = P.y; } public point(double s) { x = -s + 2*s*Math.random(); y = -s + 2*s*Math.random(); } // Move the point a units in the x-direction // and b units in the y-direction. public void translate(double a, double b) { x = x+a; y = y+b; } // Yields the x-coordinate of the point. public double get_x() { return x; } // Yields the y-coordinate of the point. public double get_y() { return y; } // Yields the distance from P0 to P1. public static double dist(point P0, point P1) { return Math.sqrt((P0.x-P1.x)*(P0.x-P1.x) + (P0.y-P1.y)*(P0.y-P1.y)); } // Yields a string representation of the point. public String pointString() { return "(" + String.valueOf(x) + "," + String.valueOf(y) + ")"; } }
The triangle class
// An instance of this class is a triangle. public class Triangle { protected point P0,P1,P2; // The triangle's vertices // Constructor for a triangle with vertices rho0, rho1, rho2. public Triangle(point v0, point v1, point v2) { P0 = new point(v0); P1 = new point(v1); P2 = new point(v2); } // Constructor for equilateral triangle with // center cent and radius r. public Triangle(point cent, double r) { double a = r*Math.sqrt(3)/2; double b = r/2; double xc = cent.get_x(); double yc = cent.get_y(); P0 = new point(xc-a,yc-b); P1 = new point(xc+a,yc-b); P2 = new point(xc,yc+r); } // Constructor for the empty triangle public Triangle() { P0 = new point(0,0); P1 = new point(0,0); P2 = new point(0,0); } // Yields the area of this triangle. public double Area() { double a = point.dist(P0,P1); double b = point.dist(P1,P2); double c = point.dist(P2,P0); double s = (a+b+c)/2; return Math.sqrt(s*(s-a)*(s-b)*(s-c)); } // Yields a 3-by-2 array whose rows house the x and y // coordinates of the triangle's vertices. public double[][] getVertices() { double[][] A = new double[3][2]; A[0][0] = P0.get_x(); A[0][1] = P0.get_y(); A[1][0] = P1.get_x(); A[1][1] = P1.get_y(); A[2][0] = P2.get_x(); A[2][1] = P2.get_y(); return A; } }
The rightTriangle class
// An instance of this class is a right triangle. public class rightTriangle extends Triangle { // Constructor for right triangle with 90 degree // angle at P0 and legs with length a and b. public rightTriangle(point Q, double a, double b) { double Qx = Q.get_x(); double Qy = Q.get_y(); P0 = new point(Qx,Qy); P1 = new point(Qx+a,Qy); P2 = new point(Qx,Qy+b); } // The hypotenuse of this triangle. public double hypot() { return point.dist(P1,P2); } // The area of this triangle. public double Area() { double side1 = point.dist(P0,P1); double side2 = point.dist(P0,P2); return side1*side2/2; } }