计算几何刷题

面积

  1. 传送门
    求面积,注意下内存开的很小,因为起点在原点,那么直接记录相邻的两个点,然后点积求面积即可
#include <iostream>
#include <cstdio>
#include <cstring>
#define ll long long
using namespace std;
ll ABS(ll x){return x > 0 ? x : -x;}
/*
8 北 (x, y + 1) 
2 南 (x, y - 1)
6 东 (x + 1, y)
4 西 (x - 1, y)
9 东北 (x + 1, y + 1)
7 西北 (x - 1, y + 1)
3 东南 (x + 1, y - 1)
1 西南 (x - 1, y - 1)
*/
int dir1[] = {0, -1, 0, 1, -1, 0, 1, -1, 0, 1};
int dir2[] = {0, -1, -1, -1, 0, 0, 0, 1, 1, 1};
void solve(){
    char s[2];
    ll x = 0, y = 0, px = 0, py = 0;
    ll area = 0;
    while(1){
        scanf("%1s", s);
        if(s[0] == '5') break;
        px += dir1[s[0] - '0'], py += dir2[s[0] - '0'];
        area += (px * y - py * x);
        x = px, y = py;
    }
    area = ABS(area);
    if(area % 2 == 0) printf("%lld\n", area / 2);
    else printf("%lld.5\n", area / 2);
}
int main(){
    int T;cin >> T;
    for(int i = 1; i <= T; i++){
        solve();
    }
    return 0;
}
  1. 传送门
    pick定理,求三角形里的格点数,求边上的格点数用gcd即可
#include <iostream>
#include <cstdio>
#define ll long long
using namespace std;
struct Point{
    ll x, y;
    Point (ll x = 0, ll y = 0):x(x),y(y){};
    Point operator - (const Point &b) const {
        return Point(x - b.x, y - b.y);
    }
};
ll ABS(ll x) {return x > 0 ? x : -x;}
ll Cross(Point a, Point b){
    return (a.x * b.y - a.y * b.x);
}
ll gcd(ll a, ll b){
    return b == 0 ? a : gcd(b, a % b);
}
ll cal(Point a, Point b){
    ll m = ABS(a.x - b.x);
    ll n = ABS(a.y - b.y);
    if(m == 0 && n == 0) return 0;
    if(m == 0) return n - 1;
    if(n == 0) return m - 1;
    return gcd(n, m) - 1;
}
int main(){
    cout << cal(Point(6, 0), Point(0, 9)) << endl;
    int a, b, c, d, e, f;
    while(scanf("%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f)){
        if(a == b && b == c && e == f && a == b && c == e) break;
        Point aa(a, b), bb(c, d), cc(e, f);
        ll S = ABS(Cross(bb - aa, cc - aa)) / 2;
        ll B = (cal(aa, bb) + cal(bb, cc) + cal(aa, cc) + 3) / 2;
        printf("%lld\n", S - B + 1);
    }
    return 0;
}
posted @ 2020-08-11 21:06  Emcikem  阅读(99)  评论(0编辑  收藏  举报