this is the question Part one Write a Point class. A Point should have the follo
ID: 3550775 • Letter: T
Question
this is the question
Part one
Write a Point class. A Point should have the following:
- two private double fields, x and y, representing the coordinates of the Point
- a constructor that takes two double parameters with which to initialize a Point's fields
- get and set methods for the x and y coordinates
Part two
will be a Line class instead of a LineSegment class. It will still be defined by two points - they just won't be endpoints (since a line is infinite), so you can call them p1 and p2 instead. You still need methods for getting and setting those two points. There will still be an intersects method, but the header will now look like this: "public Point intersects (Line otherLine)". Instead of returning a boolean, it will now return a new Point object representing where the two Lines intersect. If the two Lines are parallel, you should return null.
Part three
will just be writing a void method that takes as a parameter an array of Lines and prints out the location of each intersection. The intersection of any two lines should only be printed once (in other words, don't print the intersection of line1 and line2, and then print the intersection of line2 and line1).
Explanation / Answer
import java.util.Scanner; /* * To change this template, choose Tools | Templates * and open the template in the editor. */
/** * * @author Steves */ public class Temp {
public static void main(String[] args) { Scanner s = new Scanner(System.in); double x1,x2,x3,x4,y1,y2,y3,y4; System.out.println("Enter co ordinates first line points / second line points x1, y1..x2,y2...x3,y3...x4,y4 "); x1=s.nextDouble(); y1=s.nextDouble(); x2=s.nextDouble(); y2=s.nextDouble(); x3=s.nextDouble(); y3=s.nextDouble(); x4=s.nextDouble(); y4=s.nextDouble(); Line L1 = new Line(new Point(x1, y1), new Point(x2, y2)); Line L2 = new Line(new Point(x3, y3), new Point(x4, y4)); System.out.println("Intersection Point : " + L1.intersects(L2).toString());
System.out.println(""); } }
class Point {
private double x, y;
Point(double d, double d0) { this.x=d; this.y=d0; }
public double getX() { return x; }
public void setX(int x) { this.x = x; }
public double getY() { return y; }
public void setY(int y) { this.y = y; } @Override public String toString(){ return "("+this.x +", " +this.y+")"; } }
class Line {
Point p1, p2;
public Point getP1() { return p1; }
public void setP1(Point p1) { this.p1 = p1; }
public Point getP2() { return p2; }
Line(Point point, Point point0) { this.p1 =point; this.p2=point0; }
public void setP2(Point p2) { this.p2 = p2; }
public Point intersects(Line otherLine) {
Point point = null; if ((this.getP1().getX() - this.getP2().getX()) * (otherLine.getP1().getY() - otherLine.getP2().getY()) == (this.getP1().getY() - this.getP2().getY()) * (otherLine.getP1().getX() - otherLine.getP2().getX())) //means lines are parllel return null; { point = null; } double denominator = ((this.getP1().getX() - this.getP2().getX()) * (otherLine.getP1().getY() - otherLine.getP2().getY())) - ((this.getP1().getY() - this.getP2().getY()) * (otherLine.getP1().getX() - otherLine.getP2().getX()));
double intersecX = (otherLine.getP1().getX() - otherLine.getP2().getX()) * (this.getP1().getX() * this.getP2().getY() - this.getP1().getY() * this.getP2().getX()) - (this.getP1().getX() - this.getP2().getX()) * ((otherLine.getP1().getX() * otherLine.getP2().getY() - otherLine.getP1().getY() * otherLine.getP2().getX())); double intersecY = (otherLine.getP1().getY() - otherLine.getP2().getY()) * (this.getP1().getX() * this.getP2().getY() - this.getP1().getY() * this.getP2().getX()) - (this.getP1().getY() - this.getP2().getY()) * ((otherLine.getP1().getX() * otherLine.getP2().getY() - otherLine.getP1().getY() * otherLine.getP2().getX())); point = new Point(intersecX / denominator, intersecY / denominator);
return point; }
public void intersectionPoints(Line[] array) { for (int i = 1; i < array.length; i += 2) { System.out.println("Intersection Point " + array[i - 1].intersects(array[i]).toString()); } } }
import java.util.Scanner; /* * To change this template, choose Tools | Templates * and open the template in the editor. */
/** * * @author Steves */ public class Temp {
public static void main(String[] args) { Scanner s = new Scanner(System.in); double x1,x2,x3,x4,y1,y2,y3,y4; System.out.println("Enter co ordinates first line points / second line points x1, y1..x2,y2...x3,y3...x4,y4 "); x1=s.nextDouble(); y1=s.nextDouble(); x2=s.nextDouble(); y2=s.nextDouble(); x3=s.nextDouble(); y3=s.nextDouble(); x4=s.nextDouble(); y4=s.nextDouble(); Line L1 = new Line(new Point(x1, y1), new Point(x2, y2)); Line L2 = new Line(new Point(x3, y3), new Point(x4, y4)); System.out.println("Intersection Point : " + L1.intersects(L2).toString());
System.out.println(""); } }
class Point {
private double x, y;
Point(double d, double d0) { this.x=d; this.y=d0; }
public double getX() { return x; }
public void setX(int x) { this.x = x; }
public double getY() { return y; }
public void setY(int y) { this.y = y; } @Override public String toString(){ return "("+this.x +", " +this.y+")"; } }
class Line {
Point p1, p2;
public Point getP1() { return p1; }
public void setP1(Point p1) { this.p1 = p1; }
public Point getP2() { return p2; }
Line(Point point, Point point0) { this.p1 =point; this.p2=point0; }
public void setP2(Point p2) { this.p2 = p2; }
public Point intersects(Line otherLine) {
Point point = null; if ((this.getP1().getX() - this.getP2().getX()) * (otherLine.getP1().getY() - otherLine.getP2().getY()) == (this.getP1().getY() - this.getP2().getY()) * (otherLine.getP1().getX() - otherLine.getP2().getX())) //means lines are parllel return null; { point = null; } double denominator = ((this.getP1().getX() - this.getP2().getX()) * (otherLine.getP1().getY() - otherLine.getP2().getY())) - ((this.getP1().getY() - this.getP2().getY()) * (otherLine.getP1().getX() - otherLine.getP2().getX()));
double intersecX = (otherLine.getP1().getX() - otherLine.getP2().getX()) * (this.getP1().getX() * this.getP2().getY() - this.getP1().getY() * this.getP2().getX()) - (this.getP1().getX() - this.getP2().getX()) * ((otherLine.getP1().getX() * otherLine.getP2().getY() - otherLine.getP1().getY() * otherLine.getP2().getX())); double intersecY = (otherLine.getP1().getY() - otherLine.getP2().getY()) * (this.getP1().getX() * this.getP2().getY() - this.getP1().getY() * this.getP2().getX()) - (this.getP1().getY() - this.getP2().getY()) * ((otherLine.getP1().getX() * otherLine.getP2().getY() - otherLine.getP1().getY() * otherLine.getP2().getX())); point = new Point(intersecX / denominator, intersecY / denominator);
return point; }
public void intersectionPoints(Line[] array) { for (int i = 1; i < array.length; i += 2) { System.out.println("Intersection Point " + array[i - 1].intersects(array[i]).toString()); } } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.