Pages

Thursday 10 January 2013

UVA - 378 - Intersecting Lines


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb=new StringBuffer();
        int cases=Integer.parseInt(br.readLine());
        sb.append("INTERSECTING LINES OUTPUT\n");
        for(int i=0;i<cases;i++){
            StringTokenizer st=new StringTokenizer(br.readLine());
            Line x,y;
            x=new Line(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()),
                    Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
            y=new Line(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()),
                    Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
            if(x.getSlope()==y.getSlope()){
                if(x.getX1()==x.getX2()){
                    if(x.getX1()==y.getX1()){
                       sb.append("LINE\n");
                    }else{
                       sb.append("NONE\n");
                    }
                }
                else if(x.func(x.getX1())==y.func(x.getX1())){
                    sb.append("LINE\n");
                }else{
                    sb.append("NONE\n");
                }
            }else{
                double tempX=(double)(y.getC()-x.getC())/(x.getSlope()-y.getSlope());
                double tempY=x.func(tempX);
                String s=String.format("POINT %.2f %.2f\n", tempX,tempY);
                sb.append(s);
            }
        }
        sb.append("END OF OUTPUT\n");
        System.out.print(sb);
    }
}
class Line{
    double x1;
    double y1;
    double x2;
    double y2;
    double slope;
    double c;

    public Line(double x1, double y1, double x2, double y2) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
        if(x2-x1!=0)
            slope=(double)(this.y2-this.y1)/(this.x2-this.x1);
        else{
            slope=Integer.MAX_VALUE;
        }
        c=this.y2-this.x2*this.slope;
    }

    public double getX1() {
        return x1;
    }

    public void setX1(double x1) {
        this.x1 = x1;
    }

    public double getX2() {
        return x2;
    }

    public void setX2(double x2) {
        this.x2 = x2;
    }

    public double getY1() {
        return y1;
    }

    public void setY1(double y1) {
        this.y1 = y1;
    }

    public double getY2() {
        return y2;
    }

    public void setY2(double y2) {
        this.y2 = y2;
    }

    public double getC() {
        return c;
    }

    public void setC(double c) {
        this.c = c;
    }

    public double getSlope() {
        return slope;
    }

    public void setSlope(double slope) {
        this.slope = slope;
    }
  
    public double func(double x){
         return (x*slope+c);
    }
}

No comments:

Post a Comment