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

Java Suppose two line segments intersect. The two end- points for the first line

ID: 3783525 • Letter: J

Question

Java

Suppose two line segments intersect. The two end- points 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 Linear Equation class in Programming Exercise 9.11 to solve this equation. See Programming Exercise 3.25 for sample runs.

Explanation / Answer

import java.io.*;
import java.util.*;

class Intersection{

int x1,y1,x2,y2,x3,y3,x4,y4;
    double m1,m2,xnew,ynew,b1,b2;

public Intersection(int a,int b,int c,int d,int e,int f,int g,int h){

x1=a;

y1=b;

x2=c;

y2=d;
    x3=e;
    y3=f;
    x4=g;
    y4=h;

}

public void sol(){
    if(x1==x2||x3==x4){         //check whether the slope of anyline in zero or m1 and m2 are the slop of line
            if(x1==x2)
                m1=0;
        else{
           m1=(y2-y1)/(x2-x1);
        }
        if(x3==x4)
            m2=0;
        else
            m2=(y4-y3)/(x4-x3);
      
        
        }

  
    if(m1==m2){
         System.out.println("No intersection");       lines are parallel
    }
    else{
      
      
        b1=y1-m1*x1;
    b2=y3-m2*x3;
        xnew=(b2-b1) / (m1-m2);         //xnew and ynew are the intersection points
        ynew= m1*((b2-b1) / (m1-m2))+b1;
        System.out.println(xnew);
        System.out.println(ynew);
    }
  
}
}  
      
  

class Solve{

public static void main(String args[]){
    int a1,b1,c1,d1,e1,f1,g1,h1;
    Scanner sc=new Scanner(System.in);
    System.out.prinltn("enter the values of four endpoints");
    a1=sc.nextInt();
     b1=sc.nextInt(); c1=sc.nextInt();
     d1=sc.nextInt();
     e1=sc.nextInt();
     f1=sc.nextInt();
     g1=sc.nextInt();
     h1=sc.nextInt();

Intersection inter=new Intersection(a1,b1,c1,d1,e1,g1,f1,h1);
    inter.sol();
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote