UVA – 476 – Points in Figures: Rectangles

import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		ArrayList<Rectangle2D> rects = new ArrayList<Rectangle2D>();
		Point2D end = new Point2D.Double(9999.9, 9999.9), p;
		double epson = 1e-4;
		while(in.next().equals("r")){
			double x1 = in.nextDouble(), y1 = in.nextDouble();
			double x2 = in.nextDouble(), y2 = in.nextDouble();
			
			double X1 = Math.min(x1, x2);
			double Y1 = Math.min(y1, y2);
			double W = Math.max(x1, x2) - X1;
			double H = Math.max(y1, y2) - Y1;
			
			Rectangle2D r = new Rectangle2D.Double(X1,Y1, W, H);
			rects.add(r);
		}
		
		int pcount=0;
		while(Math.abs((p = new Point2D.Double(in.nextDouble(), in.nextDouble())).distance(end)) > epson){
			boolean found = false;
			pcount++;
			for(int i=0 ; i<rects.size(); i++){
				Rectangle2D r = rects.get(i);
				if(r.contains(p)){
					found = true;
					System.out.printf("Point %d is contained in figure %d\n", pcount, i+1 );
				}
			}
			if(!found)
				System.out.printf("Point %d is not contained in any figure\n", pcount);
		}
	}
}




Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: