/// <summary> /// 计算两个rect的距离 /// 相交 返回-1 /// 相切 返回0 /// 分离 返回最短距离 /// </summary> /// <param name="other"></param> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> public double distance_to_rect(MyRect other, out double x, out double y) { x = MyRect.distance_between_sections(this.leftright.x, this.rightup.x, other.leftright.x, other.rightup.x); y = MyRect.distance_between_sections(this.leftright.y, this.rightup.y, other.leftright.y, other.rightup.y); if (x < 0 && y < 0)//x和y方向是相交 两个rect才是相交 { return(-1); } else if (x + y == -1)//有一个方向相切 一个相交,两个rect是相切 { return(0); } else if (x == 0 && y == 0)//x和y方向是相切 两个rect是相切 { return(0); } else//两个rect分离 返回最短距离 { if (x < 0) { return(y); } if (y < 0) { return(x); } return(Math.Sqrt(x * x + y * y)); } }
public MyRect intersection(MyRect other) { double a, b; bool flag; //分成两个方向计算 flag = MyRect.intersection_between_sections( this.leftright.x, this.rightup.x, other.leftright.x, other.rightup.x, out a, out b); if (!flag) { return(null); } double a1, b1; flag = MyRect.intersection_between_sections( this.leftright.y, this.rightup.y, other.leftright.y, other.rightup.y, out a1, out b1); if (!flag) { return(null); } return(new MyRect(new Vector3D(a, a1), new Vector3D(b, b1))); }