阿里云 超级码力在线编程大赛初赛 第2场 题目1. 三角魔法

文章目录

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阿明),一起加油、一起学习进步!
Michael阿明

猜你喜欢

转载自blog.csdn.net/qq_21201267/article/details/108309955