概述
展开全部
100%符合你的要求,其中inputstr1/2随便你怎么弄,可以通62616964757a686964616fe78988e69d8331333335306230过args传入,或者System.in传入,甚至通过文件传入。
import java.math.BigDecimal;
import java.math.RoundingMode;
public class Main {
public static void main(String[] args) {
String inputStr1 = "1 1 3 3";
String inputStr2 = "2 2 4 4";
String[] r1String = inputStr1.split(" ");
Point p1 = new Point(new BigDecimal(r1String[0]),new BigDecimal(r1String[1]));
Point p2 = new Point(new BigDecimal(r1String[2]),new BigDecimal(r1String[3]));
String[] r2String = inputStr2.split(" ");
Point p3 = new Point(new BigDecimal(r2String[0]),new BigDecimal(r2String[1]));
Point p4 = new Point(new BigDecimal(r2String[2]),new BigDecimal(r2String[3]));
Rectangle r1 = new Rectangle(p1, p2);
Rectangle r2 = new Rectangle(p3, p4);
System.out.println("Rectangle 1: " + r1);
System.out.println("Rectangle 2: " + r2);
System.out.println("The crossing area is " + getCrossArea(r1, r2).setScale(2, RoundingMode.HALF_UP));
}
public static BigDecimal getCrossArea(Rectangle r1, Rectangle r2) {
if (r1.contains(r2)) {
return r2.getArea();
}
if (r2.contains(r1)) {
return r1.getArea();
}
Point crossLeftBottom, crossRightTop;
if (r1.contains(r2.leftBottom)) {
crossLeftBottom = r2.leftBottom;
if (r1.contains(r2.leftTop)) {
crossRightTop = new Point(r1.rightTop.x, r2.rightTop.y);
}
else if (r1.contains(r2.rightBottom)) {
crossRightTop = new Point(r2.rightTop.x, r1.rightTop.y);
}
else {
crossRightTop = r1.rightTop;
}
Rectangle crossingRectangle = new Rectangle(crossLeftBottom, crossRightTop);
System.out.println("Crossing rectangle is " + crossingRectangle);
return crossingRectangle.getArea();
}
else if (r1.contains(r2.rightTop)) {
crossRightTop = r2.rightTop;
if (r1.contains(r2.leftTop)) {
crossLeftBottom = new Point(r2.leftBottom.x, r1.leftBottom.y);
}
else if (r1.contains(r2.rightBottom)) {
crossLeftBottom = new Point(r1.leftBottom.x, r2.leftBottom.y);
}
else {
crossLeftBottom = r1.leftBottom;
}
Rectangle crossingRectangle = new Rectangle(crossLeftBottom, crossRightTop);
System.out.println("Crossing rectangle is " + crossingRectangle);
return crossingRectangle.getArea();
}
else {
// no crossing
System.out.println("No crossing area");
return BigDecimal.ZERO;
}
}
public static class Point {
Point(BigDecimal x, BigDecimal y) {
this.x = x;
this.y = y;
}
BigDecimal x;
BigDecimal y;
@Override
public String toString() {
return "(" + x + "," + y + ")";
}
}
public static class Rectangle {
Rectangle (Point p1, Point p2) {
if (p1.x.compareTo(p2.x) == 0 || p1.y.compareTo(p2.y) == 0) {
throw new RuntimeException("Invalid rectangle." +
" p1.x=" + p1.x + ",p1.y=" + p1.y +
",p2.x=" + p2.x + ",p2.y=" + p2.y);
}
if (p1.x.compareTo(p2.x)
leftBottom = p1;
rightTop = p2;
leftTop = new Point(p1.x, p2.y);
rightBottom = new Point(p2.x, p1.y);
}
if (p1.x.compareTo(p2.x) 0) {
leftTop = p1;
rightBottom = p2;
leftBottom = new Point(p1.x, p2.y);
rightTop = new Point(p2.x, p1.y);
}
if (p1.x.compareTo(p2.x) > 0 && p1.y.compareTo(p2.y)
rightBottom = p1;
leftTop = p2;
rightTop = new Point(p1.x, p2.y);
leftBottom = new Point(p2.x, p1.y);
}
if (p1.x.compareTo(p2.x) > 0 && p1.y.compareTo(p2.y) > 0) {
rightTop = p1;
leftBottom = p2;
rightBottom = new Point(p1.x, p2.y);
leftTop = new Point(p2.x, p1.y);
}
}
Point leftBottom;
Point rightBottom;
Point rightTop;
Point leftTop;
/**
* Check if the point is within this rectangle
* @param p the point
* @return true
if it's in the rectangle, false
otherwise
*/
public boolean contains(Point p) {
return p.x.compareTo(leftBottom.x) >= 0 &&
p.x.compareTo(rightTop.x) <= 0 &&
p.y.compareTo(rightTop.y) <= 0 &&
p.y.compareTo(leftBottom.y) >= 0;
}
/**
* Check if the target rectangle is within this rectangle, which means all the points is within this rectangle
* @param r the target rectangle
* @return true
if it's in the rectangle, false
otherwise
*/
public boolean contains(Rectangle r) {
// it's enough to check leftBottom & rightTop only
return contains(r.leftBottom) && contains(r.rightTop);
}
/**
* Calculate the area it covers
* @return the area
*/
public BigDecimal getArea() {
BigDecimal yGap = rightTop.y.subtract(leftBottom.y);
BigDecimal xGap = rightTop.x.subtract(leftBottom.x);
return yGap.multiply(xGap);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("LeftBottom=");
sb.append(leftBottom);
sb.append(",RightBottom=");
sb.append(rightBottom);
sb.append(",RightTop=");
sb.append(rightTop);
sb.append(",LeftTop=");
sb.append(leftTop);
return sb.toString();
}
}
}
最后
以上就是畅快帽子为你收集整理的Java矩形相交_JAVA 相交矩形面积的全部内容,希望文章能够帮你解决Java矩形相交_JAVA 相交矩形面积所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复