#pragma once
#ifndef LINE_H
#define LINE_H
#include "Point2.h"
class Line
{
public:
Line() {};
~Line() {};
Line(Point2 A, Point2 B)
:First(A), Second(B)
{};
Point2 First, Second;
};
#endif
#pragma once
#ifndef POINT2_H
#define POINT2_H
class Point2 {
public:
Point2() {};
~Point2() {};
Point2(double x1, double y1)
:x(x1), y(y1)
{};
void set(double x1, double y1)
{
x = x1;
y = y1;
}
double x, y;
Point2 operator - (const Point2 v) const
{
return Point2(x - v.x, y - v.y);
}
Point2 operator + (const Point2 v) const
{
return Point2(x + v.x, y + v.y);
}
Point2 operator * (const double k) const
{
return Point2(x * k, y * k);
}
double dot(const Point2 v) const
{
return x*v.x + y*v.y;
}
Point2 Vertical() const
{
return Point2(y, -x);
}
};
#endif // !POINT2_H
声明
/*
* 实现二维平面的布尔运算
* Union 并集 intersection 交集 Subtract 减
*/
#pragma once
#ifndef BOOLEAN_H
#define BOOLEAN_H
#include
#include
#include
#include
#include
#include "Point2.h" #include "Line.h" using namespace std; class Boolean { public: Boolean(); ~Boolean(); void SetPLane(list
A, list
B); //输入list数据后 先计算出所有交点 并保存在AList BList void BoolIntersection(list
&PointInter, list
&Num); //输出交集的坐标 PointInter表示交集坐标 Num的值表示交集面的坐标个数 Num的size表示交集面的个数 void Display(); private: bool IsInsect(const Point2 A, const Point2 B, const Point2 C,const Point2 D, Point2 &Insect, double &t); // 求直线AB与线段CD交点 若有交点返回true struct InPoint { Point2 Point; int IsIn; //1 为交点 0 为端点 }; list
AList, BList; }; #endif // !BOOLEAN_H
实现
#include"Boolean.h"
Boolean::Boolean()
{
}
Boolean::~Boolean()
{
}
void Boolean::SetPLane(list
A, list
B)
{
list
::iterator ita, itb;
vector
t; //保存t
Point2 tempP2;
double tempt;
for (ita = A.begin(); ita != A.end(); ita++