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

news/2024/12/28 14:31:54/

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

如上图,看代码吧:

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

前台页面:

<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/news/1558812.html

相关文章

wsl安装ubuntu 18.04

wsl安装ubuntu 18.04 开启wsl bios开启hyper-v服务与功能里开启wsl重启Windows wsl更新 由于当前wsl版本安装ubuntu会报错&#xff0c;因此升级wsl到最新版。 首先使用在线升级wsl。 wsl --update但由于网络问题&#xff0c;一直会报错&#xff0c;因此改用安装包离线安装…

while 循环

应用&#xff1a;循环轮播图 最基础、最核心 while循环 规划好条件 打印1-100 上面代码没有if break中断循环 break只结束距离自己最近的循环 输出结果1-49 打印不了50 Continue中止本次循环 提前开始下一次循环 输出结果&#xff1a;1-101&#xff08;没有50&#xff09; 有1…

Amazon Bedrock 实践 - 利用 Llama 3.2 模型分析全球糖尿病趋势

黄浩文 资深开发者布道师 亚马逊云科技 拥有电信、互联网以及云计算等行业超过 20 年的丰富经验&#xff0c;曾任职于微软、Sun 和中国电信。他目前专注于生成式 AI、大型语言模型 (LLM)、机器学习和数据科学等领域的技术内容创作和实践分享&#xff0c;致力于赋能全球开发者。…

2.阿里云flinkselectdb-jar作业

1.概述 本文继续介绍使用阿里云实时计算flink把数据从自建mysql同步到阿里云selectdb的过程。上一节使用sql作业&#xff0c;不够强大&#xff0c;有如下问题: 不支持自动创建结果表(selectdb表)。同步前需要手动在selectdb创建结果表&#xff1b;不支持源表(mysql表)的ddl语…

CI/CD是什么?

CI/CD 定义 CI/CD 代表持续集成和持续部署&#xff08;或持续交付&#xff09;。它是一套实践和工具&#xff0c;旨在通过自动化构建、测试和部署来改进软件开发流程&#xff0c;使您能够更快、更可靠地交付代码更改。 持续集成 (CI)&#xff1a;在共享存储库中自动构建、测试…

面试经典题目:LeetCode134_加油站

leetcode134_加油站 暴力解法贪心算法初始条件第一个不等式第二个不等式考虑任意加油站 z z z推导过程结论 题目链接&#xff1a;leetcode134_加油站 暴力解法 暴力解法会尝试每个加油站作为起点&#xff0c;模拟一圈行驶来验证是否能成功环绕一周。这种方法的时间复杂度是 O…

微信小程序 不同角色进入不同页面、呈现不同底部导航栏

遇到这个需求之前一直使用的小程序默认底部导航栏&#xff0c;且小程序默认入口页面为pages/index/index&#xff0c;要使不同角色呈现不同底部导航栏&#xff0c;必须要在不同页面引用不同的自定义导航栏。本篇将结合分包&#xff08;subPackages&#xff09;展开以下三步叙述…

Android Studio Gradle Sync timeout

使用Android Studio开发Android应用程序时&#xff0c;Gradle sync timeout常用的处理方法有&#xff1a; 增加超时设置&#xff1a;在 gradle.properties 文件中增加超时设置&#xff0c;给 Gradle 更长时间完成同步使用国内镜像&#xff1a;通过配置阿里云、华为或腾讯云的 …