我有一个矩阵,每行的列数不均匀。列数随着行的增加而减少。现在在矩阵顶部绘制了一条曲线。 要求是曲线必须完全在矩阵内。
图片是曲线离开矩阵边界的矩阵。这是我必须解决的问题。
计算曲线的算法不知道矩阵的大小。
问题是:满足此要求的最佳方法是什么? 计算曲线的算法是否必须改变以了解矩阵尺寸? 或者之后我可以将曲线更改为" live"在矩阵里面? 你会推荐什么?
预期曲线如下所示: 我在构成折线的点上添加了蓝点。线上的小黑点表示基于线性插值的计算点。可以测试这些计算出的点中的每一个以确定它们是在矩阵的内部还是外部。实际矩阵有更多的列和行,但显示它并不会增加手头任务的清晰度或解决它的可能方法。
我创建了一个小演示:
的 MainWindows.xaml
<Window x:Class="InsideMatrix.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:InsideMatrix"
Title="MainWindow" Height="250" Width="525" x:Name="Main">
<Grid>
<local:Matrix/>
<Polyline Stroke="Red" StrokeThickness="3" Points="{Binding Curve, ElementName=Main}" Visibility="Visible"/>
</Grid>
</Window>
MainWindow.xaml.cs
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Media;
namespace InsideMatrix
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
private PointCollection curve;
public MainWindow()
{
InitializeComponent();
if (!DesignerProperties.GetIsInDesignMode(this))
{
this.Curve = this.CalculateCurve();
}
}
private PointCollection CalculateCurve()
{
// This is just a stub for an algorith that will calculate a curve
return new PointCollection()
{
new Point(57, 24),
new Point(114, 48),
new Point(171, 72),
new Point(228, 96),
new Point(456, 153)
};
}
public PointCollection Curve
{
get { return this.curve; }
set
{
this.curve = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Matrix.xaml
<UserControl x:Class="InsideMatrix.Matrix"
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"
xmlns:insideMatrix="clr-namespace:InsideMatrix"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
</Grid>
</UserControl>
Matrix.xaml.cs
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace InsideMatrix
{
public partial class Matrix : UserControl
{
private int width = 57;
private int height = 24;
private int[] cellsPerRow = { 7, 7, 4, 4, 2, 2 };
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
if (DesignerProperties.GetIsInDesignMode(this)) return;
Pen pen = new Pen(new SolidColorBrush(Colors.Black), 1);
int row = 0;
foreach (var cells in cellsPerRow)
{
row++;
for (int column = 7; column > 7 - cells; column--)
{
drawingContext.DrawRectangle(new SolidColorBrush(Colors.Transparent), pen, new Rect(column * width, row * height, width, height));
}
}
}
}
}