#include <iostream>
#include <iomanip>
#include <limits>
#include <cmath>
using namespace std;
class POINT{
public:
int x;
int y;
//int z;
//POINT(int px,int py,int pz){
// x = px;
// y = py;
// z = pz;
//}
POINT(int px,int py){
x = px;
y = py;
}
POINT(POINT &po){
x = po.x;
y = po.y;
}
POINT& operator=(POINT &rp){
x = rp.x;
y = rp.y;
return *this;
/*
POINT pt;
pt.x = rp.x;
pt.y = rp.y;
return pt;*/
}
};
POINT& operator-(POINT &lp, POINT &rp){
POINT* pt = new POINT(0,0);
pt->x = lp.x - rp.x;
pt->y = lp.y - rp.y;
return *pt;
}
int area(POINT &pt1, POINT &pt2, POINT &pt){
POINT vec1 = pt2 - pt1;
POINT vec2 = pt - pt1;
return abs(vec1.x * vec2.y - vec1.y * vec2.x);
}
class SQUARE{
public:
POINT pt1;
POINT pt2;
POINT pt3;
POINT pt4;
SQUARE(POINT px1,POINT px2, POINT px3, POINT px4):
pt1(px1),pt2(px2),pt3(px3),pt4(px4)
{}
int areas(){
return area(pt1, pt2, pt3);
}
};
int max2(int a, int b){
if(a>b){
return a;
}else{
return b;
}
}
int min2(int a, int b){
if(a < b){
return a;
}else{
return b;
}
}
int max4(int a,int b, int c, int d){
return max2(max2(a, b), max2(c, d));
}
int min4(int a, int b, int c, int d){
return min2(min2(a, b), min2(c, d));
}
int MaxXC(const SQUARE &sq)
{
return max4(sq.pt1.x, sq.pt2.x, sq.pt3.x, sq.pt4.x);
}
int MaxYC(const SQUARE &sq)
{
return max4(sq.pt1.y, sq.pt2.y, sq.pt3.y, sq.pt4.y);
}
int MinXC(const SQUARE &sq)
{
return min4(sq.pt1.x, sq.pt2.x, sq.pt3.x,sq.pt4.x);
}
int MinYC(const SQUARE &sq)
{
return min4(sq.pt1.y, sq.pt2.y, sq.pt3.y, sq.pt4.y);
}
int ptinsq(POINT &pt, SQUARE &sq){
// 点在矩形外,返回 0
// 点在矩形内,返回 1
// 点在矩形边上,返回 2
if(pt.x < MinXC(sq) || pt.x > MaxXC(sq) || pt.y < MinYC(sq) || pt.y > MaxYC(sq)){
return 0;
}else{
if(area(sq.pt1, sq.pt2, pt) + area(sq.pt2, sq.pt3, pt) + area(sq.pt3, sq.pt4, pt) + area(sq.pt4, sq.pt1, pt) == 2 * sq.areas()){
if(area(sq.pt1, sq.pt2, pt) == 0 || area(sq.pt2, sq.pt3, pt) == 0 || area(sq.pt3, sq.pt4, pt) == 0 || area(sq.pt4, sq.pt1, pt) == 0){
return 2;
}else{
return 1;
}
}else{
return 0;
}
}
}
int cross(SQUARE &sq1, SQUARE &sq2){
// 两个矩形不相交,没有公共区域没有公共点 返回 0
// 两个矩形相交,有公共区域, 返回 1
// 两个矩形不相交,但有公共点或者公共边,返回 2
if((ptinsq(sq1.pt1, sq2) == 0 && ptinsq(sq1.pt2, sq2) == 0 && ptinsq(sq1.pt3, sq2) == 0 && ptinsq(sq1.pt4, sq2) == 0) ||
(ptinsq(sq2.pt1, sq1) == 0 && ptinsq(sq2.pt2, sq1) == 0 && ptinsq(sq2.pt3, sq1) == 0 && ptinsq(sq2.pt4, sq1) == 0)){
return 0;
}else if((ptinsq(sq1.pt1, sq2) == 2) || (ptinsq(sq1.pt2, sq2) == 2) || (ptinsq(sq1.pt3, sq2) == 2) || (ptinsq(sq1.pt4, sq2) == 2) ||
(ptinsq(sq2.pt1, sq1) == 2) || ( ptinsq(sq2.pt2, sq1) == 2) || ( ptinsq(sq2.pt3, sq1) == 2) || ( ptinsq(sq2.pt4, sq1) == 2)){
return 2;
}else{
return 1;
}
}
int main(int argc, char** argv)
{
POINT sq1pt1(0,0);
POINT sq1pt2(0,2);
POINT sq1pt3(2,2);
POINT sq1pt4(2,0);
POINT sq2pt1(2,1);
POINT sq2pt2(2,3);
POINT sq2pt3(5,3);
POINT sq2pt4(5,1);
SQUARE sq1(sq1pt1,sq1pt2,sq1pt3,sq1pt4);
SQUARE sq2(sq2pt1,sq2pt2,sq2pt3,sq2pt4);
int res = cross(sq1,sq2);
if(res == 0){
cout << "no common area\n";
}else if(res == 1){
cout << "common area exist\n";
}else{
cout << "have common point or common line\n";
}
return 0;
}
代码没有提供对输入点序列是否为矩形验证的保证,切输入的四个点须按顺时针或逆时针给出,否则会导致未知结果。