1 minute read

[no-alignment]

구현 방식

  • 두 벡터의 외적의 크기는 평행사변형의 넓이와 같음을 이용한다.

코드

  • 최종 코드는 아래와 같다.
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <climits>
#include <cmath>


using namespace std;

struct Point;

double getArea(vector<Point>& polygon);
double getArea(Point& p1, Point& p2, Point& p3);

struct Point
{
    Point(){}
    Point(double x, double y, double z)
    {
        this->x = x;
        this->y = y;
        this->z = z;

    }
    double x;
    double y;
    double z = 0.0;

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

int main() {


    int N;
    cin >> N;
    vector<Point> polygon(N);

    for(int i = 0; i < N; i++)
    {
        cin >> polygon[i].x >> polygon[i].y;
    }

    printf("%.1lf",abs(round(getArea(polygon) * 100.0) / 100.0));

    return 0;
}


double getArea(vector<Point>& polygon)
{
    Point origin = polygon[0], prev = polygon[1];
    double result = 0.f;
    for(int i = 2; i < polygon.size(); i++)
    {
        result += getArea(origin, prev, polygon[i]);
        prev = polygon[i];
    }

    return result / 2.0 ;
}

double getArea(Point& p1, Point& p2, Point& p3)
{
    Point v1 = p2 - p1;
    Point v2 = p3 - p1;

    double result = (v1.y * v2.z - v2.y * v1.z)  +
             (v1.z * v2.x - v2.z * v1.x) +
             (v1.x * v2.y - v2.x * v1.y);


    return result;
}