判断多边形是否为凹多边形

#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <vector>
#include <vector>
#include <algorithm>
#include <math.h>

using namespace std;

const double kMathEpsilon = 1e-10;
class Vec2d {
	public:
	explicit Vec2d(const double x, const double y) : x_(x), y_(y) {}
	explicit Vec2d() : Vec2d(0.0, 0.0) {}
	Vec2d operator- (const Vec2d& other) const {
		return Vec2d(other.x() - x_, other.y() - y_);
	}
	const double x() const {return x_;}
	const double y() const {return y_;}

	double crossPoint (const Vec2d& other) const {
		return this->x_*other.y() - this->y_*other.x();
	}

	double InnerPoint(const Vec2d& other) const{
		return this->x_*other.x() + this->y() * other.y();
	}

	private:
	double x_ = 0.0;
	double y_ = 0.0;

};

class LineSegment {
	public:
	LineSegment(const Vec2d start, const Vec2d end) : start_(start), end_(end) {}
	double CrossLine(const Vec2d& start_point, const Vec2d& end_point1, const Vec2d& end_point2) const {
		return (end_point1 - start_point).crossPoint(end_point2 - start_point);
	}
	double InnerLine(const Vec2d& start_point, const Vec2d& end_point1, const Vec2d& end_point2) const {
		return (end_point1 - start_point).InnerPoint(end_point2 - start_point);
	}
	double absMultiply(const Vec2d& start_point, const Vec2d& end_point1, const Vec2d& end_point2) {
		double abs_ft = std::sqrt(std::pow(end_point1.x() - start_point.x(),2) +std::pow(end_point1.y() - start_point.y(),2));
		double abs_sd = std::sqrt(std::pow(end_point2.x() - start_point.x(),2) +std::pow(end_point2.y() - start_point.y(),2));

		return abs_ft * abs_sd;
	}
	private:
	Vec2d start_;
	Vec2d end_;

};

class Polygon2d {
	public:
	Polygon2d() = default;
	explicit Polygon2d(std::vector<Vec2d> points) : points_(std::move(points)) {
		num_points_ = points_.size();
	};

	int prev(int index) {
		int prev_index = 0;
		if (index == 0) {
			prev_index = num_points_ - 1;
		} else {
			prev_index = index - 1;
		}
		return prev_index;
	}

	int next(int index) {
		int next_index = 0;
		if (index == num_points_ - 1) {
			next_index = 0;
		} else {
			next_index = index + 1;
		}
		return next_index;
	}

	bool is_convex() {
		double total_angle = (num_points_ - 2) * 180;
		double angle  = 0.0;
		for (int i = 0; i < num_points_; ++i ) {
			LineSegment line_ft(points_[prev(i)], points_[i]);

			// double cross_value = line_ft.CrossLine(points_[prev(i)], points_[i], points_[next(i)]);
			// if (cross_value < -kMathEpsilon) {
			// 	return false;
			// }
			
			double inner_value = line_ft.InnerLine(points_[i], points_[prev(i)],  points_[next(i)]);
			double abs_value = line_ft.absMultiply(points_[i], points_[prev(i)],  points_[next(i)]);
			angle += (acos(inner_value/abs_value) * 57.3);
		}
		if (angle < total_angle) {
			return false;
		}
		return true; 
	}


	private:
	std::vector<Vec2d> points_;
	int num_points_ = 0.0;
};

int main()
{

	std::vector<Vec2d> points;
	points.emplace_back(Vec2d(0,1));
	points.emplace_back(Vec2d(0, -1));
	points.emplace_back(Vec2d(1,-0.5));
	points.emplace_back(Vec2d(2,-1));
	points.emplace_back(Vec2d(2,1));
	points.emplace_back(Vec2d(1,0.5));
	Polygon2d object(points);

	cout << object.is_convex() << endl;

	std::vector<Vec2d> points_sd;
	points_sd.emplace_back(Vec2d(0,1));
	points_sd.emplace_back(Vec2d(0, -1));
	points_sd.emplace_back(Vec2d(1,-2));
	points_sd.emplace_back(Vec2d(2,-1));
	points_sd.emplace_back(Vec2d(2,1));
	points_sd.emplace_back(Vec2d(1,2));
	Polygon2d object_sd(points_sd);
	cout << object_sd.is_convex() << endl;

	cout << "====figting====" << endl;
	return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值