果园里的树

#include<bits/stdc++.h>
using namespace std;
struct pos
{
    double x;
    double y;
};
//计算三角形的有向面积
double area(double x0,double y0,double x1,double y1,double x2,double y2)
{
    if(x0*y1+x2*y0+x1*y2-x0*y2-x1*y0-x2*y1>1e-9) return x0*y1+x2*y0+x1*y2-x0*y2-x1*y0-x2*y1;
    else return -(x0*y1+x2*y0+x1*y2-x0*y2-x1*y0-x2*y1);
}
int main()
{
    pos a,b,c;
    cin>>a.x>>a.y>>b.x>>b.y>>c.x>>c.y;
   // cout<<a.x<<" "<<c.y<<" "<<b.x<<endl;
    /*判断在给出的三角形里的树的多少树
    首先计算出所给出三角形的面积
    Sabc=Soab=Soac=Sobc时,o点在所给出的三角形内部
    三角形的面积可以通过有向面积的计算给出
    1.5 1.5     1.5 6.8     6.8 1.5
    */
    int count1=0;
    for(int i=1;i<=99;i++)
        for(int j=1;j<=99;j++)
        {
           double flag=area(a.x,a.y,b.x,b.y,c.x,c.y)-(area(a.x,a.y,b.x,b.y,i+0.0,j+0.0)+area(a.x,a.y,c.x,c.y,i+0.0,j+0.0)+area(b.x,b.y,c.x,c.y,i+0.0,j+0.0));

           if(fabs(flag)<1e-9) count1++;
        }
    cout<<count1<<endl;
}
原文地址:https://www.cnblogs.com/superxuezhazha/p/5269093.html