#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int maxX = 1080, maxY = 1920, maxP = 40;
const int LineW = 4;
const int xMax = 800, xMin = 300, yMax = 1300, yMin = 600;
int num[maxX][maxY];
const double eps = 1e-6, Pi = 4 * atan(1);
struct Point {
double x, y;
Point(double x = 0, double y = 0) : x(x), y(y) {
}
};
double operator * (Point a, Point b) {
return a.x * b.x + a.y * b.y;
}
Point operator - (Point a, Point b) {
return Point(a.x - b.x, a.y - b.y);
}
double operator ^ (Point a, Point b) {
return a.x * b.y - a.y * b.x;
}
double mod(Point a) {
return sqrt(a * a * 1.0);
}
double theta(Point a, Point b) {
double csin = (a ^ b) / mod(a) / mod(b);
double ccos = (a * b) / mod(a) / mod(b);
return atan2(csin, ccos);
}
void Cohen_SutherLand(Point& a, Point& b) {
int aOrder = 0, bOrder = 0;
if (a.x < xMin)
aOrder |= (1 << 0);
if (a.x > xMax)
aOrder |= (1 << 1);
if (a.y < yMin)
aOrder |= (1 << 2);
if (a.y > yMax)
aOrder |= (1 << 3);
if (b.x < xMin)
bOrder |= (1 << 0);
if (b.x > xMax)
bOrder |= (1 << 1);
if (b.y < yMin)
bOrder |= (1 << 2);
if (b.y > yMax)
bOrder |= (1 << 3);
if ((aOrder & bOrder) != 0) {
a = Point(-1, -1);
b = Point(-1, -1);
return;
}
if ((aOrder | bOrder) ==
直线段裁剪 Cohen_SutherLand 以及 Liang_Barsky 算法
最新推荐文章于 2022-02-22 16:10:57 发布
本文介绍了如何使用Cohen-Sutherland和Liang-Barsky算法来裁剪直线段,使其在指定的边界内。通过C++代码展示了这两种算法的工作原理,包括直线段与边界的交点计算、裁剪处理等步骤。
摘要由CSDN通过智能技术生成