自分でベクトルを設計しよう(その1)

■ベクトルを管理するクラスを作ってみよう
「Vector2」があるのにわざわざ作るのかという疑問はありますが、自分が使いやすいように作ってみました

■ソース
Vec2.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace WindowsGame
{
    class Vec2
    {
        public float X;
        public float Y;

        public Vec2()
        {
            Set(0, 0);
        }

        public Vec2(float x, float y)
        {
            Set(x, y);
        }

        public void Set(float x, float y)
        {
            X = x;
            Y = y;
        }

        public float Abs()
        {
            return (float)Math.Sqrt(X * X + Y * Y);
        }

        public float Abs2()
        {
            return (float)(X * X + Y * Y);
        }

        public static Vec2 operator +(Vec2 vec1, Vec2 vec2)
        {
            return new Vec2(vec1.X + vec2.X, vec1.Y + vec2.Y);
        }

        public static Vec2 operator -(Vec2 vec1, Vec2 vec2)
        {
            return new Vec2(vec1.X - vec2.X, vec1.Y - vec2.Y);
        }

        /// 外積
        public static float operator *(Vec2 vec1, Vec2 vec2)
        {
            return (vec1.X * vec2.Y - vec1.Y * vec2.X );
        }

        /// 内積
        public static float operator %(Vec2 vec1, Vec2 vec2)
        {
            return (vec1.X * vec2.X + vec1.Y * vec2.Y);
        }

        public static Vec2 operator *(Vec2 vec1, float f)
        {
            return new Vec2(vec1.X * f, vec1.Y * f);
        }

        public static Vec2 operator /(Vec2 vec1, float f)
        {
            return new Vec2(vec1.X / f, vec1.Y / f);
        }

        public Vec2 Norm()
        {
            float d = this.Abs();
            return new Vec2(X / d, Y / d);
        }

        public override bool Equals(System.Object obj)
        {
            if (obj == null)
                return false;

            Vec2 p = obj as Vec2;
            if ((System.Object)p == null)
                return false;
             return (X == p.X) && (Y == p.Y);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public static bool operator ==(Vec2 p1, Vec2 p2)
        {
            return p1.Equals(p2);
        }

        public static bool operator !=(Vec2 p1, Vec2 p2)
        {
            return !p1.Equals(p2);
        }
    }
}

■ソース説明
ここの部分は普通のクラスと変わらないので省きます

 public float Abs()
 {
     return (float)Math.Sqrt(X * X + Y * Y);
 }
 public float Abs2()
 {
     return (float)(X * X + Y * Y);
 }

やっぱ、クラス同士のプラスマイナス
\normalsize\left(\large\begin{array}{GC+23}\varepsilon_x\\\varepsilon_y\end{array}\right)\Large=\normalsize\left(\large\begin{array}{GC+23}\sigma_x\\\sigma_y\end{array}\right)\Large+\normalsize\left(\large\begin{array}{GC+23}\tau_{x}\\\tau_{y}\end{array}\right)

 public static Vec2 operator +(Vec2 vec1, Vec2 vec2)
 {
     return new Vec2(vec1.X + vec2.X, vec1.Y + vec2.Y);
 }

\normalsize\left(\large\begin{array}{GC+23}\varepsilon_x\\\varepsilon_y\end{array}\right)\Large=\normalsize\left(\large\begin{array}{GC+23}\sigma_x\\\sigma_y\end{array}\right)\Large-\normalsize\left(\large\begin{array}{GC+23}\tau_{x}\\\tau_{y}\end{array}\right)

 public static Vec2 operator -(Vec2 vec1, Vec2 vec2)
 {
     return new Vec2(vec1.X - vec2.X, vec1.Y - vec2.Y);
 }