Friday, May 11, 2012

problem solve using java

PROBLEM

Intersecting Lines

When we do A/L and O/L we have learnt the Cartesian coordinates and we all know that a pair of distinct points on a plane defines a line and that a pair of lines on a plane will intersect in one of three ways (http://en.wikipedia.org/wiki/Cartesian_coordinate_system):

1) No intersection because they are parallel,2) Intersect in a line because they are on top of one another (i.e. they are the same line),3) Intersect in a point.  In this problem you will use your algebraic knowledge to create a program that determines how and where two lines intersect.

Problem Statement: Your program will read in four points that define two lines in the x-y plane and determine how and where the lines intersect.  All numbers required by this problem will be reasonable, say between -1000 and 1000.


Input The line will contain eight integers.  These integers represent the coordinates of four points on the plane in the order x1 y1 x2 y2 x3 y3 x4 y4.  Thus each of these input lines represents two lines on the plane: the line through (x1, y1) and (x2, y2) and the line through (x3, y3) and (x4, y4).  The point (x1, y1) is always distinct from (x2, y2), likewise with (x3, y3) and (x4, y4).


Output There will then be one line of output, describing how the lines intersect: none, line, or point.  If the intersection is a point then your program should output the x and y coordinates of the point, correct to two decimal places.


Sample Inputs                                          
  Output 

0 0 4 4 0 4 4 0                                           
POINT 2.00 2.00 

5 0 7 6 1 0 2 3                                          
 NONE 

5 0 7 6 3 -6 4 -3                                        
 LINE 



ANSWER



import java.util.Scanner;


public class AcesCoders1 {

   
    public static void main(String[] args) {


        Scanner sc = new Scanner(System.in);
        String inp = sc.nextLine();

        String[] spl = inp.split(" ");


        float x1 = Integer.parseInt(spl[0]);
        float y1 = Integer.parseInt(spl[1]);
        float x2 = Integer.parseInt(spl[2]);
        float y2 = Integer.parseInt(spl[3]);
        float x3 = Integer.parseInt(spl[4]);
        float y3 = Integer.parseInt(spl[5]);
        float x4 = Integer.parseInt(spl[6]);
        float y4 = Integer.parseInt(spl[7]);

        String out;


        float m1 = -10000f;
        if (x2 != x1) {
           
            m1 = (y2 - y1) / (x2 - x1);
        }
        float m2 = -10000f;
        if (x3 != x4) {
            m2 = (y4 - y3) / (x4 - x3);
        }

        float c1 = -10000;
        if (m1 != -10000) {
            c1 = y1 - (m1 * x1);
        }
        float c2 = -10000;
        if (m2 != -10000) {
            c2 = y3 - (m2 * x3);
        }

        
     
        
        if (m1 == m2 && c1 == c2) {
            out = "LINE";
        } else if (m1 == m2 && c1 != c2) {
            out = "NONE";
        } else {
            float x5=0;
            float y5=0;
            if(m1==-10000){
                y5=m2*x2+c2;
                x5=x2;
            }
            else if(m2==-10000){
                y5=m1*x3+c1;
                x5=x3;
            }
            else{
             x5= (c2 - c1) / (m1 - m2);
            y5 = m1 * x5 + c1;
            }
            x5=Math.round(x5*100.0f)/100.0f;
            y5=Math.round(y5*100.0f)/100.0f;
            out = "POINT " + x5 + " " + y5;
        }
        System.out.print(out);
    }
}


0 comments:

Post a Comment