using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows;
using Microsoft.Research.DynamicDataDisplay;
namespace Microsoft.Research.DynamicDataDisplay.Charts.Shapes
{
///
/// Paints an arrow with start and end points in viewport coordinates.
///
public class Arrow : Segment
{
///
/// Initializes a new instance of the class.
///
public Arrow()
{
Init();
}
///
/// Initializes a new instance of the class.
///
/// The start point of arrow.
/// The end pointof arrow .
public Arrow(Point startPoint, Point endPoint)
: base(startPoint, endPoint)
{
Init();
}
private void Init()
{
geometryGroup.Children.Add(LineGeometry);
geometryGroup.Children.Add(leftLineGeometry);
geometryGroup.Children.Add(rightLineGeometry);
}
#region ArrowLength property
///
/// Gets or sets the length of the arrow.
///
/// The length of the arrow.
public double ArrowLength
{
get { return (double)GetValue(ArrowLengthProperty); }
set { SetValue(ArrowLengthProperty, value); }
}
///
/// Identifies ArrowLength dependency property.
///
public static readonly DependencyProperty ArrowLengthProperty = DependencyProperty.Register(
"ArrowLength",
typeof(double),
typeof(Arrow),
new FrameworkPropertyMetadata(0.1, OnPointChanged));
#endregion
#region ArrowAngle property
///
/// Gets or sets the arrow angle in degrees.
///
/// The arrow angle.
public double ArrowAngle
{
get { return (double)GetValue(ArrowAngleProperty); }
set { SetValue(ArrowAngleProperty, value); }
}
///
/// Identifies ArrowAngle dependency property.
///
public static readonly DependencyProperty ArrowAngleProperty = DependencyProperty.Register(
"ArrowAngle",
typeof(double),
typeof(Arrow),
new FrameworkPropertyMetadata(15.0, OnPointChanged));
#endregion
protected override void UpdateUIRepresentationCore()
{
base.UpdateUIRepresentationCore();
var transform = Plotter.Viewport.Transform;
Point p1 = StartPoint.DataToScreen(transform);
Point p2 = EndPoint.DataToScreen(transform);
Vector arrowVector = p1 - p2;
Vector arrowCapVector = ArrowLength * arrowVector;
Matrix leftMatrix = Matrix.Identity;
leftMatrix.Rotate(ArrowAngle);
Matrix rightMatrix = Matrix.Identity;
rightMatrix.Rotate(-ArrowAngle);
Vector leftArrowLine = leftMatrix.Transform(arrowCapVector);
Vector rightArrowLine = rightMatrix.Transform(arrowCapVector);
leftLineGeometry.StartPoint = p2;
rightLineGeometry.StartPoint = p2;
leftLineGeometry.EndPoint = p2 + leftArrowLine;
rightLineGeometry.EndPoint = p2 + rightArrowLine;
}
private LineGeometry leftLineGeometry = new LineGeometry();
private LineGeometry rightLineGeometry = new LineGeometry();
private GeometryGroup geometryGroup = new GeometryGroup();
protected override Geometry DefiningGeometry
{
get { return geometryGroup; }
}
}
}