Pages

Sunday 23 December 2012

UVA - 191 - Intersection

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());
        for(int i=0;i<cases;i++){
            StringTokenizer st = new StringTokenizer(br.readLine());
            double x1=Double.parseDouble(st.nextToken());
            double y1=Double.parseDouble(st.nextToken());
            double x2=Double.parseDouble(st.nextToken());
            double y2=Double.parseDouble(st.nextToken());
            Line line=new Line(x1, y1, x2, y2);
            x1=Double.parseDouble(st.nextToken());
            y1=Double.parseDouble(st.nextToken());
            x2=Double.parseDouble(st.nextToken());
            y2=Double.parseDouble(st.nextToken());
            Rectangle rect=new Rectangle(x1, y1, x2, y2);
            if(intersecRecLine(rect, line)){
                sb.append("T\n");
            }else{
                sb.append("F\n");
            }
        }
        System.out.print(sb);
    }
   
    static boolean intersecRecLine(Rectangle x,Line y){
        if(y.getSlope()==Integer.MAX_VALUE){
           if(y.getX1()<=x.getX2() && y.getX1()>=x.getX1()){
               for(double i=Math.min(y.getY1(),y.getY2());i<=Math.max(y.getY1(),y.getY2());i++){
                   if(i<=x.getY2() && i>=x.getY1()){
                       return true;
                   }
                }
               return false;
            }else{
               return false;
           }
        }else{
            for(double i=Math.min(y.getX1(),y.getX2()),j=0;i<=Math.max(y.getX1(),y.getX2());i++){
                if(i<=x.getX2() && i>=x.getX1()){
                    j=y.func(i);
                    if(j<=x.getY2() && j>=x.getY1()){
                        return true;
                    }
                }
            }
            return false;
        }
    }
  
}class Rectangle{
    double x1;
    double y1;
    double x2;
    double y2;

    public Rectangle(double x1, double y1, double x2, double y2) {
        this.x1 = Math.min(x1, x2);
        this.y1 = Math.min(y1, y2);
        this.x2 = Math.max(x1, x2);
        this.y2 = Math.max(y1, y2);
    }
   
    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 getWidth(){
        return x2-x1;
    }
   
    public double getHeight(){
        return y2-y1;
    }
   
    public double getArea(){
        return getWidth()*getHeight();
    }
   
    public double getPeri(){
        return 2*(getWidth()+getHeight());
    }
}
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