/*
File: Geometry.java
Description:
Student Name:
Student UT EID:
Partner's Name:
Partner's UT EID:
Course Name: CS 312
Unique Numbers: 91035
Date Created: 08/06/2012
Date Last Modified: 08/08/2012
*/
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 ()
{
x = 0.0;
y = 0.0;
}
// non-default constructors
public Point (double x, double y)
{
this.x = x;
this.y = y;
}
public Point (Point p)
{
x = p.getX();
y = p.getY();
}
// accessors get the x and y coordinates
public double getX ()
{
return x;
}
public double getY ()
{
return y;
}
// mutators set the x and y coordinates
public void setX (double x)
{
this.x = x;
}
public void setY (double y)
{
this.y = y;
}
// test for equality of two doubles
public boolean isEqual (double x, double y)
{
double delta = 1.0e-13; // an arbitrary small number
return (((Math.abs (x - y)) < delta));
}
// distance to another point
public double distance (Point p)
{
return Math.sqrt ((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));
}
// string representation of a point, i.e. x and y coordinates
public String toString ()
{
return "(" + x + ", " + y + ")";
}
// test for equality of two points
public boolean equals (Point p)
{
return (isEqual(x, p.x) && isEqual(y, p.y));
}
}
class Line
{
// list of attributes
private double slope;
private double intercept;
// default constructor (slope = 1 and intercept = 0)
public Line ()
{
slope = 1.0;
intercept = 0.0;
}
// non-default constructors
public Line (double slope, double intercept)
{
this.slope = slope;
this.intercept = intercept;
}
// define line if p and q are not the same
public Line (Point p, Point q)
{
//slope = (y1-y2)/(x1-x2)
//intercept = y1-slope*x
this.slope = (p.getY() - q.getY())/(p.getX() - q.getX());
this.intercept = p.getY() - slope * p.getX();
}
// accessors
public double getSlope ()
{
return slope;
}
public double getIntercept ()
{
return intercept;
}
// mutators
public void setSlope (double slope)
{
this.slope = slope;
}
public void setIntercept (double intercept)
{
this.intercept = intercept;
}
// test for equality of two doubles
public boolean isEqual (double x, double y)
{
double delta = 1.0e-13; // an arbitrary small number
return (((Math.abs (x - y)) < delta));
}
// determine if two lines are parallel
// same slopes <=> parallel
public boolean isParallel (Line line)
{
return isEqual(this.slope, line.slope);
}
// determine the intersection point if two lines are not parallel
public Point intersectionPoint (Line line)
{
Point intersection = new Point();
double m1 = this.getSlope();
double m2 = line.getSlope();
double b1 = this.getIntercept();
double b2 = line.getIntercept();
intersection.setX((b1-b2)/(m2-m1));
intersection.setY(m1*intersection.getX() + b1);
return intersection;
}
// determine if two lines are perpendicular to each other
public boolean isPerpendicular (Line line)
{
//m1 = -(1/m2)
return isEqual(this.getSlope(),-1 * line.getSlope());
}
// determine the perpendicular distance of a point to the line
public double distance (Point p)
{
double m = this.getSlope();
double b = this.getIntercept();
double x = p.getX();
double y = p.getY();
return Math.abs(m*x - y + b)/ Math.sqrt(m*m + 1);
}
// 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)
{
//side1 if y > mx + b; side2 if y < mx + b
double x1 = p.getX();
double y1 = p.getY();
double x2 = q.getX();
double y2 = q.getY();
double m = this.getSlope();
double b = this.getIntercept();
if (y1 == m*x1 + b || y2 == m*x2 + b)
return false;
else
return (y1 > m*x1 + b) == (y2 > m*x2 + b);
}
// string representation of the slope and intercept of a line, e.g.
// slope: 1.0 intercept: 0.0
public String toString ()
{
return ("slope: " + slope + " intercept: " + intercept);
}
// determine if two lines are equal, i.e. have the same slope
// and intercept
public boolean equals (Line line)
{
return(isEqual(this.getSlope(), line.getSlope()) &&
isEqual(this.getIntercept(), line.getIntercept()));
}
}
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 ()
{
v1 = new Point(0,0);
v2 = new Point(1,0);
v3 = new Point(0,1);
}
// 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)
{
if (isTriangle(v1,v2,v3))
{
this.v1 = new Point(v1);
this.v2 = new Point(v2);
this.v3 = new Point(v3);
}
}
public Triangle (double x1, double y1,
double x2, double y2,
double x3, double y3)
{
if (isTriangle(x1, y1, x2, y2, x3, y3))
{
this.v1 = new Point(x1, y1);
this.v2 = new Point(x2, y2);
this.v3 = new Point(x3, y3);
}
}
// accessors
public Point getVertex1 ()
{
return v1;
}
public Point getVertex2 ()
{
return v2;
}
public Point getVertex3 ()
{
return v3;
}
// 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)
{
if(isTriangle(v1, v2, v3))
{
this.v1 = v1;
}
}
public void setVertex2 (Point v2)
{
if(isTriangle(v1, v2, v3))
{
this.v2 = v2;
}
}
public void setVertex3 (Point v3)
{
if(isTriangle(v1, v2, v3))
{
this.v3 = v3;
}
}
public void setVertex1 (double x1, double y1)
{
if (isTriangle(x1, y1, v2.getX(), v2.getY(), v3.getX(), v3.getY()))
{
this.v1 = new Point(x1, y1);
}
}
public void setVertex2 (double x2, double y2)
{
if (isTriangle(v1.getX(), v1.getY(), x2, y2, v3.getX(), v3.getY()))
{
this.v2 = new Point(x2, y2);
}
}
public void setVertex3 (double x3, double y3)
{
if (isTriangle(v1.getX(), v1.getY(), v2.getX(), v2.getY(), x3, y3))
{
this.v3 = new Point(x3, y3);
}
}
// test for equality of two doubles
public boolean isEqual (double x, double y)
{
double delta = 1.0e-13; // an arbitrary small number
return (((Math.abs (x - y)) < delta));
}
// determines if three points form a triangle
private boolean isTriangle (Point p1, Point p2, Point p3)
{
double x1 = p1.getX();
double y1 = p1.getY();
double x2 = p2.getX();
double y2 = p2.getY();
double x3 = p3.getX();
double y3 = p3.getY();
return isEqual((y2 - y1)/(x2-x1), (y3-y2)/(x3-x2));
}
private boolean isTriangle (double x1, double y1,
double x2, double y2,
double x3, double y3)
{
return isEqual((y2 - y1)/(x2-x1), (y3-y2)/(x3-x2));
}
// calculates area of a triangle
public double area ()
{
double base = getVertex1().distance(getVertex2());
Line baseLine = new Line(v1,v2);
double height = baseLine.distance(v3);
return 1/2 * base * height;
}
// calculates the perimeter
public double perimeter ()
{
return v1.distance(v2) + v2.distance(v3) + v3.distance(v1);
}
// determines if a point is inside the triangle
public boolean isInside (Point p)
{
Line lineV1V2 = new Line (v1, v2);
Line lineV2V3 = new Line (v2, v3);
Line lineV1V3 = new Line (v1, v3);
return lineV1V2.onSameSide(p, v3) && lineV2V3.onSameSide(p,v1)
&& lineV1V3.onSameSide(p,v2);
}
// determines if the given triangle is completely inside Triangle t
public boolean isInside (Triangle t)
{
return isInside(t.getVertex1()) && isInside(t.getVertex2())
&& isInside(t.getVertex3());
}
// 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)
{
return isInside(t.getVertex1()) || isInside(t.getVertex2())
|| isInside(t.getVertex3()) || //starOfDavid
}
// determines if a line passes through the triangle
public boolean doesIntersect (Line line)
{
Line lineV1V2 = new Line (v1, v2); //Fix this whole thing
Line lineV2V3 = new Line (v2, v3);
Line lineV1V3 = new Line (v1, v3);
Point pV1V2 = line.intersectionPoint(lineV1V2);
Point pV2V3 = line.intersectionPoint(lineV2V3);
Point pV1V3 = line.intersectionPoint(lineV1V3);
double xV1V2 = pV1V2.getX();
double yV1V2 = pV1V2.getY();
double xV1V3 = pV1V3.getX();
double yV1V3 = pV1V3.getY();
double xV2V3 = pV2V3.getX();
double yV2V3 = pV2V3.getY();
return ((xV1V2 < v1.x) == (xV1V2 > v2.x)) && (xV1V2 < v1.y) == (pV1V2.y > v2.y)) ||
((pV1V3.x < v1.x) == (pV1V3.x > v3.x)) && ((pV1V3.y < v1.y) == (pV1V3.y > v3.y)) ||
((pV1V2.x < v1.x) == (pV1V2.x > v2.x)) && ((pV1V2.x < v1.x) == (pV1V2.x > v2.x))
}
// returns a string representation of a triangle
// i.e. it gives the three vertices
public String toString ()
{
return v1.toString() + ", " + v2.toString() + ", " + v3.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
File input = new File ("geometry.txt");
Scanner sc = new Scanner(input);
// read the coordinates of the first Point P
String [] strArrayP = (sc.nextLine()).split(" ");
Point p = new Point ( Double.parseDouble(strArrayP[0]),
Double.parseDouble(strArrayP [1]));
System.out.println("Coordinates of P: " + p.toString());
// read the coordinates of the second Point Q
String [] strArrayQ = (sc.nextLine()).split(" ");
Point q = new Point ( Double.parseDouble(strArrayQ[0]),
Double.parseDouble(strArrayQ [1]));
System.out.println("Coordinates of Q: " + q.toString());
// print distance between P and Q
System.out.println("Distance between P and Q: " + p.distance(q));
// print the slope and intercept of the line passing through P and Q
Line linePQ = new Line(p,q);
System.out.println("Slope and Intercept of PQ: " + linePQ.toString());
// read the coordinates of the third Point A
String [] strArrayA = (sc.nextLine()).split(" ");
Point a = new Point ( Double.parseDouble(strArrayA[0]),
Double.parseDouble(strArrayA [1]));
System.out.println("Coordinates of A: " + a.toString());
// read the coordinates of the fourth Point B
String [] strArrayB = (sc.nextLine()).split(" ");
Point b = new Point ( Double.parseDouble(strArrayB[0]),
Double.parseDouble(strArrayB [1]));
System.out.println("Coordinates of B: " + b.toString());
// print the slope and intercept of the line passing through A and B
Line lineAB = new Line(a,b);
System.out.println("Slope and Intercept of AB: " + lineAB.toString());
// print if the lines PQ and AB are parallel or not
System.out.println("PQ is " + (linePQ.isParallel(lineAB)? "":"not ")
+ "parallel to AB.");
// print if the lines PQ and AB are perpendicular or not
System.out.println("PQ is " + (linePQ.isPerpendicular(lineAB)? "":"not ")
+ "perpendicular to AB.");
// print the coordinates of the intersection point if PQ is not
// parallel to AB
if (! lineAB.isParallel(linePQ))
{
System.out.println("Coordinates of intersection point of PQ and AB: "
+ lineAB.intersectionPoint(linePQ).toString());
}
// 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"
}
}