函数纹理(国际象棋棋盘纹理&粗布纹理)MFC

函数纹理国际象棋棋盘纹理&粗布纹理)MFC实现  源码百度云下载

  1. 国际象棋棋盘纹理(效果图见最后)
 1 //国际象棋纹理函数
 2 //g(u, v) = a , 向下取整(8u)+向下取整(8v) 为 偶数
 3 //g(u, v) = b ,向下取整(8u)+向下取整(8v) 为 奇数
 4 void CChessGiagView::DrawChess(double a, double b, double step)
 5 {
 6     CDC * pDC = GetDC();
 7     //自定义二维坐标系
 8     CRect rect;
 9     GetClientRect(&rect);
10     pDC->SetMapMode(MM_ANISOTROPIC);
11     pDC->SetWindowExt(rect.Width(), rect.Height());
12     pDC->SetViewportExt(rect.Width(), -rect.Height());
13     pDC->SetViewportOrg(rect.Width()/2, rect.Height()/2);
14     rect.OffsetRect(-rect.Width()/2, -rect.Height()/2);
15     pDC->TextOut(-350, 200, "国际象棋棋盘纹理");
16 
17     double u, v;
18     for(u=0; u<=1.0; u+=step)
19     {
20         for(v=0; v <= 1.0; v+=step)
21         {
22             if((int(floor(u*8)) + int(floor(v*8))) %2 == 0)//偶数颜色a
23             {
24                 pDC->SetPixelV(Round(u*200-400), Round(v*200-100), RGB(a*255, a*255, a*255));//u系数修改大小,减数调节显示位置
25             }
26             else//奇数颜色b
27             {
28                 pDC->SetPixelV(Round(u*200-400), Round(v*200-100), RGB(b*255, b*255, b*255));//u系数修改大小,减数调节显示位置
29             }
30         }
31     }
32     //输出相关参数
33     CString str_a, str_b;
34     str_a.Format("%.1f", a);
35     str_b.Format("%.1f", b);
36     pDC->TextOut(-350, -200, "a="+ str_a+", b="+str_b);
37 }

  2. 粗布纹理 (效果图见最后) 

 1 //粗布纹理函数: f(u, v) = A((cos(pu) + cos(qv))) 
 2 //u, v=[0, 1]; A=[0, 1]随机变量; p, q频率系数
 3 void CChessGiagView::DrawCloth(int p, int q)
 4 {
 5     CDC * pDC = GetDC();
 6     //自定义二维坐标系
 7     CRect rect;
 8     GetClientRect(&rect);
 9     pDC->SetMapMode(MM_ANISOTROPIC);
10     pDC->SetWindowExt(rect.Width(), rect.Height());
11     pDC->SetViewportExt(rect.Width(), -rect.Height());
12     pDC->SetViewportOrg(rect.Width()/2, rect.Height()/2);
13     rect.OffsetRect(-rect.Width()/2, -rect.Height()/2);
14     pDC->TextOut(200, 200, "粗布纹理");
15 
16     double u, v;
17     for(u=0; u<=1; u+=0.001)
18     {
19         for(v=0; v<=1.0; v+=0.001)
20         {
21             double A = double(rand())/RAND_MAX; // A=[0, 1]
22             double f = A*((cos(p*u) + (cos(q*v)))); //颜色
23             pDC->SetPixelV(Round(u*200 + 150), Round(v*200-100), RGB(f*255, f*255, f*255));
24         }
25     }
26     //输出相关参数
27     CString str_p, str_q;
28     str_p.Format("%d", p);
29     str_q.Format("%d", q);
30     pDC->TextOut(200, -200, "p="+ str_p+", q="+str_q);
31 }

  3. 效果

  4. 补充说明:如果需要调节图像的颜色(示例仅为灰度图像), 那么就将RGB 的一个分量置为固定值,比如要红色的图像,就将R=255.

        

VC++ 6.0 编译通过,VC++ 永不过时!

原文地址:https://www.cnblogs.com/yocichen/p/10003576.html