//
// System.Drawing.Point.cs
//
// Author:
// Mike Kestner (mkestner@speakeasy.net)
//
// Copyright (C) 2001 Mike Kestner
// Copyright (C) 2004 Novell, Inc. http://www.novell.com
//
using System.Globalization;
using System.Text.Json.Serialization;
namespace Terminal.Gui;
/// Represents an ordered pair of integer x- and y-coordinates that defines a point in a two-dimensional plane.
public partial struct Point
{
/// Gets or sets the x-coordinate of this Point.
[JsonInclude]
public int X;
/// Gets or sets the y-coordinate of this Point.
[JsonInclude]
public int Y;
// -----------------------
// Public Shared Members
// -----------------------
/// Empty Shared Field
/// An uninitialized Point Structure.
public static readonly Point Empty;
/// Addition Operator
/// Translates a Point using the Width and Height properties of the given Size.
public static Point operator + (Point pt, Size sz) { return new Point (pt.X + sz.Width, pt.Y + sz.Height); }
/// Equality Operator
///
/// Compares two Point objects. The return value is based on the equivalence of the X and Y properties of the two
/// points.
///
public static bool operator == (Point left, Point right) { return left.X == right.X && left.Y == right.Y; }
/// Inequality Operator
///
/// Compares two Point objects. The return value is based on the equivalence of the X and Y properties of the two
/// points.
///
public static bool operator != (Point left, Point right) { return left.X != right.X || left.Y != right.Y; }
/// Subtraction Operator
/// Translates a Point using the negation of the Width and Height properties of the given Size.
public static Point operator - (Point pt, Size sz) { return new Point (pt.X - sz.Width, pt.Y - sz.Height); }
/// Point to Size Conversion
/// Returns a Size based on the Coordinates of a given Point. Requires explicit cast.
public static explicit operator Size (Point p)
{
if (p.X < 0 || p.Y < 0)
{
throw new ArgumentException ("Either Width and Height must be greater or equal to 0.");
}
return new Size (p.X, p.Y);
}
// -----------------------
// Public Constructors
// -----------------------
/// Point Constructor
/// Creates a Point from a Size value.
public Point (Size sz)
{
X = sz.Width;
Y = sz.Height;
}
/// Point Constructor
/// Creates a Point from a specified x,y coordinate pair.
public Point (int x, int y)
{
X = x;
Y = y;
}
// -----------------------
// Public Instance Members
// -----------------------
/// IsEmpty Property
/// Indicates if both X and Y are zero.
[JsonIgnore]
public bool IsEmpty => X == 0 && Y == 0;
/// Equals Method
/// Checks equivalence of this Point and another object.
public override bool Equals (object obj)
{
if (!(obj is Point))
{
return false;
}
return this == (Point)obj;
}
/// GetHashCode Method
/// Calculates a hashing value.
public override int GetHashCode () { return X ^ Y; }
/// Offset Method
/// Moves the Point a specified distance.
public void Offset (int dx, int dy)
{
X += dx;
Y += dy;
}
/// ToString Method
/// Formats the Point as a string in coordinate notation.
public override string ToString ()
{
return string.Format (
"{{X={0},Y={1}}}",
X.ToString (CultureInfo.InvariantCulture),
Y.ToString (CultureInfo.InvariantCulture)
);
}
/// Adds the specified Size to the specified Point.
/// The Point that is the result of the addition operation.
/// The Point to add.
/// The Size to add.
public static Point Add (Point pt, Size sz) { return new Point (pt.X + sz.Width, pt.Y + sz.Height); }
/// Translates this Point by the specified Point.
/// The offset.
/// The Point used offset this Point.
public void Offset (Point p) { Offset (p.X, p.Y); }
/// Returns the result of subtracting specified Size from the specified Point.
/// The Point that is the result of the subtraction operation.
/// The Point to be subtracted from.
/// The Size to subtract from the Point.
public static Point Subtract (Point pt, Size sz) { return new Point (pt.X - sz.Width, pt.Y - sz.Height); }
}