2012-05-19

Articles

PathCalculator

 

As soon you need the length of a path you can use this object. It can calculate the length of any path no matter of the shape.

 

alt

 

Usage

<UserControl x:Class="DW.SharpTools.Demo.PathCalculatorControl"
			 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
			 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
			 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
			 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
			 mc:Ignorable="d" 
			 d:DesignHeight="300" d:DesignWidth="300">
	<DockPanel Background="Transparent">
		<TextBlock Text="Leftclick and drag the line end" DockPanel.Dock="Top" />
		<StackPanel Orientation="Horizontal" DockPanel.Dock="Bottom">
			<TextBlock Text="Path Length:" />
			<TextBlock x:Name="length" Margin="5,0" />
		</StackPanel>
		<Path Stroke="Black" StrokeThickness="1">
			<Path.Data>
				<PathGeometry>
					<PathFigure StartPoint="0,0" x:Name="pathFigure">
						<LineSegment x:Name="segment" Point="150,150" />
					</PathFigure>
				</PathGeometry>
			</Path.Data>
		</Path>
	</DockPanel>
</UserControl>
using System.Windows.Controls;
using System.Windows.Input;

namespace DW.SharpTools.Demo
{
	public partial class PathCalculatorControl : UserControl
	{
		public PathCalculatorControl()
		{
			InitializeComponent();

			PreviewMouseMove += new MouseEventHandler(PathCalculatorControl_PreviewMouseMove);
		}

		private void PathCalculatorControl_PreviewMouseMove(object sender, MouseEventArgs e)
		{
			if (e.LeftButton == MouseButtonState.Pressed)
			{
				segment.Point = e.GetPosition(this);
				length.Text = PathCalculator.GetPathFigureLength(pathFigure).ToString();
			}
		}
	}
}