Pages

Friday 25 January 2013

UVA - 477 - Points in Figures: Rectangles and Circles

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> listrec=new LinkedList<Rectangle>();
        LinkedList<Circle> listcirc=new LinkedList<Circle>();
        LinkedList<Integer> typ=new LinkedList<Integer>();
        while(true){
                m=br.readLine();
                if("*".equals(m))
                    break;
                StringTokenizer st=new StringTokenizer(m);
                String type=st.nextToken();
                if(type.charAt(0)=='r'){
                    listrec.add(new Rectangle(Double.parseDouble(st.nextToken()),
                                Double.parseDouble(st.nextToken()),
                                Double.parseDouble(st.nextToken()),
                                Double.parseDouble(st.nextToken())));
                    typ.add(0);
                }else{
                    listcirc.add(new Circle(Double.parseDouble(st.nextToken()),
                            Double.parseDouble(st.nextToken()),
                            Double.parseDouble(st.nextToken())));
                    typ.add(1);
                }
        }
        Rectangle[]rects=new Rectangle[listrec.size()];
        for(int i=0;i<rects.length;i++){
            rects[i]=listrec.remove();
        }
        Circle[]circs=new Circle[listcirc.size()];
        for(int i=0;i<circs.length;i++){
            circs[i]=listcirc.remove();
        }
        int[]types=new int[typ.size()];
        for(int i=0;i<types.length;i++){
            types[i]=typ.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,j=0,z=0;i<types.length;i++){
                if(types[i]==0){
                    if(pointInRect(p, rects[j])){
                        String str=String.format("Point %d is contained in figure %d\n", cases,(i+1));
                        sb.append(str);
                        flag=true;
                    }
                    j++;
                }else{
                   if(pointInCircle(p, circs[z])){
                        String str=String.format("Point %d is contained in figure %d\n", cases,(i+1));
                        sb.append(str);
                        flag=true;
                    }
                    z++;
                }
            }
            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();
    }
   
    static boolean pointInCircle(Point p,Circle x){
       double tempX=(p.getX()-x.getCenterX());
       tempX*=tempX;
       double tempY=(p.getY()-x.getCenterY());
       tempY*=tempY;
       return (x.getRadius()>Math.abs(Math.sqrt(tempX+tempY)));
    }
   
}
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());
    }
}class Circle{
    private double centerX;
    private double centerY;
    private double radius;

    public Circle(double centerX, double centerY, double radius) {
        this.centerX = centerX;
        this.centerY = centerY;
        this.radius = radius;
    }

    public double getCenterX() {
        return centerX;
    }

    public void setCenterX(double centerX) {
        this.centerX = centerX;
    }

    public double getCenterY() {
        return centerY;
    }

    public void setCenterY(double centerY) {
        this.centerY = centerY;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

}

No comments:

Post a Comment