8、判断三角形ABC中是否有点D

思路:

首先连接AD,BD,CD,SABC为三角形的面积,SABD为三角形ABD的面积,SACD....,SBCD.......

因此,若D在三角形则SABC = SABD + SACD + SBCD,若不等,则不在

#include<iostream>
#include<math.h>
using namespace std;
#define ABS_FLOAT_0 0.0001

struct point
{
    float x;
    float y;
};


/**

 计算三角形面积

*/
float areas(const point p1, const point p2, const point p3)
{
    point AB, BC;
    AB.x = p1.x - p2.x;
    AB.y = p1.y - p1.x;
    BC.x = p3.x - p2.x;
    BC.y = p3.y - p3.y;
    float s = fabs((AB.x*BC.y - BC.x*AB.y) / 2.0);
    if (s > 0)
    {
        return s;
    }
    else if (s < 0)
    {
        s = -s;
        return s;
    }

}
/**

判断给定一点是否在三角形内或边上

*/

bool Disareas(const point A, const point B, const point C, const point D)
{
    float SABC, SABD, SADC, SBCD;
    SABC = areas(A, B, C);
    SABD = areas(A, B, D);
    SADC = areas(A, D, C);
    SBCD = areas(B, C, D);
    float SABCD = SABD + SADC + SBCD;
    if ((SABC - SABCD) > -ABS_FLOAT_0 && (SABC - SABCD) < ABS_FLOAT_0)
    {
        return true;
    }
    else{
        return false;
    }

}
原文地址:https://www.cnblogs.com/zhuifeng-mayi/p/10764754.html