一、绘制蜗牛线
//绘制蜗牛线
void CCavsSampleView::OnMenuitemWoniuline()
{
float pi = 3.1415926f;
CRect rect;
GetClientRect(&rect);
UINT width = rect.Width();
UINT height = rect.Height();
CDC* pDC = GetDC();
for (float x = 0;x < 10*pi; x+= pi/width)
{
if (x == 0) pDC->SetPixel(width/2, 0, RGB(255,0,0));
else pDC->SetPixel((int)(width*sin(x)/x*cos(x)+width/2), (int)(width*sin(x)/x*sin(x)),RGB(255,0,0));
}
}
二、绘制贝塞尔曲线
每条贝塞尔曲线由四个结点组成,一个起点、两个控制点和一个结束点,当调整两个控制点时会改变贝塞尔曲线的形状。
BOOL PolyBezier(
const POINT* lpPoints,//结束点和曲线的控制点的POINT数据结构的数组
int nCount);//指定lpPoints数组中点的个数。此值必须是要画的曲线的数目的3倍加1.
// 绘制贝塞尔曲线
void CCavsSampleView::OnMenuitemDrawbezier()
{
CDC* pDC = GetDC();
CPoint pt[] = { CPoint(40, 72), CPoint(107, 302),
CPoint(329, 292),
CPoint(79, 148),
CPoint(498, 29),
CPoint(322, 201),
CPoint(176, 137) };
pDC->PolyBezier(pt, sizeof(pt)/sizeof(pt[0]));
}
三、绘制正弦曲线
y=Asin(x)
使用DrawLine()函数在屏幕的中心绘制一条蓝色的中心线,在for循环中以x坐标,即横坐标为计算变量,从0到屏幕宽。并通过正炫值和振幅计算显示的y值后,
使用DrawLine函数绘制红色正炫曲线。
//绘制正弦曲线
void CCavsSampleView::OnMenuitemSinline()
{
float pi = 3.1415926f;
Status status = GenericError;
Graphics graphics(m_hWnd);
status = graphics.GetLastStatus();
if (status != Ok)
return;
CRect rect;
GetClientRect(&rect);
UINT width = rect.Width();
UINT height = rect.Height();
Pen bluePen(Color(255, 0, 0, 255), 1);//绘制中心线
graphics.DrawLine(&bluePen, 0, height/2, width, height/2);
Pen redPen(Color(255, 255, 0, 0), 2);//绘制正弦曲线
float oldX = 0.0f;
float oldY=(float)(width/2);
for (float x = 0;x < width;x++)
{
float radian = x/width*10*pi;
float y = (float)sin(radian);
y = (1-y)*height/2;
graphics.DrawLine(&redPen, oldX, oldY, x, y);
oldX = x;
oldY = y;
}
}