UVa-143-Orchard Trees(果树林)[计算几何]

Problem
问题

An Orchardist has planted an orchard in a rectangle with trees uniformly spaced in both directions. Thus the trees form a rectangular grid and we can consider the trees to have integer coordinates. The origin of the coordinate system is at the bottom left of the following diagram:
有一个果园主种植了一片矩形的果树林,果树之间的间隔距离在纵横两个方向都相同。想象这片果树林形成了一个矩形的格网,每棵果树的纵横坐标值均为整数。坐标系统的原点定在下图的左下角:

 

142-1

 

Consider that we now overlay a series of triangles on to this grid. The vertices of the triangle can have any real coordinates in the range 0.0 to 100.0, thus trees can have coordinates in the range 1 to 99. Two possible triangles are shown.
我们现在要在这个格网上面放上一些三角形。三角形的顶点可以是0.0到100.0之间的任意实数,这样树的横纵坐标就限定在1到99之间。上图显示了两个可能三角形。

Write a program that will determine how many trees are contained within a given triangle. For the purposes of this problem, you may assume that the trees are of point size, and that any tree (point) lying exactly on the border of a triangle is considered to be in the triangle.
写一个程序计算出在给定的一个三角形内有多少树。为简化题目,你可以认为树都一个无穷小的点,而精确位于三角形边上的树应算作在三角形内的。

 

Input and Output
输入和输出

Input will consist of a series of lines. Each line will contain 6 real numbers in the range 0.00 to 100.00 representing the coordinates of a triangle. The entire file will be terminated by a line containing 6 zeroes (0 0 0 0 0 0).
输入由多行组成,每行包含6个范围在0.00到100.00之间的整数值,分别代表三角形的三个顶点坐标的x,y。全部的输入由6个0(0 0 0 0 0 0)表示结束。

Output will consist of one line for each triangle, containing the number of trees for that triangle right justified in a field of width 4.
每个输入的三角形对应一行输出,打印出在该三角形内部的树的数量。输出的数字应右对齐至第4列。

 

Sample input
输入示例

1.5 1.5  1.5 6.8  6.8 1.5
10.7 6.9  8.5 1.5  14.5 1.5
0 0 0 0 0 0

 

Sample output
输出示例

15
17

思路:这个题目其实不难,看懂题意之后就可以知道,就是要判断有多少个点在三角形内。

具体判断过程:

如果点K在三角形内就可以发现点k连接三角形三个顶点把三角形分成三个三角形,其面积和就是大三角形面积和。

所以如果点在三角形内那么,就有dcmp(sabc-(sabk+sack+scbk))==0。这道题要注意的是,当你遍历的时候,选择三个顶点中的最大最小X,最大最小Y,遍历这个矩形中的点就可以了,但是最大的X和Y不可以超过99,最小的X和Y不能小于1(题目要求),所以要对最小往上取整,最大往下取整。

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
using namespace std;
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define eps 1e-10

int dcmp(double x)
{
    if(fabs(x)<eps) return 0;
    return x<0?-1:1;
}

struct point
{
    double x,y;
}p[30];//点


double s(point a,point b,point c)
{
    double bax = b.x-a.x;
    double bay = b.y-a.y;
    double cax = c.x-a.x;
    double cay = c.y-a.y;
    return fabs(bax*cay-bay*cax);
}

int judge(point k)
{
    double sabc,sabk,sack,scbk;
    sabc=s(p[0],p[1],p[2]);
    sabk=s(k,p[0],p[1]);
    sack=s(k,p[0],p[2]);
    scbk=s(k,p[1],p[2]);
    if(dcmp(sabc-(sabk+sack+scbk))==0)
        return 1;
    else return 0;
}

int main()
{
    double x1,x2,x3,y1,y3,y2;
    while(scanf("%lf %lf %lf %lf %lf %lf",&x1,&y1,&x2,&y2,&x3,&y3))
    {
        if(!x1&&!x2&&!x3&&!y1&&!y2&&!y3) break;
        //cout<<313132<<endl;
        p[0].x=x1,p[0].y=y1;
        p[1].x=x2,p[1].y=y2;
        p[2].x=x3,p[2].y=y3;
       // int X,X1,Y,Y1;
        int X1= max(1, ceil(min(x1, min(x2, x3))));//求出最大最小xy
        int X = min(99, (int)max(x1, max(x2,x3)));
        int Y1 = max(1, ceil(min(y1, min(y2, y3))));
        int Y = min(99, (int)max(y1, max(y2, y3)));

        int sum=0;
        point k;
        for(double i=X1;i<=X;i++)
        {
            for(double j=Y1;j<=Y;j++)
            {
                k.x=i,k.y=j;
                if(judge(k))
                sum++;
            }
        }
       printf("%4d\n",sum);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值