1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows;
using System.Windows.Shapes;
namespace WpfSplineArrow
{
public class MyCurve : Shape
{
private PathGeometry pathgeo;
private PathFigure figureBezier;
private PolyBezierSegment polysegBezier;
List<PathFigure> arrowFigures;
public MyCurve()
{
Stroke = Brushes.Blue;
StrokeThickness = 2.0;
}
protected override Geometry DefiningGeometry
{
get { return CurveGeometry(); }
}
private Geometry CurveGeometry()
{
polysegBezier = new PolyBezierSegment();
figureBezier = new PathFigure();
figureBezier.IsClosed = false;
figureBezier.IsFilled = false;
figureBezier.Segments.Add(polysegBezier);
pathgeo = new PathGeometry();
pathgeo.Figures.Add(figureBezier);
arrowFigures = new List<PathFigure>();
polysegBezier.Points = DataPoints;
int division = polysegBezier.Points.Count;
for (int i = 0; i < division ; i++)
{
PathFigure arrowFig = new PathFigure();
PolyLineSegment segLine = new PolyLineSegment();
arrowFig.Segments.Add(segLine);
arrowFigures.Add(arrowFig);
}
double fraction = 1.0 / (double)division;
double progress = 0;
for (int i = 1; i < division-1; i++)
{
Point p1;
Point p;
progress = fraction*(double)i;
pathgeo.GetPointAtFractionLength(progress, out p1, out p);
Vector v = (Vector)p;
Point p2 = Point.Add(p1, v);
if (progress == 1.0 || progress > 1.0) break;
pathgeo.Figures.Add(CalculateArrow(arrowFigures[i], p1, p2));
}
return pathgeo;
}
PathFigure CalculateArrow(PathFigure pathfig, Point pt1, Point pt2)
{
Matrix matx = new Matrix();
Vector vect = pt1 - pt2;
vect.Normalize();
vect *= ArrowLength;
PolyLineSegment polyseg = pathfig.Segments[0] as PolyLineSegment;
polyseg.Points.Clear();
matx.Rotate(ArrowAngle / 2);
pathfig.StartPoint = pt2 + vect * matx;
polyseg.Points.Add(pt2);
matx.Rotate(-ArrowAngle);
polyseg.Points.Add(pt2 + vect * matx);
pathfig.IsClosed = IsArrowClosed;
return pathfig;
}
public PointCollection DataPoints
{
get { return (PointCollection)GetValue(DataPointsProperty); }
set { SetValue(DataPointsProperty, value); }
}
// Using a DependencyProperty as the backing store for DataPoints. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DataPointsProperty =
DependencyProperty.Register("DataPoints",
typeof(PointCollection), typeof(MyCurve),
new FrameworkPropertyMetadata(new PointCollection()));
/// <summary>
/// Identifies the IsArrowClosed dependency property.
/// </summary>
public static readonly DependencyProperty IsArrowClosedProperty =
DependencyProperty.Register("IsArrowClosed",
typeof(bool), typeof(MyCurve),
new FrameworkPropertyMetadata(false,
FrameworkPropertyMetadataOptions.AffectsMeasure));
/// <summary>
/// Gets or sets the property that determines if the arrow head
/// is closed to resemble a triangle.
/// </summary>
public bool IsArrowClosed
{
set { SetValue(IsArrowClosedProperty, value); }
get { return (bool)GetValue(IsArrowClosedProperty); }
}
/// <summary>
/// Identifies the ArrowAngle dependency property.
/// </summary>
public static readonly DependencyProperty ArrowAngleProperty =
DependencyProperty.Register("ArrowAngle",
typeof(double), typeof(MyCurve),
new FrameworkPropertyMetadata(45.0,
FrameworkPropertyMetadataOptions.AffectsMeasure));
/// <summary>
/// Gets or sets the angle between the two sides of the arrowhead.
/// </summary>
public double ArrowAngle
{
set { SetValue(ArrowAngleProperty, value); }
get { return (double)GetValue(ArrowAngleProperty); }
}
/// <summary>
/// Identifies the ArrowLength dependency property.
/// </summary>
public static readonly DependencyProperty ArrowLengthProperty =
DependencyProperty.Register("ArrowLength",
typeof(double), typeof(MyCurve),
new FrameworkPropertyMetadata(12.0,
FrameworkPropertyMetadataOptions.AffectsMeasure));
/// <summary>
/// Gets or sets the length of the two sides of the arrowhead.
/// </summary>
public double ArrowLength
{
set { SetValue(ArrowLengthProperty, value); }
get { return (double)GetValue(ArrowLengthProperty); }
}
}
} |
Partager