0
我是c#
和wpf
的新手,我想畫一個圖。我用CappedLine.cs作爲邊和橢圓作爲節點。我想繪製線條,直到到達圓圈並且不進入圓圈的邊界。如何連接線和圓圈的邊界
任何人都可以幫助我解決這個問題嗎? 如何檢測圓的邊界?
這裏是我的MainWindow.xaml.cs
代碼:
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 LineCaps
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Ellipse ellipse = new Ellipse();
ellipse.Height = 20;
ellipse.Width = 20;
ellipse.Stroke = Brushes.Black;
Canvas.SetLeft(ellipse, 0);
Canvas.SetTop(ellipse, 40);
can.Children.Add(ellipse);
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;
CappedLine myPath = new CappedLine();
myPath.Stroke = Brushes.Black;
myPath.StrokeThickness = 1;
myPath.LinePath = myPathGeometry;
Geometry g= Geometry.Parse("M0,0 L6,-6 L6,6 z"); ;
myPath.BeginCap = g;
can.Children.Add(myPath);
}
}
}
,這是我的XAML代碼:
<Window x:Class="LineCaps.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:loc="clr-namespace:LineCaps"
Title="MainWindow">
<Viewbox x:Name="vb">
<Canvas x:Name="can" Height="350" Width="525">
</Canvas>
</Viewbox>
</Window>
我以前從這裏CappedLine代碼:
https://blogs.msdn.microsoft.com/mrochon/2011/01/09/custom-line-caps-in-wpf/
我有這個結果:
但我想這一個:
不能正常工作......當線位於橢圓的左側時..那麼線會到達圓的右下側 –
要做到這一點,我認爲你需要找到交點你的直線和矩形(使用直線和橢圓方程),然後從這一點開始你的直線(現在你從任意10,50點開始)。 – Evk