2013-05-23 2 views

ответ

4

Раньше я использовал около Arrowhead Charles Charles.

http://www.charlespetzold.com/blog/2007/04/191200.html

+0

Да, я уже нашел эту библиотеку, но я не знаю, как настроить стиль стрелочной стрелки, например, точка-точка или пунктирная точка ... –

+0

@ DanielPeñalba - Вы пробовали 'StrokeDashArray' ArrowLine? имущество? Например: 'myArrow.StrokeDashArray = new DoubleCollection() {1, 1};' или '... new DoubleCollection() {3, 2, 1, 2}'. См. Этот ответ для более примера StrokeDash: [Рисовать линии и круги в WPF] (http://stackoverflow.com/questions/3702140/draw-lines-and-circles-in-wpf#answer-3702296) – Sphinxxx

0

Я создал следующий метод, который создает PointCollection для линии со стрелкой:

private const double _maxArrowLengthPercent = 0.3; // factor that determines how the arrow is shortened for very short lines 
private const double _lineArrowLengthFactor = 3.73205081; // 15 degrees arrow: = 1/Math.Tan(15 * Math.PI/180); 

public static PointCollection CreateLineWithArrowPointCollection(Point startPoint, Point endPoint, double lineWidth) 
{ 
    Vector direction = endPoint - startPoint; 

    Vector normalizedDirection = direction; 
    normalizedDirection.Normalize(); 

    Vector normalizedlineWidenVector = new Vector(-normalizedDirection.Y, normalizedDirection.X); // Rotate by 90 degrees 
    Vector lineWidenVector = normalizedlineWidenVector * lineWidth * 0.5; 

    double lineLength = direction.Length; 

    double defaultArrowLength = lineWidth * _lineArrowLengthFactor; 

    // Prepare usedArrowLength 
    // if the length is bigger than 1/3 (_maxArrowLengthPercent) of the line length adjust the arrow length to 1/3 of line length 

    double usedArrowLength; 
    if (lineLength * _maxArrowLengthPercent < defaultArrowLength) 
     usedArrowLength = lineLength * _maxArrowLengthPercent; 
    else 
     usedArrowLength = defaultArrowLength; 

    // Adjust arrow thickness for very thick lines 
    double arrowWidthFactor; 
    if (lineWidth <= 1.5) 
     arrowWidthFactor = 3; 
    else if (lineWidth <= 2.66) 
     arrowWidthFactor = 4; 
    else 
     arrowWidthFactor = 1.5 * lineWidth; 

    Vector arrowWidthVector = normalizedlineWidenVector * arrowWidthFactor; 


    // Now we have all the vectors so we can create the arrow shape positions 
    var pointCollection = new PointCollection(7); 

    Point endArrowCenterPosition = endPoint - (normalizedDirection * usedArrowLength); 

    pointCollection.Add(endPoint); // Start with tip of the arrow 
    pointCollection.Add(endArrowCenterPosition + arrowWidthVector); 
    pointCollection.Add(endArrowCenterPosition + lineWidenVector); 
    pointCollection.Add(startPoint + lineWidenVector); 
    pointCollection.Add(startPoint - lineWidenVector); 
    pointCollection.Add(endArrowCenterPosition - lineWidenVector); 
    pointCollection.Add(endArrowCenterPosition - arrowWidthVector); 

    return pointCollection; 
} 

Вы можете чем легко создать форму линии со следующим кодом:

var points = CreateLineWithArrowPointCollection(new Point(0, 0), new Point(200, 100), 5); 

var polygon = new Polygon(); 
polygon.Points = points; 
polygon.Fill = Brushes.Red; 

RootCanvas.Children.Add(polygon); 

Код также поддерживает сокращение стрелок для коротких линий.

Смежные вопросы