当前位置:网站首页>C # draw Bezier curve with control points for lattice images and vector graphics
C # draw Bezier curve with control points for lattice images and vector graphics
2022-07-05 09:07:00 【Dakeshan people】
【 Abstract 】 Without the help of a third party , Use c# + GDI+ Conduct SVG Equal drawing , Draw with control points Bezier curve . It can be used for dot matrix images and vector graphics ( Such as SVG) mapping .
Look at the effect :
( I don't know why , Pictures have been uploaded twice , Unable to display , For help csdn)
Figure note : Use method 2 to draw .
Method 1 :
/// <summary>
/// Bezier Splines
/// </summary>
public static class BezierSpline
{
/// <summary>
/// Get open-ended Bezier Spline Control Points.
/// </summary>
/// <param name="knots">Input Knot Bezier spline points.</param>
/// <param name="firstControlPoints">Output First Control points
/// array of knots.Length - 1 length.</param>
/// <param name="secondControlPoints">Output Second Control points
/// array of knots.Length - 1 length.</param>
/// <exception cref="ArgumentNullException"><paramref name="knots"/>
/// parameter must be not null.</exception>
/// <exception cref="ArgumentException"><paramref name="knots"/>
/// array must contain at least two points.</exception>
public static void GetCurveControlPoints(Point[] knots,
out Point[] firstControlPoints, out Point[] secondControlPoints)
{
if (knots == null)
throw new ArgumentNullException("knots");
int n = knots.Length - 1;
if (n < 1)
throw new ArgumentException
("At least two knot points required", "knots");
if (n == 1)
{ // Special case: Bezier curve should be a straight line.
firstControlPoints = new Point[1];
// 3P1 = 2P0 + P3
firstControlPoints[0].X = (2 * knots[0].X + knots[1].X) / 3;
firstControlPoints[0].Y = (2 * knots[0].Y + knots[1].Y) / 3;
secondControlPoints = new Point[1];
// P2 = 2P1 – P0
secondControlPoints[0].X = 2 *
firstControlPoints[0].X - knots[0].X;
secondControlPoints[0].Y = 2 *
firstControlPoints[0].Y - knots[0].Y;
return;
}
// Calculate first Bezier control points
// Right hand side vector
double[] rhs = new double[n];
// Set right hand side X values
for (int i = 1; i < n - 1; ++i)
rhs[i] = 4 * knots[i].X + 2 * knots[i + 1].X;
rhs[0] = knots[0].X + 2 * knots[1].X;
rhs[n - 1] = (8 * knots[n - 1].X + knots[n].X) / 2.0;
// Get first control points X-values
double[] x = GetFirstControlPoints(rhs);
// Set right hand side Y values
for (int i = 1; i < n - 1; ++i)
rhs[i] = 4 * knots[i].Y + 2 * knots[i + 1].Y;
rhs[0] = knots[0].Y + 2 * knots[1].Y;
rhs[n - 1] = (8 * knots[n - 1].Y + knots[n].Y) / 2.0;
// Get first control points Y-values
double[] y = GetFirstControlPoints(rhs);
// Fill output arrays.
firstControlPoints = new Point[n];
secondControlPoints = new Point[n];
for (int i = 0; i < n; ++i)
{
// First control point
firstControlPoints[i] = new Point(x[i], y[i]);
// Second control point
if (i < n - 1)
secondControlPoints[i] = new Point(2 * knots
[i + 1].X - x[i + 1], 2 *
knots[i + 1].Y - y[i + 1]);
else
secondControlPoints[i] = new Point((knots
[n].X + x[n - 1]) / 2,
(knots[n].Y + y[n - 1]) / 2);
}
}
/// <summary>
/// Solves a tridiagonal system for one of coordinates (x or y)
/// of first Bezier control points.
/// </summary>
/// <param name="rhs">Right hand side vector.</param>
/// <returns>Solution vector.</returns>
private static double[] GetFirstControlPoints(double[] rhs)
{
int n = rhs.Length;
double[] x = new double[n]; // Solution vector.
double[] tmp = new double[n]; // Temp workspace.
double b = 2.0;
x[0] = rhs[0] / b;
for (int i = 1; i < n; i++) // Decomposition and forward substitution.
{
tmp[i] = 1 / b;
b = (i < n - 1 ? 4.0 : 3.5) - tmp[i];
x[i] = (rhs[i] - x[i - 1]) / b;
}
for (int i = 1; i < n; i++)
x[n - i - 1] -= tmp[n - i] * x[n - i]; // Backsubstitution.
return x;
}
}
Method 2 :
private void DrawCurve(Graphics g, PointF[] points, float tension)
{
int n=points.Length;
Pen rPen = new Pen(Color.Red, 2f);
Pen blPen= new Pen(Color.Blue, 1f);
Pen bzPen = new Pen(Color.DarkGoldenrod, 2f);
for (int i = 0; i < n; ++i)
{
// draw segment points[i] - points[(i + 1) % n]
var pPrev1 = points[(i - 1 + n) % n];
var p1 = points[i];
var p2 = points[(i + 1) % n];
var pAfter2 = points[(i + 2) % n];
// tangents Tangent control points
var t1 = new PointF(tension * (p2.X - pPrev1.X), tension * (p2.Y - pPrev1.Y));
var t2 = new PointF(tension * (pAfter2.X - p1.X), tension * (pAfter2.Y - p1.Y));
// interior Bezier control points
var c1 = new PointF(p1.X + t1.X / 3.0f, p1.Y + t1.Y / 3.0f);
var c2 = new PointF(p2.X - t2.X / 3.0f, p2.Y - t2.Y / 3.0f);
// Draw Bezier curve
g.DrawBezier(bzPen, p1, c1, c2, p2);
// Draw a straight line from the key point to the tangent control point
g.DrawLine(blPen, p1, c1);
g.DrawEllipse(rPen, p1.X - 2, p1.Y - 2, 4, 4);
g.DrawEllipse(rPen, c1.X - 2, c1.Y - 2, 4, 4);
g.DrawLine(blPen, p2, c2);
g.DrawEllipse(rPen, p2.X - 2, p2.Y - 2, 4, 4);
g.DrawEllipse(rPen, c2.X - 2, c2.Y - 2, 4, 4);
g.FillEllipse(new SolidBrush(Color.Green), new RectangleF(p1.X-2, p1.Y-2, 4, 4));
}
}
Calling method of method 2 :
// What we use here is Panel Draw on , Other controls ( Such as PictureBox) It's the same thing .
Graphics g = pnlWorkArea.CreateGraphics();
g.Clear(Color.White);
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
PointF[] points = { new PointF(568,200),new PointF(168,110),new PointF(60,186),new PointF(300,191),new PointF(600,300),new PointF(800,431),new PointF(300,650), new PointF(568, 200) };
float tension=0.68f;
DrawCurve(g, points, tension);
Here is a good link :
C# GraphicsPath AddBeziers(params System.Drawing.Point[] points)
C# GraphicsPath AddBeziers(System.Drawing.PointF[] points)
The source code can also be downloaded here :C# Bessel with control points Bezier Curve algorithm ( Source code )-C# Document resources -CSDN download
边栏推荐
- Ros-10 roslaunch summary
- Wechat H5 official account to get openid climbing account
- Solution to the problem of the 10th Programming Competition (synchronized competition) of Harbin University of technology "Colin Minglun Cup"
- Ministry of transport and Ministry of Education: widely carry out water traffic safety publicity and drowning prevention safety reminders
- 2011-11-21 training record personal training (III)
- C#图像差异对比:图像相减(指针法、高速)
- What is a firewall? Explanation of basic knowledge of firewall
- Luo Gu p3177 tree coloring [deeply understand the cycle sequence of knapsack on tree]
- kubeadm系列-01-preflight究竟有多少check
- 什么是防火墙?防火墙基础知识讲解
猜你喜欢

TF coordinate transformation of common components of ros-9 ROS

ROS learning 4 custom message

C#【必备技能篇】ConfigurationManager 类的使用(文件App.config的使用)
![Introduction Guide to stereo vision (5): dual camera calibration [no more collection, I charge ~]](/img/68/6bfa390b0bedcdbc4afba2f9bd9c0f.jpg)
Introduction Guide to stereo vision (5): dual camera calibration [no more collection, I charge ~]

Codeworks round 639 (Div. 2) cute new problem solution

生成对抗网络

Count of C # LINQ source code analysis

Generate confrontation network
![[Niuke brush questions day4] jz55 depth of binary tree](/img/f7/ca8ad43b8d9bf13df949b2f00f6d6c.png)
[Niuke brush questions day4] jz55 depth of binary tree

Confusing basic concepts member variables local variables global variables
随机推荐
asp.net(c#)的货币格式化
Introduction Guide to stereo vision (3): Zhang calibration method of camera calibration [ultra detailed and worthy of collection]
图解网络:什么是网关负载均衡协议GLBP?
Introduction Guide to stereo vision (6): level constraints and polar correction of fusiello method
[daiy4] copy of JZ35 complex linked list
The location search property gets the login user name
阿里云发送短信验证码
How many checks does kubedm series-01-preflight have
Introduction Guide to stereo vision (1): coordinate system and camera parameters
ROS learning 4 custom message
Halcon Chinese character recognition
It cold knowledge (updating ing~)
我从技术到产品经理的几点体会
Blogger article navigation (classified, real-time update, permanent top)
Editor use of VI and VIM
2011-11-21 training record personal training (III)
MPSoC QSPI Flash 升级办法
np. allclose
The combination of deep learning model and wet experiment is expected to be used for metabolic flux analysis
C#【必备技能篇】ConfigurationManager 类的使用(文件App.config的使用)