基本信息
源码名称:C# 绘制贝塞尔曲线 示例源码
源码大小:0.02M
文件格式:.7z
开发语言:C#
更新时间:2016-12-18
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 5 元×
微信扫码支付:5 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DrawBezier
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
MyGraphic = this.CreateGraphics();
}
int LinesCount = 0;
Graphics MyGraphic;
public PointF GetPoint(PointF[] MyPoints, double t)
{
LinesCount ;
int PCount = MyPoints.Length;
PointF[] MyNewPoints = new PointF[PCount - 1];
double Anothert = 1.0 - t;
for (int i = 0; i < PCount - 1; i )
{
MyNewPoints[i] = Add(Mutiply(Anothert, MyPoints[i]), Mutiply(t, MyPoints[i 1]));
}
if (MyNewPoints.Length > 1)
{
return (GetPoint(MyNewPoints, t));
}
else
{
LinesCount = 0;
return MyNewPoints[0];
}
}
/// <summary>
/// double型数与点相乘生成新点
/// </summary>
/// <param name="t"></param>
/// <param name="MyPoint"></param>
/// <returns></returns>
public static PointF Mutiply(double t, PointF MyPoint)
{
return (new PointF((float)(t * MyPoint.X), (float)(t * MyPoint.Y)));
}
/// <summary>
/// 实现点的相加,生成新点
/// </summary>
/// <param name="LeftP"></param>
/// <param name="RightP"></param>
/// <returns></returns>
public static PointF Add(PointF LeftP, PointF RightP)
{
return (new PointF((LeftP.X RightP.X), LeftP.Y RightP.Y));
}
private PointF[] MyBezierpoints = new PointF[] { new PointF(0, 0), new PointF(10, 20), new PointF(100, 10) };
/// <summary>
/// 绘制Bezier曲线
/// </summary>
/// <param name="MyDrawingPen"></param>
/// <param name="Points"></param>
private void DrawMyBezier(Pen MyDrawingPen, PointF[] Points)
{
Refresh();
//int pointcount = AllMyPoints.Count;
int PointCount = Points.Length;
if (PointCount < 2)
{
throw new Exception("点数不够!");
}
for (int i = 0; i < Points.Length; i )
{
//RectangleF MyRectangleF = new RectangleF(MyBezierpoints[i], new SizeF((float)5, (float)5));
RectangleF MyRectangleF = new RectangleF(Points[i], new SizeF((float)5, (float)5));
MyGraphic.DrawEllipse(new Pen(Color.DarkRed), MyRectangleF);
MyDrawingPen = new Pen(Color.Blue);
//MyGraphic.DrawLines(MyDrawingPen, MyBezierpoints);
MyGraphic.DrawLines(MyDrawingPen, Points);
}
PointF LeftPoint = Points[0];
PointF RightPoint = new PointF();
double t = 0.0;
double tAdd = 1.0 / (100 * PointCount);
MyGraphic = this.CreateGraphics();
int Result;
bool SelectResult = int.TryParse("2", out Result);
if (!SelectResult)
{
MyDrawingPen.Width = 1;
}
else
{
MyDrawingPen.Width = Result;
}
for (int i = 0; i < (100 * PointCount); i )
{
RightPoint = GetPoint(Points, t);
MyGraphic.DrawLine(MyDrawingPen, LeftPoint, RightPoint);
LeftPoint = RightPoint;
t = tAdd;
}
}
private void Draw_Click(object sender, EventArgs e)
{
DrawMyBezier(new Pen(Color.Black), MyBezierpoints);
}
}
}