「Java矩形相交」java矩形类的定义和调用
本篇文章给大家谈谈Java矩形相交,以及java矩形类的定义和调用对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、用java编写一个判断两个矩形是否相交的函数怎么写?
- 2、用java创建一个矩形类Rectangle,其属性为各点的坐标。
- 3、JAVA 相交矩形面积
- 4、java练习题,判断第二个矩形是否完全在第一个矩形以内,还是两者部分相交、还是相离?
用java编写一个判断两个矩形是否相交的函数怎么写?
你得到2个矩形的4个X坐标4个Y坐标 排序
得到 中间两个坐标的中值 得到个点坐标
2个矩形内都含有这点的话 两矩形相交
用java创建一个矩形类Rectangle,其属性为各点的坐标。
class Point{
protected:
double x;
double y;
public:
Point(double a,double b){x=a;y=b;}
Point(){x=0.0;y=0.0;}
double GetX(){return x;}
double GetY(){return y;}
};
class Rect{
protected:
Point p1;
Point p2;//p1是左下角,p2是右上角
public:
Rect(double i,double j,double,k,double l):p1(i,j),p2(k,l){}
double Area()
{return ((p2.GetX()-p1.GetX())*(p1.GetY()-p2.GetY()));}
};
//用于测试的主函数
void main()
{
Rect rect1(2,3,4,5);
coutrect1.Area()endl;
}
JAVA 相交矩形面积
100%符合你的要求,其中inputstr1/2随便你怎么弄,可以通过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) 0 p1.y.compareTo(p2.y) 0) {
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 p1.y.compareTo(p2.y) 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) 0) {
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 codetrue/code if it's in the rectangle, codefalse/code 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 codetrue/code if it's in the rectangle, codefalse/code 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一点用也没有。练习不到算法和语法。
Java2D有类和方法解决这类问题。否则,绘图时碰撞都由程序员自己写累死了。
关于Java矩形相交和java矩形类的定义和调用的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。