#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;
}
判断多边形是否为凹多边形
最新推荐文章于 2025-11-22 23:45:52 发布
273

被折叠的 条评论
为什么被折叠?



