源代码下载

效果图:

本程序主要实现:

  • N阶贝塞尔曲线(通用公式)

本程序主要使用技术

  • MVVM
  • InterAction 事件绑定
  • 动态添加Canvas的Item

第一部分公式:

  • n=有效坐标点数量
  • i=坐标点的下标
  • P是坐标
  • t是时间0~1之间

有效坐标点是坐标点的数量减1

计算坐标时分开计算,x,y时分别计算两边

至于括号内上n下i是组合数

计算方法是:

换成贝塞尔的公式中的组合数是:

剩下部分应该是很简单了。

因为是求和,所以先是代入公式最后相加即可

例子(摘自百度)

3阶

2阶

现在给出代码的部分

阶乘代码:

 private int Factorial(int n)
{
if (n == )
{
return ;
}
else
{
return n * Factorial(n - );
} }

组合数公式代码:

        private int GetCA(int r, int n) => Factorial(r) / (Factorial(n) * Factorial((r - n)));

贝塞尔

  /// <summary>
/// 求单坐标贝塞尔公式
/// </summary>
/// <param name="Max">最大有效坐标点的数量</param>
/// <param name="Index">需要计算的坐标点的下标</param>
/// <param name="Time">时间0~1</param>
/// <param name="P">单个坐标值(x或者y)</param>
/// <returns></returns>
private double GetPoints(int Max, int Index, double Time, double P)
{
var C = GetCA(Max, Index);
var T1 = Math.Pow(Time, Index);
var T2 = Math.Pow(( - Time), Max - Index);
return (C * T1 * T2 * P);
}

使用方式

  private void SetPoints(Polyline polyline)
{
polyline.Points.Clear();
double ax = ;
double ay = ;
for (double t = 0.00; t < 1.01; t += 0.01)
{
for (int i = ; i < Points.Count; i++)
{
ax += GetPoints(Points.Count - , i, t, Points[i].X);
ay += GetPoints(Points.Count - , i, t, Points[i].Y);
}
//此处的ax ay为t时,i下标的有效Points[i].X 坐标点 和Points[i].Y坐标点的Points.Count - 1阶贝塞尔曲线
polyline.Points.Add(new Point(ax, ay));
ax = ;
ay = ;
}
SetText(polyline.Points);
}

第二部分 MVVM的事件绑定

MVVM事件绑定需要使用

 System.Windwos.Interactiivity.dll

一般来说使用NuGet搜索Expression.Blend.Sdk.WPF就可以了

使用方式为

先创建实现 TriggerAction<DependencyObject>接口类

 public class EventCommand : TriggerAction<DependencyObject>
{
public static readonly DependencyProperty CommandProperty =
DependencyProperty.Register("Command", typeof(ICommand), typeof(EventCommand),
null); public static readonly DependencyProperty CommandParameterProperty =
DependencyProperty.Register("CommandParameter", typeof(object), typeof(EventCommand),
null); public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
} public object CommandParameter
{
get { return GetValue(CommandParameterProperty); }
set { SetValue(CommandParameterProperty, value); }
} protected override void Invoke(object parameter)
{
if (this.AssociatedObject != null)
{
ICommand command = this.Command;
if (command != null)
{
if (this.CommandParameter != null)
{
if (command.CanExecute(this.CommandParameter))
{
command.Execute(this.CommandParameter);
}
}
else
{
if (command.CanExecute(parameter))
{
command.Execute(new Tuple<object, object>(this.AssociatedObject, parameter));
}
}
}
}
} }

其次是在XAML页面添加引用

分别为

  <!--此处时引用interactivity的dll-->
xmlns:event="http://schemas.microsoft.com/expression/2010/interactivity"
<!--此处时引用刚才实现的接口类-->
xmlns:Action="clr-namespace:MVVM_贝塞尔曲线任意点实现.Command"

使用方式:

 <ItemsControl  Grid.ColumnSpan="2"  ItemsSource="{Binding UI}">
<event:Interaction.Triggers>
<!--此处EventName为事件标准名称-->
<event:EventTrigger EventName="MouseLeftButtonUp">
<Action:EventCommand Command="{Binding MouseLeftButtonUpCommand}"/>
</event:EventTrigger>
</event:Interaction.Triggers>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Background="Transparent"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Canvas.Left" Value="{Binding X}"/>
<Setter Property="Canvas.Top" Value="{Binding Y}"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ContentControl Content="{Binding UI}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

VIewMolde部分则是正常使用即可

剩余部分

可以看看源代码

贝塞尔曲线 WPF MVVM N阶实现 公式详解+源代码下载的更多相关文章

  1. 热门前沿知识相关面试问题-MVC/MVP/MVVM架构设计模式面试问题详解

    MVC[最常用]: MVC的定义:M:业务逻辑处理.[业务MODEL]V:处理数据显示的部分.[如xml布局文件]C:Activity处理用户交互的问题.[也就是Activity在MVC中扮演着C的角 ...

  2. N阶台阶问题(详解)

    原创 问题描述: 有N阶台阶,每一步可以走1步台阶或者2步台阶,求出走到第N阶台阶的方法数. 解题思路: 类似于建立树的过程 1 2 1 2   1 2  1        2      1    2 ...

  3. WPF中的DependencyProperty存储方式详解

    前言 接触WPF有一段时间了,之前虽然也经常使用,但是对于DependencyProperty一直处于一知半解的状态.今天花了整整一下午将这个概念梳理了一下,自觉对这个概念有了较为清晰的认识,之前很多 ...

  4. WPF读书笔记 x名称空间详解(第二天)

    每天看一点,每天进步一点. x名称空间映射的是http://schemas.microsoft.com/winfx/2006/xaml,它包含的类均与解析XAML语言关,亦可称为"XAML名 ...

  5. [No000013F]WPF学习之X名称空间详解

    X名称空间里面的成员(如X:Name,X:Class)都是写给XAML编译器看的.用来引导XAML代码将XAML代码编译为CLR代码. 4.1X名称空间里面到底都有些什么? x名称空间映射的是:htt ...

  6. WPF学习之X名称空间详解

    X名称空间里面的成员(如X:Name,X:Class)都是写给XAML编译器看的.用来引导XAML代码将XAML代码编译为CLR代码. 4.1X名称空间里面到底都有些什么? x名称空间映射的是:htt ...

  7. WPF自定义选择年月控件详解

    本文实例为大家分享了WPF自定义选择年月控件的具体代码,供大家参考,具体内容如下 封装了一个选择年月的控件,XAML代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...

  8. Lucene TFIDFSimilarity评分公式详解

    版权声明:本文为博主原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/zteny/article/details/ ...

  9. React 高阶组价详解

    这个教程还是不错的...

随机推荐

  1. shell 中可以for 循环的时间加减日期格式

    ..};do # LAST_HOUR=`date -d '-${num} hour' +%H` 不可for循环,报格式错误 LAST_HOUR=`date "+%H" -d -${ ...

  2. Set与List之间转化

    List list = new ArrayList(set);Set set = new HashSet(list); //但是有一点,转换当中可能要丢失数据,尤其是从list转换到set的时候,因为 ...

  3. kubernetes role

    https://kubernetes.io/docs/admin/authorization/rbac/

  4. centos7 安装docker-ce ,最新版本docker,docker阿里云加速

    直接用yum install docker -y安装的docker版本为1.12,但是docker发展很快,现在都17.06.2了.docker-ce是指docker的社区版 卸载老版本的 docke ...

  5. linux安装mysql服务分两种安装方法:

    linux安装mysql服务分两种安装方法: ①源码安装,优点是安装包比较小,只有十多M,缺点是安装依赖的库多,安装编译时间长,安装步骤复杂容易出错: ②使用官方编译好的二进制文件安装,优点是安装速度 ...

  6. Kafka介绍及集群搭建

    简介 Kafka是一个开源的,分布式的,高吞吐量的消息系统.随着Kafka的版本迭代,日趋成熟.大家对它的使用也逐步从日志系统衍生到其他关键业务领域.特别是其超高吞吐量的特性,在互联网领域,使用越来越 ...

  7. SpringBoot整合SpringData和Mysql数据库

    1.新建maven项目(具体的新建过程就不细说了) 2.添加maven依赖,也就是在pom.xml文件添加项目的依赖jar包: <project xmlns="http://maven ...

  8. PHP异常处理详解

      PHP异常处理详解 异常处理(又称为错误处理)功能提供了处理程序运行时出现的错误或异常情况的方法. 异常处理通常是防止未知错误产生所采取的处理措施.异常处理的好处是你不用再绞尽脑汁去考虑各种错误, ...

  9. 695. Max Area of Island最大岛屿面积

    [抄题]: 求最多的联通的1的数量 Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (repre ...

  10. chrome crx插件存档

    https://github.com/mdamien/chrome-extensions-archive