座標値の値が必要な場合など、直接ベジェ曲線の計算をして描画する方法についてはこちらの記事を参照してください。
DrawBezier()
メソッドを利用すると簡単に描画できます。using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SimpleDrawCubicBezier
{
public partial class FormDrawCubicBezier : Form
{
Point point1;
Point point2;
Point point3;
Point point4;
Pen bPen;
Brush backBrush;
public FormDrawCubicBezier()
{
InitializeComponent();
bPen = new Pen(Color.Blue, 2);
backBrush = new SolidBrush(Color.White);
}
private void button1_Click(object sender, EventArgs e)
{
int cp1x = Convert.ToInt32(textBox1.Text);
int cp1y = Convert.ToInt32(textBox2.Text);
int cp2x = Convert.ToInt32(textBox3.Text);
int cp2y = Convert.ToInt32(textBox4.Text);
point1 = new Point(0, 320);
point2 = new Point(cp1x,cp1y);
point3 = new Point(cp2x,cp2y);
point4 = new Point(320, 0);
panel1.Refresh();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.FillRectangle(backBrush, new Rectangle(0, 0, 320, 320));
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
e.Graphics.DrawBezier(bPen, point1, point2, point3, point4);
}
}
}
int cp1x = Convert.ToInt32(textBox1.Text);
int cp1y = Convert.ToInt32(textBox2.Text);
int cp2x = Convert.ToInt32(textBox3.Text);
int cp2y = Convert.ToInt32(textBox4.Text);
point1 = new Point(0, 320);
point2 = new Point(cp1x,cp1y);
point3 = new Point(cp2x,cp2y);
point4 = new Point(320, 0);
panel1.Refresh();
e.Graphics.FillRectangle(backBrush, new Rectangle(0, 0, 320, 320));
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
e.Graphics.DrawBezier(bPen, point1, point2, point3, point4);
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SimpleDrawCubicBezier
{
public partial class FormDrawCubicBezierEx : Form
{
Point point1;
Point point2;
Point point3;
Point point4;
Pen bPen;
Pen fPen;
Pen cPen;
Brush backBrush;
Brush controlpointBrush;
int offsetX, offsetY;
int areaWidth, areaHeight;
int controlpointRaduis;
public FormDrawCubicBezierEx()
{
InitializeComponent();
offsetX = 64;
offsetY = 64;
areaWidth = 320;
areaHeight = 320;
controlpointRaduis = 8;
bPen = new Pen(Color.Blue, 2);
fPen = new Pen(Color.Black, 1);
cPen = new Pen(Color.FromArgb(105,161,255), 1);
backBrush = new SolidBrush(Color.White);
controlpointBrush= new SolidBrush(Color.FromArgb(105, 161, 255));
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.FillRectangle(backBrush, new Rectangle(0, 0, panel1.Width, panel1.Height));
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
e.Graphics.DrawRectangle(fPen, new Rectangle(offsetX, offsetY, areaWidth, areaHeight));
e.Graphics.DrawLine(cPen, point1, point2);
e.Graphics.DrawLine(cPen, point4, point3);
e.Graphics.FillEllipse(controlpointBrush, new Rectangle(point2.X - controlpointRaduis, point2.Y - controlpointRaduis, controlpointRaduis*2, controlpointRaduis * 2));
e.Graphics.FillEllipse(controlpointBrush, new Rectangle(point3.X - controlpointRaduis, point3.Y - controlpointRaduis, controlpointRaduis * 2, controlpointRaduis * 2));
e.Graphics.DrawBezier(bPen, point1, point2, point3, point4);
}
private void button1_Click(object sender, EventArgs e)
{
double cp1x = Convert.ToDouble(textBox1.Text);
double cp1y = Convert.ToDouble(textBox2.Text);
double cp2x = Convert.ToDouble(textBox3.Text);
double cp2y = Convert.ToDouble(textBox4.Text);
point1 = new Point(offsetX, offsetY + areaHeight);
point2 = new Point(offsetX + (int)(areaWidth * cp1x), offsetY + (int)(areaHeight * cp1y));
point3 = new Point(offsetX + (int)(areaWidth * cp2x), offsetY + (int)(areaHeight * cp2y));
point4 = new Point(offsetX + areaWidth, offsetY);
panel1.Refresh();
}
}
}