Pages

Wednesday 23 January 2013

UVA - 476 - Points in Figures: Rectangles

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
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();
        String m = "";
        LinkedList<Rectangle> list=new LinkedList<Rectangle>();
        while(true){
                m=br.readLine();
                if("*".equals(m))
                    break;
                StringTokenizer st=new StringTokenizer(m);
                st.nextToken();
                list.add(new Rectangle(Double.parseDouble(st.nextToken()),
                            Double.parseDouble(st.nextToken()),
                            Double.parseDouble(st.nextToken()),
                            Double.parseDouble(st.nextToken())));
        }
        Rectangle[]rects=new Rectangle[list.size()];
        for(int i=0;i<rects.length;i++){
            rects[i]=list.remove();
        }
        int cases=1;
        while(true){
            Double x,y;
            StringTokenizer st=new StringTokenizer(br.readLine());
            x=Double.parseDouble(st.nextToken());
            y=Double.parseDouble(st.nextToken());
            if(x==9999.9 &&y==9999.9){
                break;
            }
            Point p=new Point(x, y);
            boolean flag=false;
            for(int i=0;i<rects.length;i++){
                if(pointInRect(p, rects[i])){
                    String str=String.format("Point %d is contained in figure %d\n", cases,(i+1));
                    sb.append(str);
                    flag=true;
                }
            }
            if(!flag){
                String str=String.format("Point %d is not contained in any figure\n", cases);
                sb.append(str);
            }
            cases++;
        }
        System.out.print(sb);
    }
   
    static boolean pointInRect(Point p,Rectangle x){
        return x.getX1()<p.getX()&&x.getX2()>p.getX()
                &&x.getY1()<p.getY()&&x.getY2()>p.getY();
    }
   
}class Point {
    private double x;
    private double y;

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }
   
}
class Rectangle{
    private double x1;
    private double y1;
    private double x2;
    private 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());
    }
}

No comments:

Post a Comment