Hoj 1188 Orchard Trees

题目:http://acm.hit.edu.cn/hoj/problem/view?id=1188

本题核心在于如何判定一个点P是否在三角形ABC内(包括在边上)。

方法是:看PAB、PAC、PBC面积之和是否等于ABC的面积即可。

求三角形面积可以用叉积来求,注意X的范围在[1,99],不包括0和100,Y一样。

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>
using namespace std;

struct Point
{
    double x;
    double y;
};
Point p[3];

//求面积绝对值
double cross(Point O,Point A,Point B)
{
    return fabs((A.x - O.x) *  (B.y - O.y) - (B.x - O.x) * (A.y - O.y))/2;
}

bool judge()
{
    for(int i=0; i<3; i++)
    {
        if(p[i].x!=0 || p[i].y!=0)
        {
            return true;
        }
    }
    return false;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif
    while(scanf(" %lf %lf %lf %lf %lf %lf",&p[0].x,&p[0].y,&p[1].x,&p[1].y,&p[2].x,&p[2].y) && judge())
    {
        double maxX = 0,minX = 150;
        double maxY = 0,minY = 150;
        for(int i=0; i<3; i++)
        {
            if(p[i].x>maxX)
                maxX = p[i].x;
            if(p[i].y>maxY)
                maxY = p[i].y;
            if(p[i].x<minX)
                minX = p[i].x;
            if(p[i].y<minY)
                minY = p[i].y;
        }
        //求三角形的面积
        double area = cross(p[0],p[1],p[2]);
        int num = 0;
        for(int i=ceil(minX); i<=floor(maxX); i++)
        {
            if(i <= 0 || i>=100)
            {
                continue;
            }
            for(int j=ceil(minY); j<=floor(maxY); j++)
            {
                if(j <= 0 || j>=100)
                {
                    continue;
                }
                double s = 0;
                Point temp;
                temp.x = i;
                temp.y = j;
                s += cross(temp,p[0],p[1]);
                s += cross(temp,p[1],p[2]);
                s += cross(temp,p[2],p[0]);
                if(fabs(area-s)<=1e-8)
                {
                    num++;
                }
            }
        }
        printf("%4d\n",num);
    }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值