WPF 绘制过顶点的圆滑曲线(样条,贝塞尔)

server/2024/12/29 10:14:30/

项目中要用到样条曲线,必须过顶点,圆滑后还不能太走样,捣鼓一番,发现里面颇有玄机,于是把我多方抄来改造的方法发出来,方便新手:

如上图,看代码吧:

----------------------------------------

前台页面:

<Window x:Class="Wpf_north_demo.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:Wpf_north_demo"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><Canvas x:Name="ca1" Background="White" MouseLeftButtonDown="ca1_MouseLeftButtonDown" MouseMove="ca1_MouseMove" MouseRightButtonDown="ca1_MouseRightButtonDown"><Polyline x:Name="path_lines" Stroke="Silver" StrokeThickness="1" StrokeDashArray="1 1 1" IsHitTestVisible="False"></Polyline><Path x:Name="path1" Stroke="Red" StrokeThickness="1" IsHitTestVisible="False"><Path.Data><PathGeometry x:Name="pathGeometry1"></PathGeometry></Path.Data></Path></Canvas><Canvas x:Name="ca_top" IsHitTestVisible="False"/><TextBlock  HorizontalAlignment="Left" VerticalAlignment="Top" Margin="5" Text="左键绘制,右键结束" IsHitTestVisible="False"/></Grid>
</Window>

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 Wpf_north_demo
{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();}int _num = 0;bool _started = false;List<Point> _seed = new List<Point>();private void ca1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e){if(!_started){_num = 0;_seed.Clear();_started = true;ca_top.Children.Clear();path_lines.Points.Clear();pathGeometry1.Figures.Clear();}while (path_lines.Points.Count > _num && _num > 0){path_lines.Points.RemoveAt(path_lines.Points.Count - 1);}_seed.Add(e.GetPosition(ca1));_num = _seed.Count;path_lines.Points.Add(_seed[_num - 1]);ca_top.Children.Add(new Ellipse{Width = 6,Height = 6,Stroke = Brushes.Blue,Fill = Brushes.Lime,Margin = new Thickness(_seed[_num - 1].X - 3, _seed[_num - 1].Y - 3, 0, 0)});}private void ca1_MouseMove(object sender, MouseEventArgs e){if (_started && e.LeftButton == MouseButtonState.Released && _num > 0){while (path_lines.Points.Count > _num){path_lines.Points.RemoveAt(path_lines.Points.Count - 1);}path_lines.Points.Add(e.GetPosition(ca1));}}private void ca1_MouseRightButtonDown(object sender, MouseButtonEventArgs e){if(_started){while (path_lines.Points.Count > _num && _num > 0){path_lines.Points.RemoveAt(path_lines.Points.Count - 1);}_seed.Add(e.GetPosition(ca1));_num = _seed.Count;path_lines.Points.Add(_seed[_num - 1]);ca_top.Children.Add(new Ellipse{Width = 6,Height = 6,Stroke = Brushes.Blue,Fill = Brushes.Lime,Margin = new Thickness(_seed[_num - 1].X - 3, _seed[_num - 1].Y - 3, 0, 0)});BezierHelper.DrawBezierPolyline(pathGeometry1, _seed, false);}else{_num = 0;_seed.Clear();ca_top.Children.Clear();path_lines.Points.Clear();pathGeometry1.Figures.Clear();}_started = false;}}public class BezierHelper{public static void DrawBezierPolyline(PathGeometry geo, List<Point> list, bool close){geo.Figures.Clear();if (list.Count > 0){PathFigure pf = new PathFigure() { IsClosed = close };pf.StartPoint = list[0];List<Point> controls = new List<Point>();for (int i = 0; i < list.Count; i++){Point control_01, control_02;GetControlPoint(list, i, out control_01, out control_02);controls.Add(control_01);controls.Add(control_02);}for (int i = 1; i < list.Count; i++){BezierSegment bs = new BezierSegment(controls[i * 2 - 1], controls[i * 2], list[i], true);bs.IsSmoothJoin = true;pf.Segments.Add(bs);}geo.Figures.Add(pf);}}static void GetControlPoint(List<Point> list, int idx, out Point control_01, out Point control_02){if (idx == 0){control_01 = list[0];}else{control_01 = GetAverage(list[idx - 1], list[idx]);}if (idx == list.Count - 1){control_02 = list[list.Count - 1];}else{control_02 = GetAverage(list[idx], list[idx + 1]);}Point ave = GetAverage(control_01, control_02);Point sh = Sub(list[idx], ave);control_01 = Mul(Add(control_01, sh), list[idx], 0.6);control_02 = Mul(Add(control_02, sh), list[idx], 0.6);}static Point GetAverage(Point x, Point y){return new Point((x.X + y.X) / 2, (x.Y + y.Y) / 2);}static Point Add(Point x, Point y){return new Point(x.X + y.X, x.Y + y.Y);}static Point Sub(Point x, Point y){return new Point(x.X - y.X, x.Y - y.Y);}static Point Mul(Point x, Point y, double d){Point temp = Sub(x, y);temp = new Point(temp.X * d, temp.Y * d);temp = Add(y, temp);return temp;}}}


http://www.ppmy.cn/server/154168.html

相关文章

3.基于 Temporal 的 Couchbase 动态 SQL 执行场景

在使用 Temporal 和 Go 语言 调用 Couchbase 执行 SQL 脚本时&#xff0c;可以通过动态参数传递到 SQL 脚本中&#xff0c;以下是完整实现的指南&#xff1a; 1. Temporal Workflow 的参数传递 Temporal 的 Workflow 支持接收动态参数&#xff0c;将这些参数传递给执行 SQL 的…

Hadoop中MapReduce过程中Shuffle过程实现自定义排序

文章目录 Hadoop中MapReduce过程中Shuffle过程实现自定义排序一、引言二、实现WritableComparable接口1、自定义Key类 三、使用Job.setSortComparatorClass方法2、设置自定义排序器3、自定义排序器类 四、使用示例五、总结 Hadoop中MapReduce过程中Shuffle过程实现自定义排序 一…

服务器如何划分空间?

服务器如何划分空间&#xff1f;服务器是存储和处理数据的核心&#xff0c;如何有效地划分服务器空间则直接关系到资源的利用效率和系统的性能。无论是大型企业的数据中心&#xff0c;还是小型网站的共享主机&#xff0c;合理的空间划分都至关重要。下面是聚名网关于服务器如何…

丢失的MD5

丢失的MD5 源代码&#xff1a; import hashlib for i in range(32,127):for j in range(32,127):for k in range(32,127):mhashlib.md5()m.update(TASCchr(i)O3RJMVchr(j)WDJKXchr(k)ZM)desm.hexdigest()if e9032 in des and da in des and 911513 in des:print des 发现给…

Qt之数据库使用(十四)

Qt开发 系列文章 - QSqlDatabase-SQLite&#xff08;十四&#xff09; 目录 前言 一、SQLite 二、SQLite使用 1.添加SQL 2.创建数据库 3.定义类及相关变量 4.相关功能函数 5.用户类定义 6.用户类使用 7.效果演示 8.SQLite数据库 总结 前言 ‌Qt支持的数据库包括SQ…

如何在 Spring Boot 微服务中设置和管理多个数据库

在现代微服务架构中&#xff0c;通常需要与多个数据库交互的服务。这可能是由于各种原因&#xff0c;例如遗留系统集成、不同类型的数据存储需求&#xff0c;或者仅仅是为了优化性能。Spring Boot 具有灵活的配置和强大的数据访问库&#xff0c;可以轻松配置多个数据库。在本综…

Linux top指令

top指令概述 top 是 Linux 系统中用于实时监控系统性能和进程信息的命令&#xff0c;功能强大且灵活。它提供了系统资源的动态视图&#xff0c;包括 CPU、内存、运行中的进程等。 这个指令可以说是Linux中最基本的工具了&#xff0c;用来监视系统的实时运行状态&#xff0c;类…

算法

探索算法世界&#xff1a;从基础到前沿 一、引言 算法是计算机科学的核心&#xff0c;它为解决各种问题提供了明确的步骤和方法。无论是数据处理、人工智能还是日常软件应用&#xff0c;算法都起着关键作用。 二、基础算法 排序算法 排序算法是最常见的算法之一。例如冒泡排序&…