using System.Collections.Generic;
using Microsoft.Msagl.Core.Geometry;
using Microsoft.Msagl.Core.Layout;
namespace Microsoft.Msagl.Layout.Incremental {
///
/// A horizontal separation constraint requires a minimum separation between x coordinates of two nodes,
/// i.e. u.X + separation less or equal v.X
///
public class HorizontalSeparationConstraint : IConstraint {
bool equality;
///
///
///
public bool IsEquality {
get { return equality; }
}
private Node u;
///
/// Constrained to be vertically above the BottomNode
///
public Node LeftNode {
get { return u; }
}
private Node v;
///
/// Constrained to be vertically below the TopNode
///
public Node RightNode {
get { return v; }
}
private double separation;
///
/// We allow the separation of existing constraints to be modified by the user.
///
public double Separation {
get { return separation; }
set { separation = value; }
}
///
///
///
///
///
///
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704")]
public HorizontalSeparationConstraint(Node u, Node v, double separation)
{
this.u = u;
this.v = v;
this.separation = separation;
}
///
///
///
///
///
///
///
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704")]
public HorizontalSeparationConstraint(Node u, Node v, double separation, bool equality)
{
this.equality = equality;
this.u = u;
this.v = v;
this.separation = separation;
}
///
///
///
public virtual double Project() {
double uv = v.Center.X - u.Center.X;
double d = separation - uv;
if (d > 0) {
double
wu = ((FiNode)u.AlgorithmData).stayWeight,
wv = ((FiNode)v.AlgorithmData).stayWeight;
double f = d / (wu + wv);
u.Center = new Point(u.Center.X - wv * f, u.Center.Y);
v.Center = new Point(v.Center.X + wu * f, v.Center.Y);
return d;
} else {
return 0;
}
}
///
/// HorizontalSeparationConstraint are usually structural and therefore default to level 0
///
/// 0
public int Level { get { return 0; } }
///
/// Get the list of nodes involved in the constraint
///
public IEnumerable Nodes { get { return new Node[] { u, v }; } }
}
}