判断一个点是否在三角形内
2016-09-19 22:32
597 查看
平面上给定一个三角形ABC以及任意点P,判断点P是否是三角形内的点(点P在三角形边上也认为是在三角形内),输入的点坐标都是单精度浮点数。
实现原理参考:http://www.yalewoo.com/in_triangle_test.html
其实现代码如下:
#include <cstdio>
#include <iostream>
#include<vector>
using namespace std;
const float e = 0.000001;
struct Point{
float x;
float y;
Point(float a, float b) :x(a), y(b){ }
};
float cross(const Point &a, const Point &b, const Point &p)
{
return (b.x - a.x)*(p.y - a.y) - (b.y - a.y)*(p.x - a.x);
}
bool inTriangle(const Point &p, const Point &a, const Point &b, const Point &c)
{
if (cross(a, b, p) < 0 && cross(b, c, p) < 0 && cross(c, a, p) < 0)
return true;
if (cross(a, b, p) > 0 && cross(b, c, p) > 0 && cross(c, a, p) > 0)
return true;
if ((cross(a, b, p) - 0.0 > -e) && (cross(a, b, p) - 0.0 < e))
return true;
if ((cross(b, c, p) - 0.0 > -e) && (cross(b, c, p) - 0.0 < e))
return true;
if ((cross(c, a, p) - 0.0 > -e) && (cross(c, a, p) - 0.0 < e))
return true;
return false;
}
int main()
{
float x;
vector<float> vec;
for (int i = 0; i !=8; ++i)
{
cin >> x;
vec.push_back(x);
}
Point A(vec[0], vec[1]);
Point B(vec[2], vec[3]);
Point C(vec[4], vec[5]);
Point D(vec[6], vec[7]);
if (inTriangle(D, A, B, C))
cout <<"Yes"<<endl;
else
cout << "No"<<endl;
system("pause");
return 0;
}实验结果如下:
实现原理参考:http://www.yalewoo.com/in_triangle_test.html
其实现代码如下:
#include <cstdio>
#include <iostream>
#include<vector>
using namespace std;
const float e = 0.000001;
struct Point{
float x;
float y;
Point(float a, float b) :x(a), y(b){ }
};
float cross(const Point &a, const Point &b, const Point &p)
{
return (b.x - a.x)*(p.y - a.y) - (b.y - a.y)*(p.x - a.x);
}
bool inTriangle(const Point &p, const Point &a, const Point &b, const Point &c)
{
if (cross(a, b, p) < 0 && cross(b, c, p) < 0 && cross(c, a, p) < 0)
return true;
if (cross(a, b, p) > 0 && cross(b, c, p) > 0 && cross(c, a, p) > 0)
return true;
if ((cross(a, b, p) - 0.0 > -e) && (cross(a, b, p) - 0.0 < e))
return true;
if ((cross(b, c, p) - 0.0 > -e) && (cross(b, c, p) - 0.0 < e))
return true;
if ((cross(c, a, p) - 0.0 > -e) && (cross(c, a, p) - 0.0 < e))
return true;
return false;
}
int main()
{
float x;
vector<float> vec;
for (int i = 0; i !=8; ++i)
{
cin >> x;
vec.push_back(x);
}
Point A(vec[0], vec[1]);
Point B(vec[2], vec[3]);
Point C(vec[4], vec[5]);
Point D(vec[6], vec[7]);
if (inTriangle(D, A, B, C))
cout <<"Yes"<<endl;
else
cout << "No"<<endl;
system("pause");
return 0;
}实验结果如下:
相关文章推荐
- 性能测试工具总结
- 抽象类与接口的思考
- Makefile中指示符“include”、“-include”和“sinclude”的区别
- 用Spring MVC开发简单的Web应用
- C与C艹的内存管理方式
- HDU 5884 Sort
- Lock接口中的几个方法讨论
- 你只看到别人表面的光鲜,却看不到他们背后的付出和努力
- HDU5112【水】
- 反转单链表的几种方法
- linux系统分区day0919
- 实验二:跨交换机实现VLAN
- 2016 ACM/ICPC Asia Regional Shenyang Online HDU 5894 hannnnah_j’s Biological Test
- 21、在选择的时候跳到另一页面上,选择男女,然后提交表单内容
- Linux Linux程序练习十(网络编程大文件发送)
- 关于dubbo服务的xml配置文件报错的问题
- 内存对齐
- ios 关于PNG和JPEG格式压缩
- deBug日志_关于android studio导入9.png导致编译报错
- pthread_mutex_init等多线程函数的使用总结