#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
// #include <cstdlib>
#include "stdlib.h"
using namespace std;
const double PI = 3.141592654;
const int maxn = 300;
const double eps = 1e-6;
struct Point
{
double x, y;
};
Point p1[maxn], p2[maxn];
int n1, n2;
// 计算两个线段的角度
double normals(double x1, double y1, double x2, double y2)
{
double n = x1 * x2 + y1 * y2;
double m = sqrt(x1 * x1 + y1 * y1) * sqrt(x2 * x2 + y2 * y2);
if (m == 0) {
cout << "参数错误:";
return -1;
}
return acos(n / m) * (180 / PI);
}
// 计算一个点(x2, y2)在 矢量线段[(x0, y0), (x1, y1)]的哪侧,即在线段的顺时针还是逆时针位置,还是线段
double pointLocation(double x0, double y0, double x1, double y1, double x2, double y2)
{
return (x0 - x2) * (y1 - y2) - (y0 - y2) * (x1 - x2);
}
// 计算一个点在一个矢量图的那侧,正数在顺时针位置,负数为逆时针位置,零为在线段上或者延长线上
double pointLocation2(Point p0, Point p1, Point p2)
{
return (p0.x - p2.x) * (p1.y - p2.y) - (p0.y - p2.y) * (p1.x - p2.x);
}
Point intersection(Point a,Point b,Point c,Point d)
{
Point p = a;
double t =((a.x-c.x)*(c.y-d.y)-(a.y-c.y)*(c.x-d.x))/((a.x-b.x)*(c.y-d.y)-(a.y-b.y)*(c.x-d.x));
p.x +=(b.x-a.x)*t;
p.y +=(b.y-a.y)*t;
cout << "intersection p.x=" << p.x << ", p.y=" << p.y << endl;
return p;
}
/*
Point getCrossPoint(Point a,Point b, Point a1,Point b1)
{
Point base = b1 - b;
double d1= abs(cross(base,a-b));
double d2= abs(cross(base,a1-b));
double t=d1/(d1+d2);
Point temp=(a1-a)*t;
return a+temp;
}
*/
// Calculate the cross product
float cross(Point a, Point b, Point c)
{
return (a.x - c.x) * (b.y - c.y) - (b.x - c.x) * (a.y - c.y);
}
//Calculate the intersection of two line segments
Point segmentIntersection(Point a, Point b, Point c, Point d)
{
Point temp={-1, -1};
if ( min(a.x, b.x) <= max(c.x, d.x) && max(a.x, b.x) >= min(c.x, d.x)
&& min(a.y, b.y) <= max(c.y, d.y) && max(a.y, b.y) >= min(c.y, d.y)
&& cross(d, a, c) * cross(b, d, c) >= 0 && cross(b, c, a) * cross(d, b, a) >= 0) {
if (cross(d, a, c) * cross(b, d, c) == 0 && cross(b, c, a) * cross(d, b, a) == 0) {
cout << "The two lines segment are parallel." << endl;
return temp;
}
Point p = a;
float t = ((a.x - c.x) * (c.y - d.y) - (a.y - c.y) * (c.x - d.x)) / ((a.x - b.x) * (c.y - d.y)-(a.y - b.y) * (c.x - d.x));
p.x += (b.x - a.x) * t;
p.y += (b.y - a.y) * t;
cout << "intersection p.x= " << p.x << ", p.y= " << p.y << endl;
return p;
}
cout << "not intersection."<< endl;
return temp;
}
/*
int dcmp(double x)
{
if(x > eps) return 1;
return x < -eps ? -1 : 0;
}
//计算多边形面积
double PolygonArea(Point p[], int n)
{
if(n < 3) return 0.0;
double s = p[0].y * (p[n - 1].x - p[1].x);
for(int i = 1; i < n - 1; ++ i) {
s += p[i].y * (p[i - 1].x - p[i + 1].x);
// cout << "p[i-1].x =" << p[i-1].x << ", p[i-1].y=" << p[i-1].y << endl;
// cout << "p[i].x =" << p[i].x << ", p[i].y=" << p[i].y << endl;
// cout << "p[i+1].x =" << p[i+1].x << ", p[i+1].y=" << p[i+1].y << endl;
}
s += p[n - 1].y * (p[n - 2].x - p[0].x);
cout << "s =" << s << endl;
return fabs(s * 0.5);
}
double CPIA(Point a[], Point b[], int na, int nb) //ConvexPolygonIntersectArea
{
Point p[20], tmp[20];
int tn, sflag, eflag;
memcpy(p,b,sizeof(Point)*(nb));
for(int i = 0; i < na && nb > 2; i++)
{
if (i == na - 1) {
sflag = dcmp(cross(a[0], p[0],a[i]));
} else {
sflag = dcmp(cross(a[i + 1], p[0],a[i]));
}
for(int j = tn = 0; j < nb; j++, sflag = eflag)
{
if(sflag>=0) {
tmp[tn++] = p[j];
}
if (i == na - 1) {
if (j == nb -1) {
eflag = dcmp(cross(a[0], p[0], a[i]));
} else {
eflag = dcmp(cross(a[0], p[j + 1], a[i]));
}
} else {
if (j == nb -1) {
eflag = dcmp(cross(a[i + 1], p[0], a[i]));
} else {
eflag = dcmp(cross(a[i + 1], p[j + 1], a[i]));
}
}
if((sflag ^ eflag) == -2){
if (i == na - 1) {
if (j == nb -1) {
tmp[tn++] = intersection(a[i], a[0], p[j], p[0]); //求交点
} else {
tmp[tn++] = intersection(a[i], a[0], p[j], p[j + 1]);
}
} else {
if (j == nb -1) {
tmp[tn++] = intersection(a[i], a[i + 1], p[j], p[0]);
} else {
tmp[tn++] = intersection(a[i], a[i + 1], p[j], p[j + 1]);
}
}
}
}
memcpy(p, tmp, sizeof(Point) * tn);
nb = tn, p[nb] = p[0];
}
if(nb < 3) return 0.0;
return PolygonArea(p, nb);
}
double SPIA(Point a[], Point b[], int na, int nb) //SimplePolygonIntersectArea 调用此函数
{
int i, j;
Point t1[na], t2[nb];
double res = 0, num1, num2;
t1[0] = a[0], t2[0] = b[0];
for(i = 2; i < na; i++)
{
t1[1] = a[i-1], t1[2] = a[i];
num1 = dcmp(cross(t1[1], t1[2],t1[0]));
if(num1 < 0) swap(t1[1], t1[2]);
for(j = 2; j < nb; j++)
{
t2[1] = b[j - 1], t2[2] = b[j];
num2 = dcmp(cross(t2[1], t2[2],t2[0]));
if(num2 < 0) swap(t2[1], t2[2]);
res += CPIA(t1, t2, 3, 3) * num1 * num2;
}
}
cout << "Sum::res=" <<res << endl;
return res;
}
*/
int main() {
//double angle;
//double x0, y0, x1, y1, x2, y2;
//cout << "请输入三个点的坐标值:"<<endl;
//cin >> x0 >> y0 >> x1 >> y1 >> x2 >> y2;
//angle = normals(x1-x0, y1 - y0, x2 - x0, y2 - y0);
//cout << "夹角为 :" << angle << endl;
//double s = pointLocation(x0, y0, x1, y1, x2, y2);
// cout << "s = " << s << endl;
/*
while(cin>>n1)
{
for(int i = 0; i < n1; i++) scanf("%lf%lf", &p1[i].x, &p1[i].y);
double A1 = pointLocation2(p1[0], p1[1], p1[2]);
cout << "A1 =" << A1 << endl;
}
*/
Point temp0= {-1, -1};
Point temp1= {-1, -1};
Point temp2= {-1, -1};
Point temp3= {-1, -1};
Point cenPoint= {-1, -1};
vector<Point> pointsVec;
Point tar_points_center[4];
while(cin>>n1>>n2)
{
for(int i = 0; i < n1; i++) scanf("%lf%lf", &p1[i].x, &p1[i].y);
for(int i = 0; i < n2; i++) scanf("%lf%lf", &p2[i].x, &p2[i].y);
// double A1 = pointLocation2(p1[0], p1[1], p1[2]);
// cout << "A1 =" << A1 << endl;
tar_points_center[0].x = (p2[0].x + p2[1].x) / 2.0;
tar_points_center[0].y = (p2[0].y + p2[1].y) / 2.0;
tar_points_center[1].x = (p2[1].x + p2[2].x) / 2.0;
tar_points_center[1].y = (p2[1].y + p2[2].y) / 2.0;
tar_points_center[2].x = (p2[2].x + p2[3].x) / 2.0;
tar_points_center[2].y = (p2[2].y + p2[3].y) / 2.0;
tar_points_center[3].x = (p2[3].x + p2[0].x) / 2.0;
tar_points_center[3].y = (p2[3].y + p2[0].y) / 2.0;
/*
temp0 = segmentIntersection(p1[0], p1[1], p2[0], p2[1]);
cout << "temp0.x = " << temp0.x << ", temp0.y=" << temp0.y << endl;
temp1 = segmentIntersection(p1[0], p1[1], p2[1], p2[2]);
cout << "temp1.x = " << temp1.x << ", temp1.y=" << temp1.y << endl;
temp2 = segmentIntersection(p1[0], p1[1], p2[2], p2[3]);
cout << "temp2.x = " << temp2.x << ", temp2.y=" << temp2.y << endl;
temp3 = segmentIntersection(p1[0], p1[1], p2[3], p2[0]);
cout << "temp3.x = " << temp3.x << ", temp3.y=" << temp3.y << endl;
cout << endl;
if (temp0.x != -1 && temp0.y != -1) {
pointsVec.push_back(temp0);
} else if (pointLocation2(p1[0], p1[1], tar_points_center[0]) > 0){
pointsVec.push_back(p2[0]);
pointsVec.push_back(p2[1]);
}
for (unsigned int i = 0; i < pointsVec.size(); i++) {
cout << "temp0: x = " << pointsVec[i].x << ", y=" << pointsVec[i].y << endl;
}
cout << endl;
if (temp1.x != -1 && temp1.y != -1) {
pointsVec.push_back(temp1);
} else if(pointLocation2(p1[0], p1[1], tar_points_center[1]) > 0) {
pointsVec.push_back(p2[1]);
pointsVec.push_back(p2[2]);
}
for (unsigned int i = 0; i < pointsVec.size(); i++) {
cout << "temp1: x = " << pointsVec[i].x << ", y=" << pointsVec[i].y << endl;
}
cout << endl;
if (temp2.x != -1 && temp2.y != -1) {
pointsVec.push_back(temp2);
} else if(pointLocation2(p1[0], p1[1], tar_points_center[2]) > 0) {
pointsVec.push_back(p2[2]);
pointsVec.push_back(p2[3]);
}
for (unsigned int i = 0; i < pointsVec.size(); i++) {
cout << "temp2: x = " << pointsVec[i].x << ", y=" << pointsVec[i].y << endl;
}
cout << endl;
if (temp3.x != -1 && temp3.y != -1) {
pointsVec.push_back(temp3);
} else if(pointLocation2(p1[0], p1[1], tar_points_center[3]) > 0) {
pointsVec.push_back(p2[3]);
pointsVec.push_back(p2[0]);
}
for (unsigned int i = 0; i < pointsVec.size(); i++) {
cout << "temp3: x = " << pointsVec[i].x << ", y=" << pointsVec[i].y << endl;
}
cout << endl;
for (unsigned int i = 0; i < pointsVec.size(); i++) {
cout << "x = " << pointsVec[i].x << ", y=" << pointsVec[i].y << endl;
}
if (pointsVec.size() >= 2) {
for (unsigned int i = 0; i < pointsVec.size() - 1; i++) {
for (unsigned int j = i + 1; j < pointsVec.size(); j++)
if(pointsVec[j].x == pointsVec[i].x && pointsVec[j].y == pointsVec[i].y)
pointsVec.erase(pointsVec.begin() + j);
}
}
cout << "after erase ..." << endl;
for (unsigned int i = 0; i < pointsVec.size(); i++) {
cout << "x = " << pointsVec[i].x << ", y=" << pointsVec[i].y << endl;
}
int flag = 0;
cenPoint = intersection(p2[0], p2[2], p2[1], p2[3]);
int loc = pointLocation2(p1[0], p1[1], cenPoint);
if (0 == pointsVec.size()) {
if (loc > 0) {
flag = 1;
} else if (loc < 0) {
flag = -1;
}
} else {
}
cout << "flag = " << flag << endl;
*/
temp0 = intersection(p1[0], p1[1], p2[0], p2[1]);
cout << "temp0.x = " << temp0.x << ", temp0.y=" << temp0.y << endl;
temp1 = intersection(p1[0], p1[1], p2[1], p2[2]);
cout << "temp1.x = " << temp1.x << ", temp1.y=" << temp1.y << endl;
temp2 = intersection(p1[0], p1[1], p2[2], p2[3]);
cout << "temp2.x = " << temp2.x << ", temp2.y=" << temp2.y << endl;
temp3 = intersection(p1[0], p1[1], p2[3], p2[0]);
cout << "temp3.x = " << temp3.x << ", temp3.y=" << temp3.y << endl;
}
return 0;
}
计算一个线段与一个矩形的相交面积
最新推荐文章于 2022-04-12 10:15:35 发布