Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Geometry: intersecting point) Suppose two line segments intersect. The two endp

ID: 3848457 • Letter: #

Question

(Geometry: intersecting point) Suppose two line segments intersect. The two endpoints for the first line segment are (x1, y1) and (x2, y2) and for the second line segment are (x3, y3) and (x4, y4). Write a program that prompts the user to enter these four endpoints and displays the intersecting point. As discussed in Programming Exercise 3.25, the intersecting point can be found by solving a linear equation. Use the LinearEquation class in Programming Exercise 9.11 to solve this equation. See Programming Exercise 3.25 for sample runs.

LinearEquation Class from 9.11: https://www.chegg.com/homework-help/intro-to-java-programming-brief-version-10th-edition-chapter-9-problem-11PE-solution-9781292078564

Explanation / Answer

Program

import java.util.Scanner;

// main class à program execution starts here

public class Program {

public static void main(String[] args) {

Scanner input = new Scanner(System.in); // input scanner

// Prompt the user to enter four endpoints

System.out.print("Enter x1, y1, x2, y2, x3, y3, x4, y4: ");

double[][] points = new double[4][2];

for (int i = 0; i < points.length; i++)

for (int j = 0; j < points[i].length; j++)

points[i][j] = input.nextDouble();

LinearEquation linear = LinearEquation. IntersectPoint(points);

if (linear.isSolvable()) {

System.out.println("The intersecting point is at (" + linear.getX() + ", " + linear.getY() + ")");

}

else {

System.out.println("The two lines are parallel");

}

}

}

// Here is the LinearEquation class formed based on the given information from the exercise

public class LinearEquation {
private double a; // Declarations
private double b;
private double c;
private double d;
private double e;
private double f;
public LinearEquation(double a, double b, double c, double d, double e, double f) { // Constructor class
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.e = e;
this.f = f;
}
// get and set functions for a parameterized objects
public double getA() {
return a;
}
public void setA(double a) {
this.a = a;
}
public double getB() {
return b;
}
public void setB(double b) {
this.b = b;
}
public double getC() {
return c;
}
public void setC(double c) {
this.c = c;
}
public double getD() {
return d;
}
public void setD(double d) {
this.d = d;
}
public double getE() {
return e;
}
public void setE(double e) {
this.e = e;
}
public double getF() {
return f;
}
public void setF(double f) {
this.f = f;
}
private double denominator(){
return a * d - b * c;
}
public double getX() {
return (e * d - b * f) /denominator(); // since, x = ed-bf/ad-bc
}
public double getY() {
return (a * f - e * c) /denominator(); // since, y = af-ec/ad-bc
}
// checks whether the object is solvable or not and If there is no solution the lines are parallel
public boolean isSolvable(){
return denominator() != 0;
}

/* co-ordinates with different types of function calls
public static LinearEquation IntersectPoint(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) {
double a = (y1 - y2);
double b = (-x1 + x2);
double c = (y3 - y4);
double d = (-x3 + x4);
double e = -y1 * (x1 - x2) + (y1 - y2) * x1;
double f = -y3 * (x3 - x4) + (y3 - y4) * x3;
return new LinearEquation(a,b,c,d,e,f);
}
public static LinearEquation IntersectPoint(MyPoint p1, MyPoint p2, MyPoint p3, MyPoint p4) {
return IntersectPoint(p1.x(), p1.y(), p2.x(), p2.y(),p3.x(), p3.y(), p4.x(), p4.y());
}
public static LinearEquation IntersectPoint(MyPoint[] p) {
return IntersectPoint(p[0], p[1], p[2], p[3]);
}
*/
// we used below function in main as function call
public static LinearEquation IntersectPoint(double[][] points) {
return IntersectPoint(points[0][0],points[0][1],points[1][0],points[1][1],points[2][0],points[2][1],points[3][0],points[3][1]);
}
}