C#,计算几何,鼠标点击绘制 (二维,三次)B样条曲线的代码

B样条(B-Spline)是常用的曲线拟合与插值算法之一。

这里给出在 Form 的 图像 Picturebox 组件上,按鼠标点击点绘制 (三次)B样条曲线的代码。

2022-12-05 修改了代码。

1 文本格式

using System;
using System.Data;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Legalsoft.Truffer.Algorithm;
{
    public partial class Form1 : Form
    {
        public double[] splinex = new double[1001];
        public double[] spliney = new double[1001];
        public point[] pt = new point[6];
        public int no_of_points = 0;

        /// <summary>
        /// Prints a dot at the place whrere the mouseup event occurs
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            Graphics g = pictureBox1.CreateGraphics();
            Color cl = Color.DarkBlue;
            g.DrawLine(new Pen(cl, 2), e.X-3, e.Y, e.X + 3, e.Y);
            g.DrawLine(new Pen(cl, 2), e.X, e.Y-3, e.X, e.Y + 3);
            g.DrawString(no_of_points+"", new Font("宋体",12), new SolidBrush(Color.Red), e.X, e.Y);

        }

        /// <summary>
        /// At each mousedown event the the no of points is calculated
        /// if the value is more than 3 then the curve is drawn
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
           if (no_of_points > 3)
            {
                pt[0] = pt[1];
                pt[1] = pt[2];
                pt[2] = pt[3];
                pt[3].X = e.X;
                pt[3].Y = e.Y;

                double temp = Math.Sqrt(Math.Pow(pt[2].X - pt[1].X, 2F) + Math.Pow(pt[2].Y - pt[1].Y, 2F));
                int interpol = System.Convert.ToInt32(temp);
                bspline(pt[0], pt[1], pt[2], pt[3], interpol);

                int width = 2;
                Graphics g = pictureBox1.CreateGraphics();
                Color cl = Color.Blue;
                int x, y;
                for (int i = 0; i <= interpol - 1; i++)
                {
                    x = System.Convert.ToInt32(splinex[i]);
                    y = System.Convert.ToInt32(spliney[i]);
                    g.DrawLine(new Pen(cl, width), x - 1, y, x + 1, y);
                    g.DrawLine(new Pen(cl, width), x, y - 1, x, y + 1);
                }
            }
            else
            {
                pt[no_of_points].X = e.X;
                pt[no_of_points].Y = e.Y;
            }
            no_of_points++;
        }

        /// <summary>
        /// calculating the values using the algorithm 
        /// </summary>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <param name="p3"></param>
        /// <param name="p4"></param>
        /// <param name="divisions"></param>
        public void bspline(point p1, point p2, point p3, point p4, int divisions)
        {
            double[] a = new double[5];
            double[] b = new double[5];
            a[0] = (-p1.X + 3 * p2.X - 3 * p3.X + p4.X) / 6.0;
            a[1] = (3 * p1.X - 6 * p2.X + 3 * p3.X) / 6.0;
            a[2] = (-3 * p1.X + 3 * p3.X) / 6.0;
            a[3] = (p1.X + 4 * p2.X + p3.X) / 6.0;
            b[0] = (-p1.Y + 3 * p2.Y - 3 * p3.Y + p4.Y) / 6.0;
            b[1] = (3 * p1.Y - 6 * p2.Y + 3 * p3.Y) / 6.0;
            b[2] = (-3 * p1.Y + 3 * p3.Y) / 6.0;
            b[3] = (p1.Y + 4 * p2.Y + p3.Y) / 6.0;

            splinex[0] = a[3];
            spliney[0] = b[3];

            for (int i = 1; i <= divisions - 1; i++)
            {
                double t = System.Convert.ToSingle(i) / System.Convert.ToSingle(divisions);
                splinex[i] = (a[2] + t * (a[1] + t * a[0])) * t + a[3];
                spliney[i] = (b[2] + t * (b[1] + t * b[0])) * t + b[3];
            }
        }
    }
}
 

2 代码格式

using System;
using System.Data;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Legalsoft.Truffer.Algorithm;
{
    public partial class Form1 : Form
    {
        public double[] splinex = new double[1001];
        public double[] spliney = new double[1001];
        public point[] pt = new point[6];
        public int no_of_points = 0;

        /// <summary>
        /// Prints a dot at the place whrere the mouseup event occurs
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            Graphics g = pictureBox1.CreateGraphics();
            Color cl = Color.DarkBlue;
            g.DrawLine(new Pen(cl, 2), e.X-3, e.Y, e.X + 3, e.Y);
            g.DrawLine(new Pen(cl, 2), e.X, e.Y-3, e.X, e.Y + 3);
            g.DrawString(no_of_points+"", new Font("宋体",12), new SolidBrush(Color.Red), e.X, e.Y);

        }

        /// <summary>
        /// At each mousedown event the the no of points is calculated
        /// if the value is more than 3 then the curve is drawn
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
           if (no_of_points > 3)
            {
                pt[0] = pt[1];
                pt[1] = pt[2];
                pt[2] = pt[3];
                pt[3].X = e.X;
                pt[3].Y = e.Y;

                double temp = Math.Sqrt(Math.Pow(pt[2].X - pt[1].X, 2F) + Math.Pow(pt[2].Y - pt[1].Y, 2F));
                int interpol = System.Convert.ToInt32(temp);
                bspline(pt[0], pt[1], pt[2], pt[3], interpol);

                int width = 2;
                Graphics g = pictureBox1.CreateGraphics();
                Color cl = Color.Blue;
                int x, y;
                for (int i = 0; i <= interpol - 1; i++)
                {
                    x = System.Convert.ToInt32(splinex[i]);
                    y = System.Convert.ToInt32(spliney[i]);
                    g.DrawLine(new Pen(cl, width), x - 1, y, x + 1, y);
                    g.DrawLine(new Pen(cl, width), x, y - 1, x, y + 1);
                }
            }
            else
            {
                pt[no_of_points].X = e.X;
                pt[no_of_points].Y = e.Y;
            }
            no_of_points++;
        }

        /// <summary>
        /// calculating the values using the algorithm 
        /// </summary>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <param name="p3"></param>
        /// <param name="p4"></param>
        /// <param name="divisions"></param>
        public void bspline(point p1, point p2, point p3, point p4, int divisions)
        {
            double[] a = new double[5];
            double[] b = new double[5];
            a[0] = (-p1.X + 3 * p2.X - 3 * p3.X + p4.X) / 6.0;
            a[1] = (3 * p1.X - 6 * p2.X + 3 * p3.X) / 6.0;
            a[2] = (-3 * p1.X + 3 * p3.X) / 6.0;
            a[3] = (p1.X + 4 * p2.X + p3.X) / 6.0;
            b[0] = (-p1.Y + 3 * p2.Y - 3 * p3.Y + p4.Y) / 6.0;
            b[1] = (3 * p1.Y - 6 * p2.Y + 3 * p3.Y) / 6.0;
            b[2] = (-3 * p1.Y + 3 * p3.Y) / 6.0;
            b[3] = (p1.Y + 4 * p2.Y + p3.Y) / 6.0;

            splinex[0] = a[3];
            spliney[0] = b[3];

            for (int i = 1; i <= divisions - 1; i++)
            {
                double t = System.Convert.ToSingle(i) / System.Convert.ToSingle(divisions);
                splinex[i] = (a[2] + t * (a[1] + t * a[0])) * t + a[3];
                spliney[i] = (b[2] + t * (b[1] + t * b[0])) * t + b[3];
            }
        }
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/343906.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

机器人制作开源方案 | 智能特殊环境清洗机器人

作者&#xff1a;达德聪 袁豪杰 杨垚 单位&#xff1a;邢台学院 指导老师&#xff1a;王承林 杨立芹 智能特殊环境清洗机器人基于STC系列单片机为核心&#xff0c;驱动摄像头模块、超声波模块、ESP8266无线模块、自动寻迹模块、舵机模块、语音识别模块&#xff0c;实现自主寻…

《WebKit 技术内幕》学习之十二(2):安全机制

2 沙箱模型 2.1 原理 一般而言&#xff0c;对于网络上的网页中的JavaScript代码和插件是不受信的&#xff08;除非是经过认证的网站&#xff09;&#xff0c;特别是一些故意设计侵入浏览器运行的主机代码更是非常危险&#xff0c;通过一些手段或者浏览器中的漏洞&#xff0c…

中仕教育:事业编招考全流程介绍

一、报名阶段 1. 了解查看招聘信息&#xff1a;查看各类事业编岗位的招聘信息&#xff0c;包括岗位职责、招聘条件、报名时间等。 2. 填写报名表&#xff1a;按照要求填写报名表&#xff0c;包括个人信息、教育背景、工作经历等内容。 3. 提交报名材料&#xff1a;将报名表及…

作物品种测试——批量获取试验站点直线距离

参考资料&#xff1a; 根据经纬度计算两地之间的距离_经纬度计算距离-CSDN博客 用于计算不同试验站点之间的距离&#xff0c;可以据此来评估各试验站点分布的合理性。 1、首选需要准备excel文件&#xff0c;用于存放各试验站点的经纬度信息。数据列内容如下&#xff1a; 2、…

[Android] Android文件系统中存储的内容有哪些?

文章目录 前言root 文件系统/system 分区稳定性:安全性: /system/bin用来提供服务的二进制可执行文件:调试工具:UNIX 命令&#xff1a;调用 Dalvik 的脚本(upall script):/system/bin中封装的app_process脚本 厂商定制的二进制可执行文件: /system/xbin/system/lib[64]/system/…

x-cmd pkg | perl - 具有强大的文本处理能力的通用脚本语言

目录 介绍首次用户技术特点竞品进一步阅读 介绍 Perl 是一种动态弱类型编程语言。Perl 内部集成了正则表达式的功能&#xff0c;以及巨大的第三方代码库 CPAN;在处理文本领域,是最有竞争力的一门编程语言之一 生态系统&#xff1a;综合 Perl 档案网络 (CPAN) 提供了超过 25,0…

记一次Flink通过Kafka写入MySQL的过程

一、前言 总体思路&#xff1a;source -->transform -->sink ,即从source获取相应的数据来源&#xff0c;然后进行数据转换&#xff0c;将数据从比较乱的格式&#xff0c;转换成我们需要的格式&#xff0c;转换处理后&#xff0c;然后进行sink功能&#xff0c;也就是将数…

记一次压测程序时的OOM分析过程

背景&#xff1a;在一个项目调优的过程中&#xff0c;丰富了一些组件后&#xff0c;再次对项目进行压测&#xff0c;发现和之前的性能差距甚大&#xff0c;并且每次运行一段时间后&#xff0c;延迟骤增&#xff0c;带宽骤降&#xff0c;查看程序日志&#xff0c;发现了 OutOfMe…

平复一下心情 愉快一下 部署一款在线图书馆

注意:国内不让随便搞线上图书馆 注意:国内不让随便搞线上图书馆 注意:国内不让随便搞线上图书馆 1安装 1.1.拉取镜像 docker pull talebook/talebook 1.2.创建目录 mkdir -p /opt/talebook 1.3.创建并启动容器 docker run -d --name talebook -p 10015:80 -v /opt/taleb…

机器学习实验2——线性回归求解加州房价问题

文章目录 &#x1f9e1;&#x1f9e1;实验内容&#x1f9e1;&#x1f9e1;&#x1f9e1;&#x1f9e1;数据预处理&#x1f9e1;&#x1f9e1;代码缺失值处理特征探索相关性分析文本数据标签编码数值型数据标准化划分数据集 &#x1f9e1;&#x1f9e1;线性回归&#x1f9e1;&am…

【大数据精讲】全量同步与CDC增量同步方案对比

目录 背景 名词解释 问题与挑战 FlinkCDC DataX 工作原理 调度流程 五、DataX 3.0六大核心优势 性能优化 背景 名词解释 CDC CDC又称变更数据捕获&#xff08;Change Data Capture&#xff09;&#xff0c;开启cdc的源表在插入INSERT、更新UPDATE和删除DELETE活动时…

AI+量化03_股票数据获取

文章目录 思维导图问答之纯小白 vs GPT4 目标: 掌握量化金融知识、使用Python进行量化开发 背景&#xff1a;纯小白 参考资料&#xff1a;https://github.com/datawhalechina/whale-quant 本章是学习了股票数据的获取&#xff1a; 理论层面&#xff1a;包括股票数据的分类和常…

深度学习-循环神经网络-RNN实现股价预测-LSTM自动生成文本

序列模型(Sequence Model) 基于文本内容及其前后信息进行预测 基于目标不同时刻状态进行预测 基于数据历史信息进行预测 序列模型:输入或者输出中包含有序列数据的模型 突出数据的前后序列关系 两大特点: 输入(输出)元素之间是具有顺序关系。不同的顺序,得到的结果应…

一种解决常用存储设备无法被电脑识别的方法

一、通用串行总线控制器描述 通用串行总线&#xff08;Universal Serial Bus&#xff0c;简称USB&#xff09;&#xff0c;是连接电脑与设备的一种序列总线标准&#xff0c;也是一种输入输出&#xff08;I/O&#xff09;连接端口的技术规范&#xff0c;广泛应用于个人电脑和移动…

PLC协议转BACnet网关BA107

随着通讯技术和控制技术的发展&#xff0c;为了实现楼宇的高效、智能化管理&#xff0c;集中监控管理已成为楼宇智能管理发展的必然趋势。在此背景下&#xff0c;高性能的楼宇暖通数据传输解决方案——协议转换网关应运而生&#xff0c;广泛应用于楼宇自控和暖通空调系统应用中…

【centos7安装docker】

背景&#xff1a; 学习docker&#xff0c;我是想做一个隔离环境&#xff0c;并且部署的话&#xff0c;希望实现自动化&#xff0c;不为安装软件而烦恼&#xff0c;保证每个人的环境一致。 2C4G内存 50G磁盘的虚拟机事先已经准备完毕。 1.查看下centos版本&#xff0c;docker要…

tvm 中的python bindings是如何与 C++ 进行交互的呢

我们知道&#xff0c;tvm 使用 python 作为前端编程语言&#xff0c;好处是 python 简单易用&#xff0c;生态强大&#xff0c;且学习成本较低。而实际的代码&#xff0c;都是 c 代码。 源码编译 tvm&#xff0c;编译完成之后&#xff0c;会在 build 目录下生成 libtvm.so 和 l…

数据挖掘笔记1

课程&#xff1a;清华大学-数据挖掘&#xff1a;理论与算法&#xff08;国家级精品课&#xff09;_哔哩哔哩_bilibili 一、Learning Resources 二、Data 数据是最底层的一种表现形式。数据具有连续性。从存储上来讲&#xff0c;数据分为逻辑上的和物理层的。大数据&#xff1…

JAVA算法—排序

目录 *冒泡排序&#xff1a; *选择排序&#xff1a; 插入排序&#xff1a; 快速排序&#xff1a; 总结&#xff1a; 以下全部以升序为例 *冒泡排序&#xff1a; 引用&#xff1a; 在完成升序排序时&#xff0c;最大的元素会经过一轮轮的遍历逐渐被交换到数列的末尾&#…

苹果眼镜(Vision Pro)的开发者指南(6)-实战应用场景开发 - 游戏、协作、空间音频、WebXR

第一部分:【构建游戏和媒体体验】 了解如何使用visionOS在游戏和媒体体验中创建真正身临其境的时刻。游戏和媒体可以利用全方位的沉浸感来讲述令人难以置信的故事,并以一种新的方式与人们联系。将向你展示可供你入门的visionOS游戏和叙事开发途径。了解如何使用RealityKit有…
最新文章