新接到公司任务:
根据给定的500个点,在屏幕上连接成平滑曲线。
请高手指点!
根据给定的500个点,在屏幕上连接成平滑曲线。
请高手指点!
解决方案 »
- C#线程的暂停和停止问题 暂停后无法停止怎么解决?
- WPF: 这个binding应该怎么写?
- WPF TextBox控件的滚动条问题
- WINDOWS CE .net 4.2手机蓝牙开发
- showModalDialog显示一个子窗体后如何在后台操作父窗体的控件对象?
- 当Form的FormBorderStyle为None时,如何用鼠标拖动窗体?
- BindingSource绑定对象集合dataGridView中怎样控制列的个数?
- ●拒绝了对对象 'UpdShowOnNetInfo'(数据库 'EasternPassion',所有者 'dbo')的 EXECUTE 权限。
- 请指点:如何删除web共享文件夹里的文件?
- 如何循序渐进学习c#?敬请指教!!!!!
- 谁能提供c#中获取系统桌面所在路经的api函数的调用方法么?谢谢!
- 关于MediaPlayer 9.0的的EndOfStream事件无法触发的问题....
Private Sub Form_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
'定义Point数组
dim ppoints as point() = new point(499){}
'将这500个点值存放到这个ppoints数组中,过程略......
ppoints(0).x=...:ppoints(0).y=...
...........
ppoints(n).x=...:ppoints(n).y=...
...........
ppoints(499).x=...:ppoints(499).y=... '绘制平滑曲线
e.Grapjocs.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias '消除锯齿
e.Graphics.(Drawing.Pens.Red, ppoints) '用红色绘制此平滑线条
End Sub
//定义Point数组
Point[] ppoints = new point[500]{}
//将这500个点值存放到这个ppoints数组中,过程略......
ppoints[0].x=...:ppoints[0].y=...
...........
ppoints[n].x=...:ppoints[n].y=...
...........
ppoints[499].x=...:ppoints[499].y=... //绘制平滑曲线
e.Grapjocs.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias //消除锯齿
e.Graphics.(Drawing.Pens.Red, ppoints) //用红色绘制此平滑线条
查了些资料,还有些收获!贴出来,请高手点拨查阅了一些以前的帖子,有了些头绪:
给定两点坐标,需要一个插值算法来做平滑处理
查到了三个插值算法公式
分别是
1 线形插值
2 余弦插值
3 三次样条插值 比如三次样条插值的公式
v0 = the point before a
v1 = the point a
v2 = the point b
v3 = the point after b
function Cubic_Interpolate(v0, v1, v2, v3,x)
P = (v3 - v2) - (v0 - v1)
Q = (v0 - v1) - P
R = v2 - v0
S = v1
return Px3 + Qx2 + Rx + S
end of function
但是,对于一个点来说,有两个坐标值。
是不是将前后4个点的X坐标代入公式,得到插值得x坐标;
将前后4个点的Y坐标代入公式,得到插值得y坐标; 另外,公式中的x,是一个系数(0 到1),根据x值的变化,求得的插值在a和b间变化。
在计算横纵坐标值时,只要x相等就得到一个插值点了
我不知理解的是否正确,请高手指点 ,急
A1,A2,A3,A4,A5,A6,A7,A8,.....An选前面K个数(这里设K=5)进行计算,计算后的值为Y5 = (A1 + A2 + A3 + A4 + A5) / 5Y6 = (A2 + A3 + A4 + A5 + A6) / 5;...
Yn = (An-4 + An-3 + An-2 + An-1 + An) / K即,目前点的值为前K个点的平均值。最后,再把Y1...Yn这些点连起来主很平滑了。K越大越平滑!!!
你一定要试一下啊,因为我也做过,可以的!!!!!!
http://xynet.6to23.com/blog/more.asp?name=chdxueyuan&id=14C语言编写,参考一下哈!
绘制贝塞尔曲线需要开始和结束两个点,还要两个控制点。
你是说,用开始和结束两个点旁边的点作控制点吗?
用上边几位提到的DrawCurve 这是Programming Windows with C#中使用DrawCurve的范例
//----------------------------------------------
// CanonicalSpline.cs ?2001 by Charles Petzold
//----------------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;class CanonicalSpline: Form
{
protected Point[] apt = new Point[4];
protected float fTension = 0.5f; public static void Main()
{
Application.Run(new CanonicalSpline());
}
public CanonicalSpline()
{
Text = "Canonical Spline";
BackColor = SystemColors.Window;
ForeColor = SystemColors.WindowText;
ResizeRedraw = true; ScrollBar scroll = new VScrollBar();
scroll.Parent = this;
scroll.Dock = DockStyle.Right;
scroll.Minimum = -100;
scroll.Maximum = 109;
scroll.SmallChange = 1;
scroll.LargeChange = 10;
scroll.Value = (int) (10 * fTension);
scroll.ValueChanged += new EventHandler(ScrollOnValueChanged); OnResize(EventArgs.Empty);
}
void ScrollOnValueChanged(object obj, EventArgs ea)
{
ScrollBar scroll = (ScrollBar) obj; fTension = scroll.Value / 10f; Invalidate(false);
}
protected override void OnResize(EventArgs ea)
{
base.OnResize(ea); int cx = ClientSize.Width;
int cy = ClientSize.Height; apt[0] = new Point( cx / 4, cy / 2);
apt[1] = new Point( cx / 2, cy / 4);
apt[2] = new Point( cx / 2, 3 * cy / 4);
apt[3] = new Point(3 * cx / 4, cy / 2);
}
protected override void OnMouseDown(MouseEventArgs mea)
{
Point pt; if (mea.Button == MouseButtons.Left)
{
if (ModifierKeys == Keys.Shift)
pt = apt[0];
else if (ModifierKeys == Keys.None)
pt = apt[1];
else
return;
}
else if (mea.Button == MouseButtons.Right)
{
if (ModifierKeys == Keys.None)
pt = apt[2];
else if (ModifierKeys == Keys.Shift)
pt = apt[3];
else
return;
}
else
return; Cursor.Position = PointToScreen(pt);
}
protected override void OnMouseMove(MouseEventArgs mea)
{
Point pt = new Point(mea.X, mea.Y); if (mea.Button == MouseButtons.Left)
{
if (ModifierKeys == Keys.Shift)
apt[0] = pt;
else if (ModifierKeys == Keys.None)
apt[1] = pt;
else
return;
}
else if (mea.Button == MouseButtons.Right)
{
if (ModifierKeys == Keys.None)
apt[2]= pt;
else if (ModifierKeys == Keys.Shift)
apt[3] = pt;
else
return;
}
else
return; Invalidate();
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics grfx = pea.Graphics;
Brush brush = new SolidBrush(ForeColor); grfx.DrawCurve(new Pen(ForeColor), apt, fTension); grfx.DrawString("Tension = " + fTension, Font, brush, 0, 0); for (int i = 0; i < 4; i++)
grfx.FillEllipse(brush, apt[i].X - 3, apt[i].Y - 3, 7, 7);
}
}