BoundingBox.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using FastReport.Utils;
  5. using System.IO;
  6. using System.Globalization;
  7. using System.ComponentModel;
  8. namespace FastReport.Map
  9. {
  10. /// <summary>
  11. /// Represents four coordinates that define a bounding box.
  12. /// </summary>
  13. [TypeConverter(typeof(ExpandableObjectConverter))]
  14. public class BoundingBox
  15. {
  16. #region Fields
  17. private double minX;
  18. private double minY;
  19. private double maxX;
  20. private double maxY;
  21. #endregion // Fields
  22. #region Properties
  23. /// <summary>
  24. /// Gets or sets the minimum X-coordinate of a bounding box.
  25. /// </summary>
  26. public double MinX
  27. {
  28. get { return minX; }
  29. set { minX = value; }
  30. }
  31. /// <summary>
  32. /// Gets or sets the minimum Y-coordinate of a bounding box.
  33. /// </summary>
  34. public double MinY
  35. {
  36. get { return minY; }
  37. set { minY = value; }
  38. }
  39. /// <summary>
  40. /// Gets or sets the maximum X-coordinate of a bounding box.
  41. /// </summary>
  42. public double MaxX
  43. {
  44. get { return maxX; }
  45. set { maxX = value; }
  46. }
  47. /// <summary>
  48. /// Gets or sets the maximum Y-coordinate of a bounding box.
  49. /// </summary>
  50. public double MaxY
  51. {
  52. get { return maxY; }
  53. set { maxY = value; }
  54. }
  55. #endregion // Properties
  56. #region Private Methods
  57. private string ToString(double value)
  58. {
  59. return value.ToString(CultureInfo.InvariantCulture.NumberFormat);
  60. }
  61. private double FromString(string value)
  62. {
  63. return double.Parse(value, CultureInfo.InvariantCulture.NumberFormat);
  64. }
  65. #endregion
  66. #region Public Methods
  67. /// <summary>
  68. /// Copies the contents of another <see cref="BoundingBox"/> instance.
  69. /// </summary>
  70. /// <param name="src">Source box to copy the contents from.</param>
  71. public void Assign(BoundingBox src)
  72. {
  73. MinX = src.MinX;
  74. MinY = src.MinY;
  75. MaxX = src.MaxX;
  76. MaxY = src.MaxY;
  77. }
  78. internal string GetAsString()
  79. {
  80. StringBuilder result = new StringBuilder();
  81. result.Append(ToString(minX)).Append(",").
  82. Append(ToString(minY)).Append(",").
  83. Append(ToString(maxX)).Append(",").
  84. Append(ToString(maxY));
  85. return result.ToString();
  86. }
  87. internal void SetAsString(string value)
  88. {
  89. string[] arr = value.Split(',');
  90. minX = FromString(arr[0]);
  91. minY = FromString(arr[1]);
  92. maxX = FromString(arr[2]);
  93. maxY = FromString(arr[3]);
  94. }
  95. internal void Load(Stream stream)
  96. {
  97. byte[] buffer8 = new byte[8];
  98. stream.Read(buffer8, 0, buffer8.Length);
  99. MinX = BitConverter.ToDouble(buffer8, 0);
  100. stream.Read(buffer8, 0, buffer8.Length);
  101. MinY = BitConverter.ToDouble(buffer8, 0);
  102. stream.Read(buffer8, 0, buffer8.Length);
  103. MaxX = BitConverter.ToDouble(buffer8, 0);
  104. stream.Read(buffer8, 0, buffer8.Length);
  105. MaxY = BitConverter.ToDouble(buffer8, 0);
  106. }
  107. internal void Save(Stream stream)
  108. {
  109. byte[] buffer8 = BitConverter.GetBytes(MinX);
  110. stream.Write(buffer8, 0, buffer8.Length);
  111. buffer8 = BitConverter.GetBytes(MinY);
  112. stream.Write(buffer8, 0, buffer8.Length);
  113. buffer8 = BitConverter.GetBytes(MaxX);
  114. stream.Write(buffer8, 0, buffer8.Length);
  115. buffer8 = BitConverter.GetBytes(MaxY);
  116. stream.Write(buffer8, 0, buffer8.Length);
  117. }
  118. #endregion // Public Methods
  119. }
  120. }