Question: operations on geometric objects. Specifically, the program will create lines and circles using points, and calculate standard properties, including the points of intersection between them.
operations on geometric objects. Specifically, the program will create lines and circles using points, and calculate standard properties, including the points of intersection between them. For this assignment you must implement various member methods in the Point2D, Line2D, and Circle2D objects.
1. Calculate the area of a Circle2D double getArea()
2. Calculate the circumference of a Circle2D double getCircumference()
3. Determine whether a Point2D is on a Line2D boolean pointExists(Point2D pt)
4. Implement a Line2D constructor using two Point2D objects Line2D(Point2D ptA, Point2D ptB)
5. Determine whether a Point2D is on a Circle2D boolean pointExists(Point2D pt)
6. Calculate the intersection of two Line2D objects List intersect(Line2D lA, Line2D lB)
7. Implement a method to combine the redundant lines in main() that print the intersection points. Many methods have already been provided, such as most get methods, set methods, and a method to find the intersection points between a Circle2D and Line2D.
Part2
1. The intersections of two Circle2D objects List intersect(Circle2D cA, Circle2D cB) There is code in main() that must be used to test this method. (2%)
2. Implement the Deg2Curve2D class, which has the form y = ax2 + bx + c. The code in main() should be used to test this class. If you complete this task, make sure to uncomment the lines in main() which use Deg2Curve2D. (2%) the List intersect(Circle2D cA, Circle2D cB) method must calculate the intersections between two Circle2D objects.
This method, like the other two intersect methods, should be a static method in the public class (XXcsc210hw3). The intersection should be solved in the same way as the two line case. Set the equations to be equal,noting that the equation of a circle is r 2 = (x a) 2 + (y b) 2 where r is the radius, and (a, b) is the center of the circle. To calculate the intersections between two circles, use the equality (x a1) 2 + (y b1) 2 r 2 1 = (x a2) + (y b2) 2 r 2 2 When solving this, make sure you check whether a value is positive before using the Math.sqrt() method. If your program is trying to calculate the square root of a negative number, then the circles do not intersect.
Part3
1. Provide a default constructor and set a = 1, b = 0, c = 0 public Deg2Curve2D()
2. Provide a constructor given a, b, and c public Deg2Curve2D(double a, double b, double c)
3. Get methods for a, b, and c
4. Set methods for a, b, and c
5. Whether a Point2D is on a Deg2Curve2D boolean pointExists(Point2D pt)
Skeleton Body:
import java.util.List; import java.util.ArrayList;
class Point2D { ////////////////////////// // private members private double x; private double y; ////////////////////////// // constructors public Point2D() { this.x = 0; this.y = 0; } public Point2D(double x, double y) { this.x = x; this.y = y; } ////////////////////////// // getters and setters public double getX() { return this.x; } public double getY() { return this.y; } public void setX(double x) { this.x = x; } public void setY(double y) { this.y = y; } ////////////////////////// // quick print info for convenience public String info() { return "[Point: (" + String.format("% 6.4f",this.x) + ", " + String.format("% 6.4f",this.y) + ")]"; } } class Circle2D { ////////////////////////// // private members private Point2D center; private double radius; ////////////////////////// // constructors public Circle2D() { this.center = new Point2D(); this.radius = 0.0; } public Circle2D(Point2D pt, double r) { this.center = pt; this.radius = r; } ////////////////////////// // getters and setters public Point2D getCenter() { return this.center; } public double getRadius() { return this.radius; } public void setCenter(Point2D pt) { this.center = pt; } public void setRadius(double r) { this.radius = r; } public double getArea() { //Q1: implement this method double result = 0.0; return result; } public double getCircumference() { //Q2: implement this method double result = 0.0; return result; } public boolean pointExists(Point2D pt){ //Q5: implement this method boolean exists = false; return exists; } ////////////////////////// // quick print info for convenience public String info() { return "[Circle: center = " + this.center.info() + ", r = " + this.radius + "]"; } } class Line2D { ////////////////////////// // private members private double slope; private double intercept; ////////////////////////// // constructors public Line2D() { this.slope = 0.0; this.intercept = 0.0; } public Line2D(double slope, double intercept) { this.slope = slope; this.intercept = intercept; } public Line2D(Point2D ptA, Point2D ptB) { //Q4: implement this constructor } ///////////////////////// // getters and setters public double getSlope() { return this.slope; } public double getIntercept() { return this.intercept; } public void setSlope(double m) { this.slope = m; } public void setIntercept(double b) { this.intercept = b; } public boolean pointExists(Point2D pt){ //Q3: implement this method boolean exists = false; return exists; } ////////////////////////// // quick print info for convenience public String info() { return "[Line: m = " + this.slope + ", b = " + this.intercept + "]"; } } class Deg2Curve2D { // Extra credit: implement a class for second-order polynomials // of the form y = ax^2 + bx + c // To earn extra credit, implement getA, getB, getC, // setA, setB, setC, appropriate constructors, and pointExists(Point2D pt)
} public class SKELcsc210hw3 { // intersect methods are here, in the public class // These methods return a list holding Point2D objects private static List
private static List
// Calculates the intersections between a line and a circle // This method is provided to detail how to use the objects private static List
// Lines follow formula y = mx + i // Circles follow formula r^2 = (x-a)^2 + (y-b)^2 // // The following substitutes y with mx + i into the circle equation and // solves for x using the quadratic formula. // The resulting x value is then used to calculate y for // the line and circle. If they match, the x,y pair is an intersecting // point. Since there are two square roots involved (one for quadratic // and one for the circle), there are 4 possible combinations to check.
//the calculations below are from expanding (x-a)^2 + (mx+i-b)^2 - r^2 = 0 //Note that all values are known except x.
//A B C used for the quadratic formula //need to check for +- due to sqrt double A = m*m + 1; double B = 2.0 * (-a + m*i - m*b); double C = a*a - r*r + Math.pow(b-i,2.0); double BAC = B*B - 4.0*A*C; //Since we have to do sqrt of BAC, if it is negative then //there are no solutions, ie no intersections //Just return with result being empty if(BAC < 0.0) return result; xplus = ( -B + Math.sqrt(BAC) ) / (2.0*A); xminus = ( -B - Math.sqrt(BAC) ) / (2.0*A); //circle equation is +- due to sqrt yplus = Math.sqrt(r*r - Math.pow(xplus-a,2.0)) + b; //check if line equation matches the circle equation if( Math.abs(m*xplus + i - yplus) < eps ) //this point is on both the line and the circle. intersection! result.add(new Point2D(xplus,yplus)); //the if/else is in case the +- solution is the same; we dont //want to count the same point twice else { yminus = -Math.sqrt(r*r - Math.pow(xplus-a,2.0)) + b; if( Math.abs(m*xplus + i - yminus) < eps ) result.add(new Point2D(xplus,yminus)); }
//repeat above except for change xplus with xminus //however skip if xplus and xminus the same (also if eg 0 and -0) if(Math.abs(xplus - xminus) > eps) { yplus = Math.sqrt(r*r - Math.pow(xminus-a,2.0)) + b; if( Math.abs(m*xminus + i - yplus) < eps ) result.add(new Point2D(xminus,yplus)); else { yminus = -Math.sqrt(r*r - Math.pow(xminus-a,2.0)) + b; if( Math.abs(m*xminus + i - yminus) < eps ) result.add(new Point2D(xminus,yminus)); } } return result; } //for convenience; just swap arguments and call method above private static List
// Tests for Q3: System.out.print(" "); System.out.print("********************************************* "); System.out.print("*** Testing Q3: Line2D pointExists *** "); System.out.print("********************************************* "); Point2D somePoint = new Point2D(3.0,3.0); line.setSlope(1.0); line.setIntercept(0.0); System.out.println("Is " + somePoint.info() + " on " + line.info() + "?: " + line.pointExists(somePoint) + " ** should be true **");
somePoint.setX(2.0); System.out.println("Is " + somePoint.info() + " on " + line.info() + "?: " + line.pointExists(somePoint) + " ** should be false **");
// Test for Q4 System.out.print(" "); System.out.print("********************************************** "); System.out.print("*** Testing Q4: Line2D constructor *** "); System.out.print("********************************************** ");
Point2D firstPt = new Point2D( 1.5,6.0); Point2D secondPt = new Point2D(-1.5,0.0); Line2D someLine = new Line2D(firstPt, secondPt); System.out.println("someLine is " + someLine.info()); System.out.println(" ** slope should be 2.0000 **"); System.out.println(" ** intercept should be 3.0000 **");
// Test for Q5 System.out.print(" "); System.out.print("********************************************* "); System.out.print("*** Testing Q5: Circle2D pointExists *** "); System.out.print("********************************************* "); somePoint.setX(1.0); somePoint.setY(4.0); System.out.println("Is " + somePoint.info() + " on " + circ.info() + "?: " + circ.pointExists(somePoint) + " ** should be false **"); somePoint.setX(1.5); somePoint.setY(3.0 + Math.sqrt(0.75)); System.out.println("Is " + somePoint.info() + " on " + circ.info() + "?: " + circ.pointExists(somePoint) + " ** should be true **"); // Test for Q6 System.out.print(" "); System.out.print("********************************************* "); System.out.print("*** Testing Q6: Line2D intersection *** "); System.out.print("********************************************* "); System.out.println("Calculating intersection between " + line.info() + " and " + line2.info()); intersectPoints = intersect(line, line2); System.out.println("There were " + intersectPoints.size() + " intersections " + " ** should be 1 at (1.0000, 1.0000) **"); for(Point2D pt: intersectPoints ) { System.out.println(pt.info()); } System.out.println(' ');
System.out.println("Calculating intersection between " + line.info() + " and " + line3.info()); intersectPoints = intersect(line, line3); System.out.println("There were " + intersectPoints.size() + " intersections " + " ** should be 0 **"); for(Point2D pt: intersectPoints ) { System.out.println(pt.info()); }
// Q7: The following chunk of code is copied 3 times. Make a method to // that prints the intersections. Use the method here for each chunk. System.out.print(" "); System.out.print("********************************************** "); System.out.print("*** Testing Q7: Method creation *** "); System.out.print("********************************************** "); //intersection between a circle and a line intersectPoints = intersect(circ, line); //////////////////////////////// // chunk 1 System.out.println("Calculated intersections between " + circ.info() + " and " + line.info()); System.out.println("There were " + intersectPoints.size() + " intersections"); //this loop will not do anything if intersectPoints is empty. Uses //Java-style loop with iterators for(Point2D pt: intersectPoints ) { System.out.println(pt.info()); } System.out.println(); // end chunk 1 //////////////////////////////// line = new Line2D(2.0,1.0); intersectPoints = intersect(circ, line); //////////////////////////////// // chunk 2 System.out.println("Calculated intersections between " + circ.info() + " and " + line.info()); System.out.println("There were " + intersectPoints.size() + " intersections"); for(Point2D pt: intersectPoints ) { System.out.println(pt.info()); } System.out.println(); // end chunk 2 ////////////////////////////////
line = new Line2D(0.5,-5.0); intersectPoints = intersect(circ, line); //////////////////////////////// // chunk 3 System.out.println("Calculated intersections between " + circ.info() + " and " + line.info()); System.out.println("There were " + intersectPoints.size() + " intersections"); for(Point2D pt: intersectPoints ) { System.out.println(pt.info()); } System.out.println(); // end chunk 3 //////////////////////////////// // Extra Credit // Determine the intersections between two Circle2D objects System.out.print(" "); System.out.print("********************************************* "); System.out.print("*** Testing EC1: Circle2D intersection *** "); System.out.print("********************************************* "); System.out.println("Calculating circle intersection..."); intersectPoints = intersect(circ, new Circle2D(new Point2D(3.0,2.0),1.0)); System.out.println("There were " + intersectPoints.size() + " intersections " + " ** should be 2 at (2.0000, 2.0000) and (3.0000, 3.0000) **"); for(Point2D pt: intersectPoints ) { System.out.println(pt.info()); }
// Extra Credit // Implement the Deg2Curve2D class // Uncomment if necessary. /* System.out.print("********************************************* "); System.out.print("*** Testing EC2: Deg2Curve2D class *** "); System.out.print("********************************************* "); Deg2Curve2D curveA = new Deg2Curve2D(); Deg2Curve2D curveB = new Deg2Curve2D(1.0,-2.0,1.0); somePoint.setX(1.0); somePoint.setY(0.0); System.out.println("Is " + somePoint.info() + " on " + curveA + "?: " + curveA.pointExists(somePoint) + " ** should be false **"); System.out.println("Is " + somePoint.info() + " on " + curveB + "?: " + curveB.pointExists(somePoint) + " ** should be true **"); */
System.out.print(" "); System.out.print("********************************************* "); System.out.print(" \"LIFE CAN ONLY BE UNDERSTOOD BACKWARD, BUT " + " MUST BE LIVED FORWARD.\" -- KIERKEGAARD ");
System.out.print("********************************************* "); System.out.print("*** Homework 3 complete! *** "); System.out.print("********************************************* ");
} }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
