Basic Geometry (Due 21 April 2013)

This assignment is on object oriented programming. You will be developing several classes that are fundamental in Geometry - Point, Line, and Triangle. You will then write a test class that will test some of the methods that you have written.

Here is the skeleton of the code that you will be writing. You will have to write the bodies of all the methods that I have listed. You may always add more methods.

import java.util.*;
import java.io.*;

class Point
{
  // list of attributes - x and y coordinates
  private double x;
  private double y;

  // default constructor
  public Point ()

  // non-default constructor
  public Point (double x, double y)

  // accessors get the x and y coordinates
  public double getX ()
  public double getY ()

  // mutators set the x and y coordinates
  public void setX (double x)
  public void setY (double y)

  // distance to another point
  public double distance (Point p)

  // string representation of a point, i.e. x and y coordinates
  public String toString ()

  // test for equality of two points
  public boolean equals (Point p)
}

class Line
{
  // list of attributes
  private Point p1;
  private Point p2;

  // default constructor (p1 = (0, 0) and p2 (1, 1))
  public Line ()

  // non-default constructors
  public Line (double x1, double y1, double x2, double y2)

  // define line if p and q are not the same
  public Line (Point p, Point q)

  // accessors
  public Point getP1 ()

  public Point getP2 ()

  // mutators
  public void setP1 (double x, double y)

  public void setP2 (double x, double y)

  // determine if line is parallel to x axis
  public boolean isParallelX ()

  // determine if line is parallel to y axis
  public boolean isParallelY ()

  // determine if two lines are parallel
  public boolean isParallel (Line line)

  // get slope of line if not parallel to y axis
  public double slope ()

  // get x axis intercept if not parallel to x axis
  public double xIntercept ()

  // get y axis intercept if not parallel to y axis
  public double yIntercept ()

  // determine the intersection point if two lines are not parallel
  public Point intersectionPoint (Line line)

  // determine if two lines are perpendicular to each other
  public boolean isPerpendicular (Line line)

  // determine the perpendicular distance of a point to the line
  public double distance (Point p)

  // determine if two points are on the same side of the line
  // if one or both points are on the line return false
  public boolean onSameSide (Point p, Point q)

  // string representation of line of the form:
  // y = m * x + c
  // y = c
  // x = c
  public String toString ()

  // determine if two lines are equal, i.e. have the same slope 
  // and intercept
  public boolean equals (Line line)
}

class Triangle
{
  // list attributes
  private Point v1;
  private Point v2;
  private Point v3;

  // default constructor creates a triangle having
  // vertices (0, 0), (1, 0), and (0, 1).
  public Triangle ()

  // non-default constructors accept user defined points
  // and creates triangle object if the points form a
  // triangle or the default triangle if they do not.
  public Triangle (Point v1, Point v2, Point v3)

  public Triangle (double x1, double y1,
                   double x2, double y2,
		   double x3, double y3)

  // accessors
  public Point getVertex1 ()
  public Point getVertex2 ()
  public Point getVertex3 ()

  // mutators reset the vertices only if the triangle shape
  // is preserved i.e. the points do not collapse to a line
  public void setVertex1 (Point v1)
  public void setVertex2 (Point v2)
  public void setVertex3 (Point v3)

  public void setVertex1 (double x1, double y1)
  public void setVertex2 (double x2, double y2)
  public void setVertex3 (double x3, double y3)

  // determines if three points form a triangle
  private boolean isTriangle (Point p1, Point p2, Point p3)
  private boolean isTriangle (double x1, double y1,
                              double x2, double y2,
			      double x3, double y3)

  // calculates area of a triangle
  // if a, b, and c are the sides and s = (a + b + c) / 2
  // area      = Math.sqrt (s * (s - a) * (s - b) * (s - c))
  public double area ()

  // calculates the perimeter
  public double perimeter ()

  // determines if a point is inside the triangle
  public boolean isInside (Point p)

  // determines if the given triangle is completely inside Triangle t
  public boolean isInside (Triangle t)

  // determines if the given triangle overlaps Triangle t, 
  // if it shares some (or all) of its area with Triangle t
  public boolean doesOverlap (Triangle t)

  // determines if a line passes through the triangle
  public boolean doesIntersect (Line line)

  // returns a string representation of a triangle
  // i.e. it gives the three vertices
  public String toString ()

  // determines if two triangles are congruent, i.e. the
  // three sides of one are equal to three sides of the other
  public boolean equals (Triangle t)

}

public class Geometry
{

  public static void main (String[] args) throws IOException
  {
    // open file "geometry.txt" for reading

    // read the coordinates of the first Point P

    // read the coordinates of the second Point Q

    // print distance between P and Q

    // print the slope and intercept of the line passing through P and Q

    // read the coordinates of the third Point A
 
    // read the coordinates of the fourth Point B

    // print the slope and intercept of the line passing through A and B

    // print if the lines PQ and AB are parallel or not

    // print if the lines PQ and AB are perpendicular or not

    // print if coordinates of the intersection point if PQ is not
    // parallel to AB

    // read the coordinates of the fifth Point G

    // read the coordinates of the sixth Point H

    // print if the the points G and H are on the same side of PQ

    // print if the the points G and H are on the same side of AB

    // read the coordinates of the vertices R, S, and T

    // print the perimeter of triangle RST

    // print the area of triangle RST

    // print if the line PQ passes through the triangle RST

    // print if the line AB passes through the triangle RST

    // read the coordinates of the vertices J, K, and L

    // print if triangle JKL is inside triangle RST

    // print if triangle JKL overlaps triangle RST

    // print if triangle JKL is congruent to triangle RST

    // close file "geometry.txt"

  }
   
}

Note that in this program you will be checking for equality of two floating point numbers. Since there is a finite precision in the representation of floating point numbers, it is not always possible to determine exact equality. A working solution to determine equality is to take the difference of the floating point numbers and see if the difference is less than a pre-determined limit. This limit is arbitrary and is often dictated by the problem that you are trying to solve. Here is a method that tests for equality of two doubles.

public boolean isEqual (double x, double y)
{
  double delta = 1.0e-18;    // an arbitrary small number
  return (((Math.abs (x - y)) < delta);
}

You will be reading your input from the file geometry.txt. The format of the file will be exactly like this:

-3.0 2.0                       // coordinates of P
2.0 -3.0                       // coordinates of Q
2.0 1.0                        // coordinates of A
-1.0 -2.0                      // coordinates of B
1.0 1.0                        // coordinates of G
-2.0 -1.0                      // coordinates of H
-1.0 4.0 -3.0 1.0 2.0 2.0      // coordinates of the vertices R, S, and T
-2.0 2.0 -3.0 -2.0 2.0 -1.0    // coordinates of the vertices J, K, and L
The structure of your output will be as follows:
Coordinates of P: (-3.0, 2.0)
Coordinates of Q: (2.0, -3.0)
Distance between P and Q:
Slope and Intercept of PQ:
Coordinates of A: (2.0, 1.0)
Coordinates of B: (-1.0, -2.0)
Slope and Intercept of AB:
PQ (is / is not) parallel to AB
PQ (is / is not) perpendicular to AB
Coordinates of intersection point of PQ and AB:
Coordinates of G: (1.0, 1.0)
Coordinates of H: (-2.0, -1.0)
G and H (are / are not) on the same side of PQ
G and H (are / are not) on the same side of AB
Vertices of triangle RST: (-1.0, 4.0), (-3.0, 1.0), (2.0, 2.0)
Perimeter of triangle RST:
Area of triangle RST:
PQ (does / does not) pass through triangle RST
AB (does / does not) pass through triangle RST
Vertices of triangle JKL: (-2.0, 2.0), (-3.0, -2.0), (2.0, -1.0)
Triangle JKL (is / is not) inside triangle RST
Triangle JKL (does / does not) overlap triangle RST
Triangle JKL (is / is not) congruent to triangle RST

We will be looking at good documentation, design, and adherence to the coding convention mentioned below. You may use the same variable names used in the problem statement or come up with your own. Your code must have the following header:

/*
  File: Geometry.java

  Description:

  Student Name:

  Student UT EID:

  Course Name: CS 312

  Unique Numbers: 

  Date Created:

  Date Last Modified:

*/

You will follow the standard Java Coding Conventions. You can either view the HTML page or download the PDF or Postscript and print it out. There is a modification that I would like to make to the standard coding conventions. Please align the opening and closing braces vertically so that you can easily make out the blocks of code. For example:

Do this:
if ( x > 5 )
{
  a = b + c;
}

Not this:
if ( x > 5 ) {
  a = b + c;
}

Use the turnin program to submit your Geometry.java file. We should receive your work by 11 PM on Sunday 21 April 2013. There will be substantial penalties if you do not adhere to the guidelines.