Hoj 2183 Extrusion

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

本题核心是求任意多边形的面积。

我们可以用叉积来求。任取一点,顺时针取相邻两点,与这点组成两个向量,进行叉积。顺时针一周求得的所有叉积和求反除以二即是多边形的面积。

#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[100000];

double cross(Point O,Point A,Point B)
{
    return (A.x - O.x) *  (B.y - O.y) - (B.x - O.x) * (A.y - O.y);
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif
    int n;
    double vol;
    while(scanf(" %d",&n)!=EOF && n>=3)
    {
        p[0].x = p[0].y = 0;
        for(int i=1;i<=n;i++)
        {
            scanf(" %lf %lf",&p[i].x,&p[i].y);
        }
        scanf(" %lf",&vol);
        p[n+1].x = p[1].x;
        p[n+1].y = p[1].y;
        double area = 0;
        for(int i=1;i<=n;i++)
        {
            area += cross(p[0],p[i],p[i+1]);
        }
        area = -area;
        area /=2;
        double len;
        len = vol/area;
        printf("BAR LENGTH: %.2lf\n",len);
    }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值