UVa - 476 Points in Figures: Rectangles

//java solution

import java.util.*;
import java.util.regex.*;
import java.io.*;
import java.awt.*;
import java.awt.geom.*;
import java.math.*;
import java.text.*;

class Main
{
 

    public static void main (String args[])  // entry point from OS
    {
        Main myWork = new Main();  // create a dynamic instance
        myWork.Begin();            // the true entry point
    }
boolean chkborderrect(double x,double y,double x1,double y1,double x2,double y2)
{
return ((x1<=x && x<=x2 && y==y1) || (x1<=x && x<=x2 && y==y2) || (y1<=y && y<=y2 && x==x1) || (y1<=y && y<=y2 && x==x2));
}
    void Begin()
    {
Scanner sc=new Scanner(new BufferedReader(new InputStreamReader(System.in)));
PrintWriter pr=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
/*try
{
  sc=new Scanner(new File("in.txt"));
  pr=new PrintWriter(new File("out.txt"));
}
 catch(Exception e)
 {
  System.exit(0);
 }
*/
String c;
int nr;
double x,y,lx1,ly1,lx2,ly2;
boolean got;
Rectangle2D[] rs=new Rectangle2D[20];
for(nr=0;;nr++)
{
c=sc.next();
if(c.compareTo("*")==0)
{
break;
}
lx1=sc.nextDouble();
ly1=sc.nextDouble();
lx2=sc.nextDouble();
ly2=sc.nextDouble();
rs[nr]=new Rectangle2D.Double();
rs[nr].setFrameFromDiagonal(lx1,ly1,lx2,ly2);
}
for(int lpx=0;;lpx++)
{
got=false;
x=sc.nextDouble();y=sc.nextDouble();
if(x==9999.9 && y==9999.9)
{
break;
}
for(int lx=0;lx<nr;lx++)
{
if(chkborderrect(x,y,rs[lx].getX(),rs[lx].getY(),rs[lx].getX()+rs[lx].getWidth(),rs[lx].getY()+rs[lx].getHeight()))
{
continue;
}
if(rs[lx].contains(x,y))
{
got=true;
pr.printf("Point %d is contained in figure %d%n",(lpx+1),(lx+1));
}
}
if(!got)
{
pr.printf("Point %d is not contained in any figure%n",(lpx+1));
}
}
pr.close();
sc.close();
    }
}

No comments:

Post a Comment