MFC运用GraphicsPath绘制曲线、选择曲线(判断点是否在曲线上)

来源:互联网 发布:java redis lua脚本 编辑:程序博客网 时间:2024/05/17 05:55

最近在看GDI+相关知识,发现GDI+提供了绘制曲线的方法。想起自己以前为了实现张力样条曲线单独写了一个动态库(= =!),便想试一试看看效果如何

用到的类有Graphics和GraphicsPath,方法有Graphics.DrawPath() 、GraphicsPath.AddCurve()和GraphicsPath.IsOutlineVisible()

类似的用法在C#中貌似很多,但是MFC中相关的用法比较少,现总结如下:

1.主要函数介绍

public void AddCurve(
PointF[] points, int num,
 int offset,
int numberOfSegments,
float tension)

points 一个PointF 结构的数组,它表示定义曲线的点。

offsetpoints 数组中某元素的索引,该元素将用作曲线的第一个点。

num用于绘制曲线的点的数目

numberOfSegments 用于绘制曲线的线段的数目。 线段可被视为连接两点的直线。(非闭合为num -1 

tension 指定曲线在控制点间弯曲程度的值。大于 1 的值将产生不可预知的结果。

2.思路解析

首先建立Graphics对象和画笔。然后运用GraphicsPath.AddCurve()方法将曲线添加到路径(GraphicsPath对象),最后运用GraphicsPath.IsOutlineVisible()进行测试,判断点是否在曲线上。

3.示例代码(部分)

//这段代码是绘制样条曲线时鼠标移动实时绘制中的部分代码 m_PushNumb>1
GraphicsPath graphicsPath;//构造路径对象Graphics graphics(pDC->m_hDC);//构造GraphicsPen whitePen(Color::White, 3);//构造画笔PointF *curvePoints = new PointF[m_PushNumb+1];//int xx,yy;//重新绘制for (int i=0;i<m_PushNumb;++i){f_PvFundll.DPtoVP(m_PointXyz[i].x,m_PointXyz[i].y,&xx,&yy);//转换成屏幕坐标curvePoints[i].X = xx;curvePoints[i].Y = yy;}curvePoints[m_PushNumb].X = m_draw_curve_point.x;//鼠标停留点坐标curvePoints[m_PushNumb].Y = m_draw_curve_point.y;// 画曲线GraphicsPath gPath;gPath.AddCurve(curvePoints,m_PushNumb+1,0,m_PushNumb,0.4f);graphics.DrawPath(&whitePen,&gPath);
//判断t_x t_y是否在路径上(可用来做图形选择)
BOOL TESTB =  gPath.IsOutlineVisible(t_x,t_y,&whitePen,&graphics);delete []curvePoints;



0 0