1. 题目
题目来源:https://tianchi.aliyun.com/oj/15165469968503404/76745683722506851
2. 解题
采用直线的一般式进行判定
- 首先要能组成三角形
- 然后是我的位置和第3点在另外两点形成的直线同侧
class Solution {
public:
/**
* @param triangle: Coordinates of three points
* @param point: Xiaoqi's coordinates
* @return: Judge whether you can cast magic
*/
string castMagic(vector<vector<int>> &triangle, vector<int> &point) {
// write your code here
for(int i = 0; i < 3; i++)
{
if(!ok(triangle[i],triangle[(i+1)%3],triangle[(i+2)%3],point))
return "No";
}
return "Yes";
}
bool ok(vector<int> &p1, vector<int> &p2, vector<int> &p3, vector<int> &p)
{
int x1 = p1[0], y1 = p1[1], x2 = p2[0], y2 = p2[1], x3 = p3[0], y3 = p3[1];
int xi = p[0], yi = p[1];
int v1 = (y2-y1)*(x3-x1)-(y3-y1)*(x2-x1);
int v2 = (y2-y1)*(xi-x1)-(yi-y1)*(x2-x1);
if(v1==0 || v1*v2 < 0)//异号,说明,我的位置不在第3点的一侧,v1=0 不能组成三角形
return false;
return true;
}
};
我的CSDN博客地址 https://michael.blog.csdn.net/
长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!