Point.cpp

#include "Point.h"


using namespace Robotics;
using namespace System;
using namespace System::Diagnostics;


/// [ Constructor ]
Point::Point( double X, double Y ) :
    m_x( X ),
    m_y( Y )
{
}


/// 符号を反転して返す [ Operator Overloading ]
Point Point::operator-()
{
    Point result( -m_x, -m_y );
    return result;
}


/// 和 += [ Operator Overloading ]
Point Point::operator+=( Point% obj )
{
    m_x += obj.m_x;
    m_y += obj.m_y;

    return *this;
}

/// 差 -= [ Operator Overloading ]
Point Point::operator-=( Point% obj )
{
    m_x -= obj.m_x;
    m_y -= obj.m_y;

    return *this;
}

/// 積 *= [ Operator Overloading ]
Point Point::operator*=( double val )
{
    m_x *= val;
    m_y *= val;

    return *this;
}

/// 商 /= [ Operator Overloading ]
Point Point::operator/=( double val )
{
    m_x /= val;
    m_y /= val;

    return *this;
}


/// 和 + [ Operator Overloading ]
Point Point::operator+( Point% left, Point% right )
{
    Point result = left;
    result += right;

    return result;
}

/// 差 - [ Operator Overloading ]
Point Point::operator-( Point% left, Point% right )
{
    Point result = left;
    result -= right;

    return result;
}


/// 積 * [ Operator Overloading ]
Point Point::operator*( double val, Point% obj )
{
    Point result = obj;
    result *= val;

    return result;
}

/// 積 * [ Operator Overloading ]
Point Point::operator*( Point% obj, double val )
{
    // 引数の順を入れ替えて、オーバーロードしている同一関数を呼び出す
    return val * obj;
}

/// 商 / [ Operator Overloading ]
Point Point::operator/( Point% obj, double val )
{
    Point result = obj;
    result /= val;

    return result;
}


/// 等価演算子 == [ Operator Overloading ]
bool Point::operator==( Point% left, Point% right )
{
    return Math::Abs( left.m_x - right.m_x ) < Double::Epsilon
        && Math::Abs( left.m_y - right.m_y ) < Double::Epsilon;
}

/// 不等価演算子 != [ Operator Overloading ]
bool Point::operator!=( Point% left, Point% right )
{
    // 等価演算の否定を返す
    return !( left == right );
}


/// 原点か?
bool Point::IsInOrigin()
{
    return (
        ( Math::Abs( m_x ) < Double::Epsilon ) &&
        ( Math::Abs( m_y ) < Double::Epsilon )
    );
}


/// PointF構造体に キャストして返す
System::Drawing::PointF Point::ToPointF()
{
    Drawing::PointF result(
        safe_cast< float >( m_x ),
        safe_cast< float >( m_y )
    );
    return result;
}


/// 距離を求める
double Point::CalculateDistance( Point% left, Point% right )
{
    double dx = left.X - right.X;
    double dy = left.Y - right.Y;

    return Math::Sqrt( dx * dx + dy * dy );

    /// @note 三平方の定理
}

/// 2つの位置を交換する
void Point::Swap( Point% left, Point% right )
{
    // コピーを生成する
    Point swap( left );

    left = right;
    right = swap;
}