aboutsummaryrefslogtreecommitdiffstatsci
path: root/Drawing2D/src/Point.cs
blob: 72bb22326daeb5f023ddf854d3383daf35f0a996 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright (c) 2013-2022  Bruyère Jean-Philippe jp_bruyere@hotmail.com
//
// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)

using System;

namespace Drawing2D
{
	public struct Point : IEquatable<Point>, IEquatable<int>
    {
		public int X, Y;

		#region CTOR
		public Point (int x, int y) {
			X = x;
			Y = y;
		}
		public Point (int pos) {
			X = pos;
			Y = pos;
		}
		#endregion

		public int Length => (int)Math.Sqrt (Math.Pow (X, 2) + Math.Pow (Y, 2));
		public double LengthD => Math.Sqrt (Math.Pow (X, 2) + Math.Pow (Y, 2));
		public Point Normalized {
			get {
				int l = Length;
				return new Point (X / l, Y / l);
			}
		}
		public static implicit operator PointD (Point p) => new PointD (p.X, p.Y);
		public static implicit operator Point (int i) => new Point (i, i);

		public static Point operator + (Point p1, Point p2) => new Point (p1.X + p2.X, p1.Y + p2.Y);
		public static Point operator + (Point p, int i) => new Point (p.X + i, p.Y + i);
		public static Point operator - (Point p1, Point p2) => new Point (p1.X - p2.X, p1.Y - p2.Y);
		public static Point operator - (Point p, int i) => new Point (p.X - i, p.Y - i);
		public static Point operator * (Point p1, Point p2) => new Point (p1.X * p2.X, p1.Y * p2.Y);
		public static Point operator * (Point p, int d) => new Point (p.X * d, p.Y * d);
		public static Point operator / (Point p1, Point p2) => new Point (p1.X / p2.X, p1.Y / p2.Y);
		public static Point operator / (Point p, int d) => new Point (p.X / d, p.Y / d);

		public static bool operator == (Point s1, Point s2) => s1.Equals (s2);
		public static bool operator != (Point s1, Point s2) => !s1.Equals (s2);
		public static bool operator == (Point s, int i) => s.Equals(i);
		public static bool operator != (Point s, int i) => !s.Equals (i);
		public static bool operator > (Point p1, Point p2) => p1.X > p2.X && p1.Y > p2.Y;
		public static bool operator > (Point s, int i) => s.X > i && s.Y > i;
		public static bool operator < (Point p1, Point p2) => p1.X < p2.X && p1.Y < p2.Y;
		public static bool operator < (Point s, int i) => s.X < i && s.Y < i;
		public static bool operator >= (Point p1, Point p2) => p1.X >= p2.X && p1.Y >= p2.Y;
		public static bool operator >= (Point s, int i) => s.X >= i && s.Y >= i;
		public static bool operator <= (Point p1, Point p2) => p1.X <= p2.X && p1.Y <= p2.Y;
		public static bool operator <= (Point s, int i) => s.X <= i && s.Y <= i;

		public bool Equals (Point other) => X == other.X && Y == other.Y;
		public bool Equals (int other) => X == other && Y == other;


		public override int GetHashCode () => HashCode.Combine (X, Y);
		public override bool Equals (object obj) => obj is Point s ? Equals (s) : false;
		public override string ToString () => $"{X},{Y}";
		public static Point Parse (string s) {
			ReadOnlySpan<char> tmp = s.AsSpan ();
			if (tmp.Length == 0)
				return default (Point);
			int ioc = tmp.IndexOf (',');
			return ioc < 0 ? new Point (int.Parse (tmp)) : new Point (
				int.Parse (tmp.Slice (0, ioc)),
				int.Parse (tmp.Slice (ioc + 1)));
		}
    }
}