直线段裁剪 Cohen_SutherLand 以及 Liang_Barsky 算法

本文介绍了如何使用Cohen-Sutherland和Liang-Barsky算法来裁剪直线段,使其在指定的边界内。通过C++代码展示了这两种算法的工作原理,包括直线段与边界的交点计算、裁剪处理等步骤。
摘要由CSDN通过智能技术生成
#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) == 
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值