Compartir a través de


Cómo crear un SegmentoDeLínea en GeometríaDeRuta

En este ejemplo se muestra cómo crear un segmento de línea. Para crear un segmento de línea, use las PathGeometryclases , PathFigurey LineSegment .

Ejemplo

En los ejemplos siguientes se dibuja un LineSegment de (10, 50) a (200, 70). En la ilustración siguiente se muestra el LineSegmentresultado; se ha agregado un fondo de cuadrícula para mostrar el sistema de coordenadas.

Un Segmento de Línea en una Figura de Ruta Un Segmento de Línea dibujado de (10, 50) a (200, 70)

En Extensible Application Markup Language (XAML), puedes usar la sintaxis de atributo para describir una ruta de acceso.

<Path Stroke="Black" StrokeThickness="1"
  Data="M 10,50 L 200,70" />

(Tenga en cuenta que esta sintaxis de atributo realmente crea una versión de peso más ligero de StreamGeometry, PathGeometry. Para obtener más información, vea la página Sintaxis de marcado de ruta de acceso).

En XAML, también puedes dibujar un segmento de línea mediante la sintaxis del elemento de objeto. Lo siguiente es equivalente al ejemplo XAML anterior.

<Path Stroke="Black" StrokeThickness="1">
  <Path.Data>
    <PathGeometry>
      <PathFigure StartPoint="10,50">
        <LineSegment Point="200,70" />
      </PathFigure>
    </PathGeometry>
  </Path.Data>
</Path>
PathFigure myPathFigure = new PathFigure();
myPathFigure.StartPoint = new Point(10, 50);

LineSegment myLineSegment = new LineSegment();
myLineSegment.Point = new Point(200, 70);

PathSegmentCollection myPathSegmentCollection = new PathSegmentCollection();
myPathSegmentCollection.Add(myLineSegment);

myPathFigure.Segments = myPathSegmentCollection;

PathFigureCollection myPathFigureCollection = new PathFigureCollection();
myPathFigureCollection.Add(myPathFigure);

PathGeometry myPathGeometry = new PathGeometry();
myPathGeometry.Figures = myPathFigureCollection;

Path myPath = new Path();
myPath.Stroke = Brushes.Black;
myPath.StrokeThickness = 1;
myPath.Data = myPathGeometry;
Dim myPathFigure As New PathFigure()
myPathFigure.StartPoint = New Point(10, 50)

Dim myLineSegment As New LineSegment()
myLineSegment.Point = New Point(200, 70)

Dim myPathSegmentCollection As New PathSegmentCollection()
myPathSegmentCollection.Add(myLineSegment)

myPathFigure.Segments = myPathSegmentCollection

Dim myPathFigureCollection As New PathFigureCollection()
myPathFigureCollection.Add(myPathFigure)

Dim myPathGeometry As New PathGeometry()
myPathGeometry.Figures = myPathFigureCollection

Dim myPath As New Path()
myPath.Stroke = Brushes.Black
myPath.StrokeThickness = 1
myPath.Data = myPathGeometry

Este ejemplo forma parte del ejemplo más grande; para obtener el ejemplo completo, consulte el ejemplo de geometrías.

Consulte también