当前位置:C++技术网 > 资讯 > 扭曲路径之“瓶子倒影”

扭曲路径之“瓶子倒影”

更新时间:2015-12-03 21:32:29浏览次数:1+次

扭曲就是改变物体的形状。扭曲路径,实际上就是改变路径的轮廓。路径的;轮廓取决于自路径的图形形状,多个子路径的图形最终可能构成一个外观极不规则的路径轮廓。不过,我们可以使用GraphicsPath的成员函数GetBound函数(获取包含路径的矩形获取最接近路径轮廓的一个矩形。更改某个矩形的形状,并让路径中的所有自路径的形状也随之改变,即可扭曲路径。Graphics的成员函数Warp可以对路径进行扭曲,其函数的调用格式为:


public:
void Warp (
	array<PointF>^ destPoints, 
	INT count,
	RectangleF srcRect, 
	Matrix^ matrix, 
	WarpMode warpMode
	REAL flatness
);
destPoints
一个 PointF 结构的数组,该数组定义由 srcRect 定义的矩形将变换到的平行四边形。该数组可以包含三个或四个元素。如果该数组包含三个元素,则平行四边形的右下角位置的点可从前三个点导出。
count:目标点数组包含的点的总数,该参数只能取3或4
srcRect
一个 RectangleF,表示将变换为 destPoints 定义的平行四边形的矩形。
matrix
指定将应用于路径的几何变换的 Matrix。
warpMode
一个 WarpMode 枚举,它指定此扭曲操作是使用透视模式还是双线性模式。
flatness:平坦度,不必深究,设为NULL就行了
实现代码:
新建单文档的GDI+工程,加入必要的GDI+的程序,在菜单中添加菜单项“路径填充”,并在其子菜单项下添加一个新的子菜单项“路径的扭曲”,建立消息响应:



void CPathFillpathView::OnPathwrap()
{
	// TODO:  在此添加命令处理程序代码
	Graphics graphics(this->m_hWnd);
	this->RedrawWindow();
	//////创建一个全部由线条组成的路径
	PointF points[] = {
		PointF(20, 60),
		PointF(30, 90),
		PointF(15, 110),
		PointF(15, 145),
		PointF(55, 145),
		PointF(55, 110),
		PointF(40, 90),
		PointF(50, 60)
	};
	GraphicsPath path;
	path.AddLines(points, 8);
	path.CloseFigure();
	/////绘制扭曲前的路径
	Pen bluepen(Color(255, 0, 0,255), 3);
	graphics.DrawPath(&bluepen, &path);
	//////定义放置路径的源矩形
	RectF srcRect(10, 50, 50, 100);
	//////定义矩形映射的目标点
	PointF desPts[] = {
		PointF(220, 10),
		PointF(280, 10),
		PointF(100, 150),
		PointF(300, 150)
	};
	//////扭曲矩形
	path.Warp(desPts, 4, srcRect);
	graphics.DrawPath(&bluepen, &path);
	//////绘制源矩形和目标的多边形
	Pen blackpen(Color(255, 0, 0, 0),2);
	graphics.DrawRectangle(&blackpen, srcRect);
	graphics.DrawLine(&blackpen, desPts[0], desPts[1]);
	graphics.DrawLine(&blackpen, desPts[0], desPts[2]);
	graphics.DrawLine(&blackpen, desPts[1], desPts[3]);
	graphics.DrawLine(&blackpen, desPts[2], desPts[3]);
}
代码实现: