HDU2108和HDU2036(叉乘)

hdu2108

判断是否为凸边形

判断连续三点的叉乘

若为凸,内角<180;若为凹,内角>180

所以通过正负来判断


#include 
#include 
#include 
#include 
#include 
#include 
#include 
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int N=100050;
struct node
{
    int x,y;
}pnode[N];


int judge(int x,int y,int z)
{
    return (pnode[y].x - pnode[x].x)*(pnode[z].y - pnode[y].y)-(pnode[z].x - pnode[y].x)*(pnode[y].y - pnode[x].y);
}

int main()
{
    int n;

    while(scanf("%d",&n) && n)
    {
        for(int i = 1;i <= n;i++)
            scanf("%d%d",&pnode[i].x,&pnode[i].y);
        pnode[n+1].x = pnode[1].x;
        pnode[n+1].y = pnode[1].y;
        pnode[n+2].x = pnode[2].x;
        pnode[n+2].y = pnode[2].y;
        int flag = 0;
        for(int i = 1;i <= n;i++)
        {
            if(judge(i,i+1,i+2)<0)
            {
                flag = 1;
                break;
            }
        }
        if(flag )
            printf("concave\n");
        else
            printf("convex\n");

    }
    return 0;
}


hdu2036

求一个多边形的面积

分成许多个三角形来计算。   S△ = 向量AB 与 向量AC的叉乘/2



#include 
#include 
int main()
{
    int n,i,j;
    double S,x[101],y[101];
    while(scanf("%d",&n) && n != 0)
    {
        for(i = 0;i < n;i++)
            scanf("%lf%lf",&x[i],&y[i]);
        x[i] = x[0];
        y[i] = y[0];
        S = 0;
        for(j = 0;j < i;j++)
            S += x[j] * y[j+1] - x[j+1] * y[j];
        S = fabs(S / 2);
            printf("%.1lf\n",S);
    }
    return 0;
}

  

转载于:https://www.cnblogs.com/Przz/p/5409794.html

你可能感兴趣的:(HDU2108和HDU2036(叉乘))