日期:2014-05-20 浏览次数:20775 次
import java.awt.Point; public class Main { public static void main(String args[]){ RectangeDemo r1 = new RectangeDemo(new Point(1,2),5,5); RectangeDemo r2 = new RectangeDemo(new Point(10,8),5,5); checkRelation(r1,r2); } static class RectangeDemo{ Point center; double width; double height; public RectangeDemo(Point point, int width, int height) { this.center = point; this.width = width; this.height = height; } } public static void checkRelation(RectangeDemo r1,RectangeDemo r2){ if(Math.abs(r1.center.x -r2.center.x) > r1.width + r2.width && Math.abs(r1.center.y -r2.center.y) > r1.height + r2.height ){ System.out.println("相离"); } else if(Math.abs(r1.center.x - r2.center.x) < Math.abs(r1.width - r2.width) && Math.abs(r1.center.y -r2.center.y) > Math.abs(r1.height - r2.height)){ System.out.println("包含"); } else{ System.out.println("相交"); } } }
------解决方案--------------------
纠正一下。sorry
|x1-x2|>(L1+L2)/2&&|y1 - y2|>(H1+H2)/2;相离。
|x1-x2| <|L1-L2|/2 && |y1 - y2|< |H1-H2|/2 包含
其他情况相交。
import java.awt.Point; public class Main { public static void main(String args[]){ RectangeDemo r1 = new RectangeDemo(new Point(1,2),5,5); RectangeDemo r2 = new RectangeDemo(new Point(10,8),5,5); checkRelation(r1,r2); } static class RectangeDemo{ Point center; double width; double height; public RectangeDemo(Point point, int width, int height) { this.center = point; this.width = width; this.height = height; } } public static void checkRelation(RectangeDemo r1,RectangeDemo r2){ if(Math.abs(r1.center.x -r2.center.x) > r1.width/2 + r2.width/2 && Math.abs(r1.center.y -r2.center.y) > r1.height/2 + r2.height/2 ){ System.out.println("相离"); } else if(Math.abs(r1.center.x - r2.center.x) < Math.abs(r1.width/2 - r2.width/2) && Math.abs(r1.center.y -r2.center.y) > Math.abs(r1.height/2 - r2.height/2)){ System.out.println("包含"); } else{ System.out.println("相交"); }