2012-01-05 99 views
1

我建立了一个简单的StreamGeometry,使用MSDN例如:如何从StreamGeometry中提取Point对象?

StreamGeometry geometry = new StreamGeometry(); 
geometry.FillRule = FillRule.EvenOdd; 
using (StreamGeometryContext ctx = geometry.Open()) 
{ 
    ctx.BeginFigure(new Point(10, 100), true /* is filled */, true /* is closed */); 
    ctx.LineTo(new Point(100, 100), true /* is stroked */, false /* is smooth join */); 
    ctx.LineTo(new Point(100, 50), true /* is stroked */, false /* is smooth join */); 
} 
return geometry; 

我怎样才能找回从StreamGeometry的Point对象?
我似乎无法找到任何合适的方法。所有我能看到的是的ToString(),这使我的迷你语言格式:{M10,100L100,100 100,50z}

+0

都期待雅按照你的问题是什么点图? – 2012-01-05 09:45:32

+0

@Dmitry,我已经在代码中放置的那些 - 点(10,100),点(100,100),点(100,50) – 2012-01-05 09:47:56

+0

那么更多的贝齐尔,圆等等呢? – 2012-01-05 09:50:57

回答

1

这是我们为类似任务所做的。请注意,您的几何图形不得包含任何线条。基本上它是几何上的一个极小的抽象级别,Stroke()返回一个包含mimilanguage和Points的集合的元组。你可以通过编写一个简单的行为来使Navigator可绑定,如果感兴趣,请让我知道,我会调整我的答案。

更新1 - 的功能,它接受的minilang表达式,并返回一个点阵列:

public static Point[] Parse(string minilanguage) 
     { 
      // leave just M's 
      minilanguage = minilanguage.ToUpper().Replace("M", string.Empty); 

      // remove spaces 
      minilanguage = minilanguage 
       .ToCharArray() 
       .Where(t => t != ' ') 
       .Select(t => t.ToString()) 
       .Aggregate((f, s) => f + s).ToString(); 


      return minilanguage 
       .Split("L".ToCharArray()) 
       .Select(t => Point.Parse(t)) 
       .ToArray(); 
     } 

代码:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

namespace GeometryParts 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 

      this.Loaded += new RoutedEventHandler(MainWindow_Loaded); 
     } 

     void MainWindow_Loaded(object sender, RoutedEventArgs e) 
     { 
      Navigator navigator = new Navigator(0, 0); 

      navigator.NavigateTo(30, 30); 
      navigator.NavigateTo(60, 0); 

      Tuple<string, Point[]> stroke = navigator.Stroke(); 
      this.g.Data = Geometry.Parse(stroke.Item1); 

      MessageBox.Show(stroke.Item2.Length.ToString()); 
     } 
    } 


    public class Navigator 
    { 
     private List<Point> points = new List<Point>(); 

     public Navigator(double x, double y) 
     { 
      this.points.Add(new Point(x, y)); 
     } 

     public void NavigateTo(double x, double y) 
     { 
      this.points.Add(new Point(x, y)); 
     } 

     public Tuple<string, Point[]> Stroke() 
     { 
      string path = this.points.Select(t => t.ToString()).Aggregate((f, s) => "L" + f + " L" + s); 
      path = "M" + path.Substring(2, path.Length - 2); 

      Tuple<string, Point[]> stroke = new Tuple<string, Point[]>(path, this.points.ToArray()); 
      points.Clear(); 

      return stroke; 
     } 
    } 
} 

标记:

<Window x:Class="GeometryParts.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <Path Stroke="Gray" StrokeThickness="2" x:Name="g" /> 
    </Grid> 
</Window> 
2

创建后不能更改StreamGeometry。我很确定你不能访问你添加的点。你可以用PathGeometry代替吗?您可以通过PathGeometry.Figures属性访问和修改施工后的点。

+0

我可以将StreamGeometry转换为PathGeometry吗?我想使用StreamGeometry,因为它是有效的。我需要点的唯一原因是单元测试。 – 2012-01-05 09:25:40

+1

你可以通过迷你语言和一个类型转换器。我不会推荐它!如果这只是单元测试,为什么不在测试断言中使用迷你语言字符串? – ColinE 2012-01-05 09:27:16