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

/Main/src/DynamicDataDisplay/ViewportConstraints/DomainConstraint.cs

#
C# | 80 lines | 54 code | 5 blank | 21 comment | 11 complexity | 2fdf83da8c7cf20b1f66bcedd97aa68b 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 Microsoft.Research.DynamicDataDisplay.Common;
  7. namespace Microsoft.Research.DynamicDataDisplay.ViewportConstraints
  8. {
  9. /// <summary>
  10. /// Represents a constraint which returns data rectangle, intersected with specified data domain.
  11. /// </summary>
  12. public class DomainConstraint : ViewportConstraint
  13. {
  14. /// <summary>
  15. /// Initializes a new instance of the <see cref="DomainConstraint"/> class.
  16. /// </summary>
  17. public DomainConstraint() { }
  18. /// <summary>
  19. /// Initializes a new instance of the <see cref="DomainConstraint"/> class with given domain property.
  20. /// </summary>
  21. /// <param name="domain">The domain.</param>
  22. public DomainConstraint(DataRect domain)
  23. {
  24. this.Domain = domain;
  25. }
  26. private DataRect domain = new DataRect(-1, -1, 2, 2);
  27. /// <summary>
  28. /// Gets or sets the domain.
  29. /// </summary>
  30. /// <value>The domain.</value>
  31. public DataRect Domain
  32. {
  33. get { return domain; }
  34. set
  35. {
  36. if (domain != value)
  37. {
  38. domain = value;
  39. RaiseChanged();
  40. }
  41. }
  42. }
  43. /// <summary>
  44. /// Applies the specified old data rect.
  45. /// </summary>
  46. /// <param name="oldDataRect">The old data rect.</param>
  47. /// <param name="newDataRect">The new data rect.</param>
  48. /// <param name="viewport">The viewport.</param>
  49. /// <returns></returns>
  50. public override DataRect Apply(DataRect oldDataRect, DataRect newDataRect, Viewport2D viewport)
  51. {
  52. DataRect res = domain;
  53. if (domain.IsEmpty)
  54. {
  55. res = newDataRect;
  56. }
  57. else if (newDataRect.IntersectsWith(domain))
  58. {
  59. res = newDataRect;
  60. if (newDataRect.Size == oldDataRect.Size)
  61. {
  62. if (res.XMin < domain.XMin) res.XMin = domain.XMin;
  63. if (res.YMin < domain.YMin) res.YMin = domain.YMin;
  64. if (res.XMax > domain.XMax) res.XMin += domain.XMax - res.XMax;
  65. if (res.YMax > domain.YMax) res.YMin += domain.YMax - res.YMax;
  66. }
  67. else
  68. {
  69. res = DataRect.Intersect(newDataRect, domain);
  70. }
  71. }
  72. return res;
  73. }
  74. }
  75. }