CIS 3020 Project 3
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; } }