(计算几何POJ step 8.1.1.2)POJ 1654 Area(使用叉积来计算多边形面积)

本文提供了一种解决POJ 1654问题的算法实现,通过解析输入字符串来计算由一系列坐标变化形成的多边形面积。使用了C++语言,并涉及点运算和叉积计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
 * POJ_1654.cpp
 *
 *  Created on: 2013年11月16日
 *      Author: Administrator
 */
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>

using namespace std;

const double epsi = 1e-10;
const double pi = acos(-1.0);
const int maxn = 100005;

int sign(const double& x){
	if(x > epsi){
		return 1;
	}else if(x < -epsi){
		return -1;
	}

	return 0;
}


struct Point{
	long long x,y;
	Point(long long _x = 0,long long _y = 0):x(_x),y(_y){

	}

	Point operator-(const Point& op2)const{
		return Point(x - op2.x,y - op2.y);
	}

	Point operator+(const Point& op2)const{
		return  Point(x + op2.x,y + op2.y);
	}

	double operator^(const Point& op2)const{
		return x*op2.y - y*op2.x;
	}
};

int main(){
	int t;
	string s;
	scanf("%d",&t);
	while(t--){
		cin >> s;

		int i;
		Point p =Point(0,0);
		Point p1;

		long long ans = 0;
		for(i = 0 ; i < s.size() ; ++i){
			if(s[i] == '1'){
				p1 = p + Point(-1,-1);
			}else if(s[i] == '2'){
				p1 = p + Point(0,-1);
			}else if(s[i] == '3'){
				p1 = p + Point(1,-1);
			}else if(s[i] == '4'){
				p1 = p + Point(-1,0);
			}else if(s[i] == '5'){
				p1 = Point(0,0);
			}else if(s[i] == '6'){
				p1 = p + Point(1,0);
			}else if(s[i] == '7'){
				p1 = p + Point(-1,1);
			}else if(s[i] == '8'){
				p1 = p + Point(0,1);
			}else if(s[i] == '9'){
				p1 = p + Point(1,1);
			}

			ans += p^p1;
			p = p1;
		}

		/**
		 * 使用叉积来计算多边形的面积:
		 *
		 */
		if(ans < 0){
			ans = -ans;
		}
		printf("%lld",ans/2);
		if(ans%2 != 0){
			printf(".5");
		}
		printf("\n");
	}

	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气的东哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值