summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core/Point.cs
blob: 5c6f54710de6d0dc23d623d2720b43ad65b831e5 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.Diagnostics;
using System.Globalization;

namespace Xamarin.Forms
{
	[DebuggerDisplay("X={X}, Y={Y}")]
	[TypeConverter(typeof(PointTypeConverter))]
	public struct Point
	{
		public double X { get; set; }

		public double Y { get; set; }

		public static Point Zero = new Point();

		public override string ToString()
		{
			return string.Format("{{X={0} Y={1}}}", X.ToString(CultureInfo.InvariantCulture), Y.ToString(CultureInfo.InvariantCulture));
		}

		public Point(double x, double y) : this()
		{
			X = x;
			Y = y;
		}

		public Point(Size sz) : this()
		{
			X = sz.Width;
			Y = sz.Height;
		}

		public override bool Equals(object o)
		{
			if (!(o is Point))
				return false;

			return this == (Point)o;
		}

		public override int GetHashCode()
		{
			return X.GetHashCode() ^ (Y.GetHashCode() * 397);
		}

		public Point Offset(double dx, double dy)
		{
			Point p = this;
			p.X += dx;
			p.Y += dy;
			return p;
		}

		public Point Round()
		{
			return new Point(Math.Round(X), Math.Round(Y));
		}

		public bool IsEmpty
		{
			get { return (X == 0) && (Y == 0); }
		}

		public static explicit operator Size(Point pt)
		{
			return new Size(pt.X, pt.Y);
		}

		public static Point operator +(Point pt, Size sz)
		{
			return new Point(pt.X + sz.Width, pt.Y + sz.Height);
		}

		public static Point operator -(Point pt, Size sz)
		{
			return new Point(pt.X - sz.Width, pt.Y - sz.Height);
		}

		public static bool operator ==(Point ptA, Point ptB)
		{
			return (ptA.X == ptB.X) && (ptA.Y == ptB.Y);
		}

		public static bool operator !=(Point ptA, Point ptB)
		{
			return (ptA.X != ptB.X) || (ptA.Y != ptB.Y);
		}

		public double Distance(Point other)
		{
			return Math.Sqrt(Math.Pow(X - other.X, 2) + Math.Pow(Y - other.Y, 2));
		}
	}
}