-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPoint.cs
More file actions
203 lines (173 loc) · 7.14 KB
/
Point.cs
File metadata and controls
203 lines (173 loc) · 7.14 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System;
using System.Collections.Generic;
using System.Text;
namespace SFML.Game.Framework;
public class Point
{
/// <summary>
/// Gets or sets the X coordinate of this <see cref="Point"/>.
/// </summary>
public int X { get; set; }
/// <summary>
/// Gets or sets the Y coordinate of this <see cref="Point"/>.
/// </summary>
public int Y { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="Point"/> class.
/// </summary>
public Point()
{
X = 0;
Y = 0;
}
/// <summary>
/// Initializes a new instance of the <see cref="Point"/> class with specified coordinates.
/// </summary>
/// <param name="x">The X coordinate.</param>
/// <param name="y">The Y coordinate.</param>
public Point( int x, int y )
{
X = x;
Y = y;
}
/// <summary>
/// Returns the distance between two <see cref="Point"/> instances.
/// </summary>
/// <param name="a">The first point.</param>
/// <param name="b">The second point.</param>
/// <returns>The distance between points as a float.</returns>
public static float Distance( Point a, Point b )
=> MathF.Sqrt( ( a.X - b.X ) * ( a.X - b.X ) + ( a.Y - b.Y ) * ( a.Y - b.Y ) );
/// <summary>
/// Returns the squared distance between two <see cref="Point"/> instances (avoids square root).
/// </summary>
/// <param name="a">The first point.</param>
/// <param name="b">The second point.</param>
/// <returns>The squared distance as an integer.</returns>
public static int DistanceSquared( Point a, Point b )
=> ( a.X - b.X ) * ( a.X - b.X ) + ( a.Y - b.Y ) * ( a.Y - b.Y );
/// <summary>
/// Offsets this <see cref="Point"/> by the specified X and Y amounts.
/// </summary>
/// <param name="offsetX">The X offset.</param>
/// <param name="offsetY">The Y offset.</param>
public void Offset( int offsetX, int offsetY )
{
X += offsetX;
Y += offsetY;
}
/// <summary>
/// Returns a new <see cref="Point"/> offset by the specified amount.
/// </summary>
/// <param name="offsetX">The X offset.</param>
/// <param name="offsetY">The Y offset.</param>
/// <returns>A new offset point.</returns>
public Point OffsetCopy( int offsetX, int offsetY )
=> new Point( X + offsetX, Y + offsetY );
/// <summary>
/// Converts this <see cref="Point"/> to a <see cref="Vector2"/>.
/// </summary>
/// <returns>A <see cref="Vector2"/> with the same coordinates.</returns>
public Vector2 ToVector2() => new Vector2( X, Y );
/// <summary>
/// Returns a new <see cref="Point"/> created by adding two points.
/// </summary>
/// <param name="a">The first point.</param>
/// <param name="b">The second point.</param>
/// <returns>A new point representing the sum.</returns>
public static Point Add( Point a, Point b )
=> new Point( a.X + b.X, a.Y + b.Y );
/// <summary>
/// Returns a new <see cref="Point"/> created by subtracting one point from another.
/// </summary>
/// <param name="a">The first point.</param>
/// <param name="b">The second point.</param>
/// <returns>A new point representing the difference.</returns>
public static Point Subtract( Point a, Point b )
=> new Point( a.X - b.X, a.Y - b.Y );
/// <summary>
/// Returns a new <see cref="Point"/> scaled by the given factor.
/// </summary>
/// <param name="point">The point to scale.</param>
/// <param name="scale">The scale factor.</param>
/// <returns>A new scaled point.</returns>
public static Point Multiply( Point point, float scale )
=> new Point( ( int )( point.X * scale ), ( int )( point.Y * scale ) );
/// <summary>
/// Returns the dot product of two points treated as vectors.
/// </summary>
/// <param name="a">The first point.</param>
/// <param name="b">The second point.</param>
/// <returns>The dot product as a float.</returns>
public static float Dot( Point a, Point b )
=> a.X * b.X + a.Y * b.Y;
/// <summary>
/// Returns the length (magnitude) of this <see cref="Point"/> treated as a vector.
/// </summary>
/// <returns>The magnitude as a float.</returns>
public float Length() => MathF.Sqrt( X * X + Y * Y );
// ----------------------------------------------------------
// Operator Overloads
// ----------------------------------------------------------
/// <summary>
/// Adds two <see cref="Point"/> instances.
/// </summary>
public static Point operator +( Point a, Point b ) => new Point( a.X + b.X, a.Y + b.Y );
/// <summary>
/// Subtracts one <see cref="Point"/> from another.
/// </summary>
public static Point operator -( Point a, Point b ) => new Point( a.X - b.X, a.Y - b.Y );
/// <summary>
/// Negates the coordinates of this <see cref="Point"/>.
/// </summary>
public static Point operator -( Point p ) => new Point( -p.X, -p.Y );
/// <summary>
/// Multiplies a <see cref="Point"/> by a scalar value.
/// </summary>
public static Point operator *( Point p, float scale )
=> new Point( ( int )( p.X * scale ), ( int )( p.Y * scale ) );
/// <summary>
/// Divides a <see cref="Point"/> by a scalar value.
/// </summary>
public static Point operator /( Point p, float scale )
=> new Point( ( int )( p.X / scale ), ( int )( p.Y / scale ) );
/// <summary>
/// Determines whether two <see cref="Point"/> instances are equal.
/// </summary>
public static bool operator ==( Point a, Point b ) => a.X == b.X && a.Y == b.Y;
/// <summary>
/// Determines whether two <see cref="Point"/> instances are not equal.
/// </summary>
public static bool operator !=( Point a, Point b ) => !( a == b );
// ----------------------------------------------------------
// Utility Overrides
// ----------------------------------------------------------
/// <summary>
/// Determines whether this <see cref="Point"/> is equal to another object.
/// </summary>
/// <param name="obj">The object to compare to.</param>
/// <returns>True if equal; otherwise, false.</returns>
public override bool Equals( object? obj )
=> obj is Point p && X == p.X && Y == p.Y;
/// <summary>
/// Returns a hash code for this <see cref="Point"/>.
/// </summary>
/// <returns>An integer hash code.</returns>
public override int GetHashCode() => HashCode.Combine( X, Y );
/// <summary>
/// Returns a string representation of this <see cref="Point"/>.
/// </summary>
/// <returns>A string in the format (X, Y).</returns>
public override string ToString() => $"({X}, {Y})";
// ----------------------------------------------------------
// Static Members
// ----------------------------------------------------------
/// <summary>
/// Gets a <see cref="Point"/> representing (0, 0).
/// </summary>
public static readonly Point Zero = new Point(0, 0);
/// <summary>
/// Gets a <see cref="Point"/> representing (1, 1).
/// </summary>
public static readonly Point One = new Point(1, 1);
}