Two points on line 1 are given as (x1, y1) and (x2, y2) and on line 2 as (x3, y3
ID: 3787320 • Letter: T
Question
Two points on line 1 are given as (x1, y1) and (x2, y2) and on line 2 as (x3, y3) and (x4, y4), as shown in Figure 3.8a-b. The intersecting point of the two lines can be found by solving the following linear equation: (y1-y2)x - (x1 - x2)y = (y1 - y2)x1 - (x1 - x2)y1 (y3 - y4)x - (x3 - x4)y = (y3 - y4)x3 - (x3 - x4)y3 This linear equation can be solved using Cramer's rule (see Programming Exercise 3.3). If the equation has no solutions, the two lines are parallel (Figure 3.8c). Write a program that prompts the user to enter four points and displays the intersecting point. Here are sample runs:Explanation / Answer
import javax.swing.JOptionPane;
class MyExample {
private static double x1;
private static double x2;
private static double x3;
private static double x4;
private static double y1;
private static double y2;
private static double y3;
private static double y4;
private static double intersectionp;
public static void main(String[] args) {
MyExample p=new MyExample();
p.intersection();
}
public void intersection(){
String userInputx1 = JOptionPane.showInputDialog("Enter the first x cordinate of the first line:");
// System.out.println(userInputx1); //to test if working
x1 = Double.parseDouble(userInputx1);
String userInputx2 = JOptionPane.showInputDialog("Enter the second x cordinate of the first line:");
x2 = Double.parseDouble(userInputx2);
String userInputx3 = JOptionPane.showInputDialog("Enter the first x cordinate of the second line:");
x3 = Double.parseDouble(userInputx3);
String userInputx4 = JOptionPane.showInputDialog("Enter the second x cordinate of the second line:");
x4 = Double.parseDouble(userInputx4);
String userInputy1 = JOptionPane.showInputDialog("Enter the first y cordinate of the first line:");
y1 = Double.parseDouble(userInputy1);
String userInputy2 = JOptionPane.showInputDialog("Enter the second y cordinate of the first line:");
y2 = Double.parseDouble(userInputy2);
String userInputy3 = JOptionPane.showInputDialog("Enter the first y cordinate of the second line:");
y3 = Double.parseDouble(userInputy3);
String userInputy4 = JOptionPane.showInputDialog("Enter the second y cordinate of the second line:");
y4 = Double.parseDouble(userInputy4);
double d = (x1-x2)*(y3-y4) - (y1-y2)*(x3-x4);
if (d == 0) System.out.println("Parallel");;
double xi = ((x3-x4)*(x1*y2-y1*x2)-(x1-x2)*(x3*y4-y3*x4))/d;
double yi = ((y3-y4)*(x1*y2-y1*x2)-(y1-y2)*(x3*y4-y3*x4))/d;
System.out.println(intersectionp);
// return new Point(xi,yi);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.