PageRenderTime 39ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
C# | 67 lines | 57 code | 10 blank | 0 comment | 1 complexity | 223d506b0e6c6e38979c0911b0f31ec2 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System.Windows;
  2. using Microsoft.Research.DynamicDataDisplay.Common;
  3. using System.Diagnostics;
  4. namespace Microsoft.Research.DynamicDataDisplay
  5. {
  6. public static class RectExtensions
  7. {
  8. public static Point GetCenter(this Rect rect)
  9. {
  10. return new Point(rect.Left + rect.Width * 0.5, rect.Top + rect.Height * 0.5);
  11. }
  12. public static Rect FromCenterSize(Point center, Size size)
  13. {
  14. return FromCenterSize(center, size.Width, size.Height);
  15. }
  16. public static Rect FromCenterSize(Point center, double width, double height)
  17. {
  18. Rect res = new Rect(center.X - width / 2, center.Y - height / 2, width, height);
  19. return res;
  20. }
  21. public static Rect Zoom(this Rect rect, Point to, double ratio)
  22. {
  23. return CoordinateUtilities.RectZoom(rect, to, ratio);
  24. }
  25. public static Rect ZoomOutFromCenter(this Rect rect, double ratio)
  26. {
  27. return CoordinateUtilities.RectZoom(rect, rect.GetCenter(), ratio);
  28. }
  29. public static Rect ZoomInToCenter(this Rect rect, double ratio)
  30. {
  31. return CoordinateUtilities.RectZoom(rect, rect.GetCenter(), 1 / ratio);
  32. }
  33. public static Int32Rect ToInt32Rect(this Rect rect)
  34. {
  35. Int32Rect intRect = new Int32Rect(
  36. (int)rect.X,
  37. (int)rect.Y,
  38. (int)rect.Width,
  39. (int)rect.Height);
  40. return intRect;
  41. }
  42. [DebuggerStepThrough]
  43. public static DataRect ToDataRect(this Rect rect)
  44. {
  45. return new DataRect(rect);
  46. }
  47. internal static bool IsNaN(this Rect rect)
  48. {
  49. return !rect.IsEmpty && (
  50. rect.X.IsNaN() ||
  51. rect.Y.IsNaN() ||
  52. rect.Width.IsNaN() ||
  53. rect.Height.IsNaN()
  54. );
  55. }
  56. }
  57. }