Share via


방법: PathGeometry에 LineSegment 만들기

이 예제에서는 선분을 만드는 방법을 보여 줍니다. 직선 세그먼트를 만들려면 PathGeometry, PathFigureLineSegment 클래스를 사용합니다.

예제

다음 예제에서는 (10, 50)에서 (200, 70)으로 LineSegment를 그립니다. 다음 그림에서는 결과 LineSegment를 보여 줍니다. 여기서는 좌표계를 표시하기 위해 그리드 배경이 추가되었습니다.

PathFigure의 LineSegment(10,50)부터 (200,70)까지 그린 LineSegment

XAML(Extensible Application Markup Language)에서 특성 구문을 사용하여 경로를 설명할 수 있습니다.

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

(이 특성 구문은 PathGeometry의 경량 버전인 StreamGeometry를 만듭니다. 자세한 내용은 경로 태그 구문 페이지를 참조하세요.)

XAML에서는 개체 요소 구문을 사용하여 직선 세그먼트를 그릴 수도 있습니다. 다음 예제는 이전 XAML 예제와 동일합니다.

<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

이 예제는 더 큰 샘플에 속합니다. 전체 샘플을 보려면 기하 도형 샘플을 참조하세요.

참고 항목