PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Main/src/DynamicDataDisplay/Common/Auxiliary/DataRectExtensions.cs

#
C# | 115 lines | 71 code | 13 blank | 31 comment | 1 complexity | c78b011c727e968e78ebedf7932fe667 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Media.TextFormatting;
  7. using Microsoft.Research.DynamicDataDisplay.Common;
  8. namespace Microsoft.Research.DynamicDataDisplay
  9. {
  10. public static class DataRectExtensions
  11. {
  12. internal static bool IsNaN( this DataRect rect )
  13. {
  14. return !rect.IsEmpty &&
  15. (
  16. rect.XMin.IsNaN() ||
  17. rect.YMin.IsNaN() ||
  18. rect.XMax.IsNaN() ||
  19. rect.YMax.IsNaN()
  20. );
  21. }
  22. /// <summary>
  23. /// Gets the center of specified rectangle.
  24. /// </summary>
  25. /// <param name="rect">The rect.</param>
  26. /// <returns></returns>
  27. public static Point GetCenter( this DataRect rect )
  28. {
  29. return new Point( rect.XMin + rect.Width * 0.5, rect.YMin + rect.Height * 0.5 );
  30. }
  31. public static DataRect Zoom( this DataRect rect, Point to, double ratio )
  32. {
  33. return CoordinateUtilities.RectZoom( rect, to, ratio );
  34. }
  35. /// <summary>
  36. /// Zooms out from center.
  37. /// </summary>
  38. /// <param name="rect">The rect.</param>
  39. /// <param name="ratio">The ratio.</param>
  40. /// <returns></returns>
  41. public static DataRect ZoomOutFromCenter( this DataRect rect, double ratio )
  42. {
  43. return CoordinateUtilities.RectZoom( rect, rect.GetCenter(), ratio );
  44. }
  45. /// <summary>
  46. /// Zooms in to center.
  47. /// </summary>
  48. /// <param name="rect">The rect.</param>
  49. /// <param name="ratio">The ratio.</param>
  50. /// <returns></returns>
  51. public static DataRect ZoomInToCenter( this DataRect rect, double ratio )
  52. {
  53. return CoordinateUtilities.RectZoom( rect, rect.GetCenter(), 1 / ratio );
  54. }
  55. public static DataRect ZoomX( this DataRect rect, Point to, double ratio )
  56. {
  57. return CoordinateUtilities.RectZoomX( rect, to, ratio );
  58. }
  59. public static DataRect ZoomY( this DataRect rect, Point to, double ratio )
  60. {
  61. return CoordinateUtilities.RectZoomY( rect, to, ratio );
  62. }
  63. /// <summary>
  64. /// Gets the square of specified DataRect.
  65. /// </summary>
  66. /// <param name="rect">The rect.</param>
  67. /// <returns></returns>
  68. public static double GetSquare( this DataRect rect )
  69. {
  70. if ( rect.IsEmpty )
  71. return 0;
  72. return rect.Width * rect.Height;
  73. }
  74. /// <summary>
  75. /// Determines whether one DataRect is close to another DataRect.
  76. /// </summary>
  77. /// <param name="rect1">The rect1.</param>
  78. /// <param name="rect2">The rect2.</param>
  79. /// <param name="difference">The difference.</param>
  80. /// <returns>
  81. /// <c>true</c> if [is close to] [the specified rect1]; otherwise, <c>false</c>.
  82. /// </returns>
  83. public static bool IsCloseTo( this DataRect rect1, DataRect rect2, double difference )
  84. {
  85. DataRect intersection = DataRect.Intersect( rect1, rect2 );
  86. double square1 = rect1.GetSquare();
  87. double square2 = rect2.GetSquare();
  88. double intersectionSquare = intersection.GetSquare();
  89. bool areClose = MathHelper.AreClose( square1, intersectionSquare, difference ) &&
  90. MathHelper.AreClose( square2, intersectionSquare, difference );
  91. return areClose;
  92. }
  93. public static DataRect WithX( this DataRect rect, double xmin, double xmax )
  94. {
  95. return DataRect.Create( xmin, rect.YMin, xmax, rect.YMax );
  96. }
  97. public static DataRect WithY( this DataRect rect, double ymin, double ymax )
  98. {
  99. return DataRect.Create( rect.XMin, ymin, rect.XMax, ymax );
  100. }
  101. }
  102. }