AOJ : 0059 - Intersection of Rectangles
問題概要
http://rose.u-aizu.ac.jp/onlinejudge/ProblemSet/description.jsp?id=0059&lang=jp
友人が解いてたので、僕も今一度解いておきました。
日本語の問題文なので省略です
アルゴリズム
単純な幾何問題です。
幾何はC++の方が解きやすいですが、この問題の場合は、JavaのRectangle2Dを使った方が楽です。
ただし、Rectangle2Dを使用する際、注意しなければいけないのは、接しているときは、重なっていると判定してくれない点です。
この点は、どちらか一方の長方形のサイズを、少しだけ大きくしておけば解消することができます。
プログラム
import java.util.*; import java.awt.geom.Rectangle2D; public class Main{ private static final double EPS = 1e-10; public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNextDouble()){ double x,y,w,h; x = sc.nextDouble()-EPS; y = sc.nextDouble()-EPS; w = sc.nextDouble()-x+EPS; h = sc.nextDouble()-y+EPS; Rectangle2D r = new Rectangle2D.Double(x,y,w,h); x = sc.nextDouble(); y = sc.nextDouble(); w = sc.nextDouble()-x; h = sc.nextDouble()-y; System.out.println(r.intersects(x,y,w,h) ? "YES" : "NO"); } } }