코딩하는 나귀

[Struct] Size

Unity2015. 11. 7. 22:57

using System;


public struct Size {


    public static readonly Size Empty = new Size();


    private int width;

    private int height;


    public Size (Point pt) {

        width = pt.X;

        height = pt.Y;

    }


    public Size (int width, int height) {

        this.width = width;

        this.height = height;

    }


    public static implicit operator Size (Point p) {

        return new Size(p.X, p.Y);

    }


    public static explicit operator Point (Size s) {

        return new Point (s.Width, s.Height);

    }


    public static Size operator + (Size sz1, Size sz2) {

        return Add(sz1, sz2);

    }

    public static Size operator - (Size sz1, Size sz2) {

        return Subtract(sz1, sz2);

    }


    public static bool operator == (Size sz1, Size sz2) {

        return sz1.Width == sz2.Width && sz1.Height == sz2.Height;

    }


    public static bool operator != (Size sz1, Size sz2) {

        return !(sz1 == sz2);

    }


    public bool IsEmpty { get { return width == 0 && height == 0; } }


    public int Width {

        get { return width; }

        set { width = value; }

    }


    public int Height {

        get { return height; }

        set { height = value; }

    }


    public static Size Add (Size sz1, Size sz2) {

        return new Size(sz1.Width + sz2.Width, sz1.Height + sz2.Height);

    }


    public static Size Ceiling (Size value) {

        return new Size((int)Math.Ceiling((double)value.Width), (int)Math.Ceiling((double)value.Height));

    }


    public static Size Subtract (Size sz1, Size sz2) {

        return new Size(sz1.Width - sz2.Width, sz1.Height - sz2.Height);

    }


    public static Size Truncate (Size value) {

        return new Size((int)value.Width, (int)value.Height);

    }


    public static Size Round (Size value) {

        return new Size((int)Math.Round((double)value.Width), (int)Math.Round((double)value.Height));

    }


    public override bool Equals (object obj) {

        if (!(obj is Size)) return false;


        Size comp = (Size)obj;

        return (comp.width == this.width) && (comp.height == this.height);

    }


    public override int GetHashCode () {

        return width ^ height;

    }


    public override string ToString () {

        return "{Width=" + width.ToString() + ", Height=" + height.ToString() + "}";

    }

}