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