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

namespace CommonUsage.Geometries
{
	/// <summary>
	/// MDCS数学类：向量。
	/// </summary>
	public class Vectoriel
	{
        public Vectoriel()
        {
            _vec2 = Vector2.Zero;
            _dir2 = Vector2.Normalize(_vec2);
            _len = _vec2.Length();
            _angle = (float)(Math.Atan2(_vec2.Y, _vec2.X) / Math.PI * 180f);
        }

		public Vectoriel(Vector2 vec)
		{
			_vec2 = vec;
			_dir2 = Vector2.Normalize(_vec2);
			_len = _vec2.Length();
			_angle = (float)(Math.Atan2(_vec2.Y, _vec2.X) / Math.PI * 180f);
		}

		/// <summary>
		/// 通过笛卡尔坐标系X和Y值构建向量。
		/// </summary>
		/// <param name="x"></param>
		/// <param name="y"></param>
		/// <returns></returns>
		public static Vectoriel FromXY(float x, float y)
		{
			return new Vectoriel(new Vector2(x, y));
		}

		/// <summary>
		/// 通过极坐标系的角度和距离值构建向量。
		/// </summary>
		/// <param name="angle"></param>
		/// <param name="len"></param>
		/// <returns></returns>
		public static Vectoriel FromAngleLen(float angle, float len)
		{
			var rad = angle / 180f * Math.PI;
			return new Vectoriel(new Vector2((float)Math.Cos(rad), (float)Math.Sin(rad)) * len);
		}

		public static implicit operator Vector2(Vectoriel vec)
		{
			return vec._vec2;
		}

		public static explicit operator Vectoriel(Vector2 vec)
		{
			return FromXY(vec.X, vec.Y);
		}

		public Vector2 Direction => _dir2;

		public float Length => _len;

		public float Angle => _angle;

		private Vector2 _vec2, _dir2;
		private float _len, _angle;
	}
}
