Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/SharpVectorCore/Svg/SvgPointF.cs @ 13825

Last change on this file since 13825 was 12762, checked in by aballeit, 9 years ago

#2283 GUI updates, Tree-chart, MCTS Version 2 (prune leaves)

File size: 11.3 KB
Line 
1using System;
2using System.Globalization;
3
4namespace SharpVectors.Dom.Svg
5{
6    /// <summary>
7    /// This represents an ordered pair of float precision x- and y-coordinates
8    /// that defines a point in a two-dimensional plane.
9    /// </summary>
10    [Serializable]
11    public struct SvgPointF : IEquatable<SvgPointF>
12    {
13        #region Private Fields
14
15        /// <summary>
16        /// Represents a new instance of the <see cref="SvgPointF"/> structure
17        /// with member data left uninitialized.
18        /// </summary>
19        public static readonly SvgPointF Empty = new SvgPointF();
20
21        private double _x;
22        private double _y;
23        private bool   _notEmpty;
24
25        #endregion
26
27        #region Constructors and Destructor
28
29        /// <summary>
30        /// Initializes a new instance of the <see cref="SvgPointF"/> structure
31        /// with the specified coordinates.
32        /// </summary>
33        /// <param name="x">The x-coordinate of the point. </param>
34        /// <param name="y">The y-coordinate of the point. </param>
35        public SvgPointF(float x, float y)
36        {
37            _x        = x;
38            _y        = y;
39            _notEmpty = true;
40        }
41
42        /// <summary>
43        /// Initializes a new instance of the <see cref="SvgPointF"/> structure
44        /// with the specified coordinates.
45        /// </summary>
46        /// <param name="x">The x-coordinate of the point. </param>
47        /// <param name="y">The y-coordinate of the point. </param>
48        public SvgPointF(double x, double y)
49        {
50            _x        = x;
51            _y        = y;
52            _notEmpty = true;
53        }
54
55        #endregion
56
57        #region Public Properties
58
59        /// <summary>
60        /// Gets a value indicating whether this <see cref="SvgPointF"/> is empty.
61        /// </summary>
62        /// <value>
63        /// This is <see langword="true"/> if both <see cref="SvgPointF.X"/> and
64        /// <see cref="SvgPointF.Y"/> are 0; otherwise, <see langword="false"/>.
65        /// </value>
66        public bool IsEmpty
67        {
68            get
69            {
70                return !_notEmpty;
71            }
72        }
73
74        /// <summary>
75        /// Gets the x-coordinate of this <see cref="SvgPointF"/>.
76        /// </summary>
77        /// <value>
78        /// The x-coordinate of this <see cref="SvgPointF"/>.
79        /// </value>
80        public float X
81        {
82            get
83            {
84                return (float)_x;
85            }
86        }
87
88        /// <summary>
89        /// Gets the y-coordinate of this <see cref="SvgPointF"/>.
90        /// </summary>
91        /// <value>
92        /// The y-coordinate of this <see cref="SvgPointF"/>.
93        /// </value>
94        public float Y
95        {
96            get
97            {
98                return (float)_y;
99            }
100        }
101
102        /// <summary>
103        /// Gets or sets the x-coordinate of this <see cref="SvgPointF"/>.
104        /// </summary>
105        /// <value>
106        /// The x-coordinate of this <see cref="SvgPointF"/>.
107        /// </value>
108        public double ValueX
109        {
110            get
111            {
112                return _x;
113            }
114            set
115            {
116                _x        = value;
117                _notEmpty = true;
118            }
119        }
120
121        /// <summary>
122        /// Gets or sets the y-coordinate of this <see cref="SvgPointF"/>.
123        /// </summary>
124        /// <value>
125        /// The y-coordinate of this <see cref="SvgPointF"/>.
126        /// </value>
127        public double ValueY
128        {
129            get
130            {
131                return _y;
132            }
133            set
134            {
135                _y        = value;
136                _notEmpty = true;
137            }
138        }
139
140        #endregion
141
142        #region Public Operators
143
144        /// <summary>
145        /// This translates the <see cref="SvgPointF"/> by the specified
146        /// <see cref="SvgSizeF"/>.
147        /// </summary>
148        /// <param name="sz">
149        /// The <see cref="SvgSizeF"/> that specifies the numbers to add to the
150        /// x- and y-coordinates of the <see cref="SvgPointF"/>.
151        /// </param>
152        /// <param name="pt">The <see cref="SvgPointF"/> to translate.</param>
153        /// <returns>The translated <see cref="SvgPointF"/>.</returns>
154        public static SvgPointF operator +(SvgPointF pt, SvgSizeF sz)
155        {
156            return SvgPointF.Add(pt, sz);
157        }
158
159        /// <summary>
160        /// This translates a <see cref="SvgPointF"/> by the negative of a specified
161        /// <see cref="SvgSizeF"/>.
162        /// </summary>
163        /// <param name="sz">
164        /// The <see cref="SvgSizeF"/> that specifies the numbers to subtract from
165        /// the coordinates of pt.
166        /// </param>
167        /// <param name="pt">The <see cref="SvgPointF"/> to translate.</param>
168        /// <returns>The translated <see cref="SvgPointF"/>.</returns>
169        public static SvgPointF operator -(SvgPointF pt, SvgSizeF sz)
170        {
171            return SvgPointF.Subtract(pt, sz);
172        }
173
174        /// <summary>
175        /// This compares two <see cref="SvgPointF"/> structures. The result
176        /// specifies whether the values of the <see cref="SvgPointF.X"/> and
177        /// <see cref="SvgPointF.Y"/> properties of the two <see cref="SvgPointF"/>
178        /// structures are equal.
179        /// </summary>
180        /// <param name="right">A <see cref="SvgPointF"/> to compare. </param>
181        /// <param name="left">A <see cref="SvgPointF"/> to compare. </param>
182        /// <returns>
183        /// This is <see langword="true"/> if the <see cref="SvgPointF.X"/> and
184        /// <see cref="SvgPointF.Y"/> values of the left and right
185        /// <see cref="SvgPointF"/> structures are equal; otherwise, <see langword="false"/>.
186        /// </returns>
187        public static bool operator ==(SvgPointF left, SvgPointF right)
188        {
189            if (left.X.Equals(right.X))
190            {
191                return (left.Y.Equals(right.Y));
192            }
193
194            return false;
195        }
196
197        /// <summary>
198        /// This determines whether the coordinates of the specified points are
199        /// not equal.
200        /// </summary>
201        /// <param name="left">A <see cref="SvgPointF"/> to compare.</param>
202        /// <param name="right">A <see cref="SvgPointF"/> to compare.</param>
203        /// <returns>
204        /// This <see langword="true"/> to indicate the <see cref="SvgPointF.X"/>
205        /// and <see cref="SvgPointF.Y"/> values of left and right are not equal;
206        /// otherwise, <see langword="false"/>.
207        /// </returns>
208        public static bool operator !=(SvgPointF left, SvgPointF right)
209        {
210            return !(left == right);
211        }
212
213        #endregion
214
215        #region Public Methods
216
217        /// <summary>
218        /// This computes the distance between this <see cref="SvgPointF"/>
219        /// and the specified <see cref="SvgPointF"/>.
220        /// </summary>
221        /// <param name="point">
222        /// A <see cref="SvgPointF"/> object specifying the other point from
223        /// which to determine the distance.
224        /// </param>
225        /// <returns>
226        /// The distance between this point and the specified point.
227        /// </returns>
228        public double Distance(SvgPointF point)
229        {
230            return Math.Sqrt((point.X - this.X) * (point.X - this.X)
231                + (point.Y - this.Y) * (point.Y - this.Y));
232        }
233
234        /// <summary>
235        /// This determines whether this <see cref="SvgPointF"/> contains the same
236        /// coordinates as the specified <see cref="System.Object"/>.
237        /// </summary>
238        /// <param name="obj">The <see cref="System.Object"/> to test.
239        /// </param>
240        /// <returns>
241        /// This method returns <see langword="true"/> if the specified object
242        /// is a <see cref="SvgPointF"/> and has the same coordinates as this
243        /// <see cref="SvgPointF"/>; otherwise, <see langword="false"/>.
244        /// </returns>
245        public override bool Equals(object obj)
246        {
247            if (obj is SvgPointF)
248            {
249                return Equals((SvgPointF)obj);
250            }
251
252            return false;
253        }
254
255        /// <summary>
256        /// This determines whether this <see cref="SvgPointF"/> contains the same
257        /// coordinates as the specified <see cref="SvgPointF"/>.
258        /// </summary>
259        /// <param name="other">The <see cref="SvgPointF"/> to test.</param>
260        /// <returns>
261        /// This method returns <see langword="true"/> if the specified
262        /// <see cref="SvgPointF"/> has the same coordinates as this
263        /// <see cref="SvgPointF"/>; otherwise, <see langword="false"/>.
264        /// </returns>
265        public bool Equals(SvgPointF other)
266        {
267            if ((other.X == this.X) && (other.Y == this.Y))
268            {
269                return other.GetType().Equals(base.GetType());
270            }
271
272            return false;
273        }
274
275        /// <summary>
276        /// This returns a hash code for this <see cref="SvgPointF"/> structure.
277        /// </summary>
278        /// <returns>
279        /// An integer value that specifies a hash value for this
280        /// <see cref="SvgPointF"/> structure.
281        /// </returns>
282        public override int GetHashCode()
283        {
284            return (_x.GetHashCode() ^ _y.GetHashCode());
285        }
286
287        /// <summary>
288        /// This converts this <see cref="SvgPointF"/> to a human readable string.
289        /// </summary>
290        /// <returns>
291        /// A string that represents this <see cref="SvgPointF"/>.
292        /// </returns>
293        public override string ToString()
294        {
295            return string.Format(CultureInfo.CurrentCulture,
296                "{{X={0}, Y={1}}",  _x, _y);
297        }
298
299        #endregion
300
301        #region Public Static Methods
302
303        /// <summary>
304        /// This translates a given <see cref="SvgPointF"/> by a specified
305        /// <see cref="SvgSizeF"/>.
306        /// </summary>
307        /// <param name="pt">The <see cref="SvgPointF"/> to translate.</param>
308        /// <param name="sz">
309        /// The <see cref="SvgSizeF"/> that specifies the numbers to add to the
310        /// coordinates of pt.
311        /// </param>
312        /// <returns>The translated <see cref="SvgPointF"/>.</returns>
313        public static SvgPointF Add(SvgPointF pt, SvgSizeF sz)
314        {
315            return new SvgPointF(pt.X + sz.Width, pt.Y + sz.Height);
316        }
317
318        /// <summary>
319        /// This translates a <see cref="SvgPointF"/> by the negative of a
320        /// specified size.
321        /// </summary>
322        /// <param name="pt">The <see cref="SvgPointF"/> to translate.</param>
323        /// <param name="sz">
324        /// The <see cref="SvgSizeF"/> that specifies the numbers to subtract from
325        /// the coordinates of pt.
326        /// </param>
327        /// <returns>The translated <see cref="SvgPointF"/>.</returns>
328        public static SvgPointF Subtract(SvgPointF pt, SvgSizeF sz)
329        {
330            return new SvgPointF(pt.X - sz.Width, pt.Y - sz.Height);
331        }
332
333        #endregion
334    }
335}
Note: See TracBrowser for help on using the repository browser.