CIS 3020 Project 3

From In The Wings
Revision as of 10:00, 30 March 2007 by Jka (talk | contribs) (→‎TwoDimensionalPoint.java)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Background

In this assignment you will perform calculations with points in a 2-dimensional plane, which are defined by their x- and y-coordinates. The notation for a point will be P(x,y). The operations that you will be writing include the comparison of 2 points, the calculation of distance between 2 points, the angle defined by the straight line through 2 points, and the slope of the line that passes through two points. Finally, you will also evaluate whether 3 points are aligned on the same line.

TwoDimensionalPoint.java

import java.lang.*;
public class TwoDimensionalPoint {
  private int x;
  private int y;

  public TwoDimensionalPoint(int x, int y) {
    this.x = x;
    this.y = y;
  }

  public TwoDimensionalPoint() {
    this.x = 0;
    this.y = 0;
  }

  public int getX() {
    return x;
  }

  public int getY() {
    return y;
  }

  public void setLocation(int x, int y) {
    this.x = x;
    this.y = y;
  }

  public boolean equals(TwoDimensionalPoint rn) {
    boolean result = false;
    if((this.getX() == rn.getX()) && (this.getY() == rn.getY()))
    {
      result = true;
    }
    return result;
  }

  public void error(String msg) {
    System.out.println(msg);
  }

  public double perform(String operation, TwoDimensionalPoint rn) {
    double result = 0;
    if (operation.toLowerCase() == "slope")
    {
      result = this.slope(rn);
    }
    else if (operation.toLowerCase() == "distance")
    {
      result = this.distance(rn);
    }
    else if (operation.toLowerCase() == "angle")
    {
      result = this.angle(rn);
    }
    else
    {
      error("You must choose one of these three operations: slop, distance, angle");
    }
    return result;
  }
  public double xDistance(TwoDimensionalPoint p)
  {
    double distance = Math.abs(this.getX() - p.getX());
    return distance;
  }
  public double yDistance(TwoDimensionalPoint p)
  {
    double distance = Math.abs(this.getY() - p.getY());
    return distance;
  }
  public double distance(TwoDimensionalPoint p)
  {
    //    double distance = Math.sqrt(((this.getY() - p.getY())^2) +
    //                  ((this.getX() - p.getX())^2));
    double X = this.xDistance(p);
    double Y = this.yDistance(p);
    double distance = Math.sqrt(((X * X) + (Y * Y)));
    return distance;
  }
  public double slope(TwoDimensionalPoint p)
  {
    /* System.out.println(this.getX());
     *          System.out.println(this.getY());
     *                   System.out.println(p.getX());
     *                            System.out.println(p.getY());
     *                                  */
    double slope = Math.abs(this.yDistance(p)/this.xDistance(p));
    //slope = Math.abs(((p.getY() - this.getY()) / (p.getX() - this.getX())));
    return slope;
  }

  public boolean areAligned(TwoDimensionalPoint p1, TwoDimensionalPoint p2)
  {
    boolean result = false;
    if((this.slope(p1)==p2.slope(this)) && (this.slope(p1)==p2.slope(p1)))
    {
      result = true;
    }
    return result;
  }

  public double angle(TwoDimensionalPoint p)
  {
    double angle = Math.atan((this.yDistance(p)) / (this.xDistance(p)));
    angle = ((180 * angle) / Math.PI);
    return angle;
  }

  public String toString()
  {
    String point = new String();
    point = "(" + this.getX() + ", " + this.getY() + ")";
    return point;
  }
}

PointDemo.java

public class PointDemo
{
  public static void main (String[] args)
  {

    TwoDimensionalPoint P0 = new TwoDimensionalPoint();
    System.out.println("TwoDimensionalPoint 1, P0, has been defined to be at: " + P0.toString());

    TwoDimensionalPoint P1 = new TwoDimensionalPoint(1, 1);
    System.out.println("TwoDimensionalPoint 2, P1, has been defined to be at: " + P1.toString());

    TwoDimensionalPoint P2 = new TwoDimensionalPoint(6, 4);
    System.out.println("TwoDimensionalPoint 3, P2, has been defined to be at: " + P2.toString());

    TwoDimensionalPoint P3 = new TwoDimensionalPoint(4, 4);
    System.out.println("TwoDimensionalPoint 4, P3, has been defined to be at: " + P3.toString());


    //Start distance calculations.

    System.out.println("\nThe distance from " + P0.toString() + " to " + P1.toString() + " is " + P0.perform("distance", P1));
    System.out.println("The distance from " + P0.toString() + " to " + P2.toString() + " is " + P0.perform("distance", P2));
    System.out.println("The distance from " + P0.toString() + " to " + P3.toString() + " is " + P0.perform("distance", P3));

    System.out.println("The distance from " + P1.toString() + " to " + P2.toString() + " is " + P1.perform("distance", P2));
    System.out.println("The distance from " + P1.toString() + " to " + P3.toString() + " is " + P1.perform("distance", P3));

    System.out.println("The distance from " + P2.toString() + " to " + P3.toString() + " is " + P2.perform("distance", P3) + "\n");
    //    Stop distance calculations.

    // Start slope calculations.

    System.out.println("The slope of the line through " + P0.toString() + " to " + P1.toString() + " is " + P0.perform("slope", P1));
    System.out.println("The slope of the line through " + P0.toString() + " to " + P2.toString() + " is " + P2.perform("slope", P0));
    System.out.println("The slope of the line through " + P0.toString() + " to " + P3.toString() + " is " + P3.perform("slope", P0));

    System.out.println("The slope of the line through " + P1.toString() + " to " + P2.toString() + " is " + P2.perform("slope", P1));
    System.out.println("The slope of the line through " + P1.toString() + " to " + P3.toString() + " is " + P1.perform("slope", P3));

    System.out.println("The slope of the line through " + P2.toString() + " to " + P3.toString() + " is " + P2.perform("slope", P3) + "\n");

    // Stop slope calculations.

    //  Start angle calculations.

    System.out.println("The angle of the line through " + P0.toString() + " to " + P1.toString() + " is " + P0.perform("angle", P1));
    System.out.println("The angle of the line through " + P0.toString() + " to " + P2.toString() + " is " + P2.perform("angle", P0));
    System.out.println("The angle of the line through " + P0.toString() + " to " + P3.toString() + " is " + P0.perform("angle", P3));
 
    System.out.println("The angle of the line through " + P1.toString() + " to " + P2.toString() + " is " + P2.perform("angle", P1));
    System.out.println("The angle of the line through " + P1.toString() + " to " + P3.toString() + " is " + P1.perform("angle", P3));
 
    System.out.println("The angle of the line through " + P2.toString() + " to " + P3.toString() + " is " + P2.perform("angle", P3));
    // Stop angle calculations.


    //  Start aligned testing

    if (P0.areAligned(P1, P2))
    {
      System.out.println("The points " + P0.toString() + ", " + P1.toString() + ", and " + P2.toString() + " are aligned");
    }
    if (P0.areAligned(P1, P3))
    {
      System.out.println("The points " + P0.toString() + ", " + P1.toString() + ", and " + P3.toString() + " are aligned");
    }
    if (P0.areAligned(P2, P3))
    {
      System.out.println("The points " + P0.toString() + ", " + P2.toString() + ", and " + P3.toString() + " are aligned");
    }
  }
}