yipo
6/2/2017 - 4:20 PM

Fourth Point!!

Fourth Point!!

#include <iomanip>
#include <iostream>
using namespace std;

template <typename T>
class _point
{
public:
    _point() : x(0), y(0)
    {
    }

    _point(const T &x, const T &y) : x(x), y(y)
    {
    }

    bool operator ==(const _point<T> &p) const
    {
        return x == p.x && y == p.y;
    }

    _point<T> operator +(const _point<T> &p) const
    {
        return _point<T>(x + p.x, y + p.y);
    }

    _point<T> operator -(const _point<T> &p) const
    {
        return _point<T>(x - p.x, y - p.y);
    }

    friend istream &operator >>(istream &is, _point<T> &p)
    {
        is >> p.x >> p.y;
        return is;
    }

    friend ostream &operator <<(ostream &os, const _point<T> &p)
    {
        os << p.x << " " << p.y;
        return os;
    }

private:
    T x, y;
};

using point = _point<double>;

point find_4th_point(point a1, point a2, point b1, point b2)
{
    for (int i = 1; i <= 4; i++) // Try for 4 different combinations.
    {
        switch (i)
        {
        case 2: case 4:
            swap(a1, a2);
            break;
        case 3:
            swap(b1, b2);
            break;
        }
        if (a1 == b1) break;
    }

    return b2 + (a2 - a1); // Simply do the math.
}

int main()
{
    cout << fixed << setprecision(3);

    point a1, a2, b1, b2;
    while (cin >> a1 >> a2 >> b1 >> b2)
    {
        cout << find_4th_point(a1, a2, b1, b2) << endl;
    }
    return 0;
}