PdfPig/src/UglyToad.PdfPig/Geometry/PdfPoint.cs

127 lines
4.0 KiB
C#
Raw Normal View History

namespace UglyToad.PdfPig.Geometry
{
using System.Diagnostics;
/// <summary>
/// A point in a PDF file.
/// </summary>
/// <remarks>
/// PDF coordinates are defined with the origin at the lower left (0, 0).
/// The Y-axis extends vertically upwards and the X-axis horizontally to the right.
/// Unless otherwise specified on a per-page basis, units in PDF space are equivalent to a typographic point (1/72 inch).
/// </remarks>
public struct PdfPoint
{
/// <summary>
/// The origin of the coordinates system.
/// </summary>
public static PdfPoint Origin { get; } = new PdfPoint(0m, 0m);
/// <summary>
/// The X coordinate for this point. (Horizontal axis).
/// </summary>
public decimal X { get; }
/// <summary>
/// The Y coordinate of this point. (Vertical axis).
/// </summary>
public decimal Y { get; }
/// <summary>
/// Create a new <see cref="PdfPoint"/> at this position.
/// </summary>
[DebuggerStepThrough]
public PdfPoint(decimal x, decimal y)
{
X = x;
Y = y;
}
/// <summary>
/// Create a new <see cref="PdfPoint"/> at this position.
/// </summary>
[DebuggerStepThrough]
public PdfPoint(int x, int y)
{
X = x;
Y = y;
}
/// <summary>
/// Create a new <see cref="PdfPoint"/> at this position.
/// </summary>
[DebuggerStepThrough]
public PdfPoint(double x, double y)
{
X = (decimal)x;
Y = (decimal)y;
}
/// <summary>
/// Creates a new <see cref="PdfPoint"/> which is the current point moved in the x direction relative to its current position by a value.
/// </summary>
/// <param name="dx">The distance to move the point in the x direction relative to its current location.</param>
/// <returns>A new point shifted on the x axis by the given delta value.</returns>
public PdfPoint MoveX(decimal dx)
{
return new PdfPoint(X + dx, Y);
}
/// <summary>
/// Creates a new <see cref="PdfPoint"/> which is the current point moved in the y direction relative to its current position by a value.
/// </summary>
/// <param name="dy">The distance to move the point in the y direction relative to its current location.</param>
/// <returns>A new point shifted on the y axis by the given delta value.</returns>
public PdfPoint MoveY(decimal dy)
{
return new PdfPoint(X, Y + dy);
}
2018-03-31 06:16:54 +08:00
internal PdfVector ToVector()
{
return new PdfVector(X, Y);
}
2019-08-10 02:58:48 +08:00
/// <summary>
/// Converts this <see cref="PdfPoint"/> into an array.
/// </summary>
/// <returns></returns>
public double[] ToDouble()
{
return new double[] { (double)this.X, (double)this.Y };
}
/// <summary>
/// Returns a value indicating whether this <see cref="PdfPoint"/> is equal to a specified <see cref="PdfPoint"/> .
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(object obj)
{
if (obj is PdfPoint)
{
PdfPoint point = (PdfPoint)obj;
return point.X == this.X && point.Y == this.Y;
}
return false;
}
/// <summary>
/// Returns the hash code for this <see cref="PdfPoint"/>.
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
return (int)(this.X * 10_000 + 31 * this.Y * 10_000);
}
/// <summary>
/// Get a string representation of this point.
/// </summary>
public override string ToString()
{
return $"(x:{X}, y:{Y})";
}
}
}