找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 16071|回复: 0

[其他] EasyIFS分形学演示,命令行版

[复制链接]
发表于 2017-5-3 11:42:10 | 显示全部楼层 |阅读模式
EasyIFS分形学演示工具是ZH@EASYX.CN用easyx库写的作品,体积比较大,并且不属于控制台程序,我用GDI库改写了该工具,使其可以在批处理中批量调用,并修复原版个别漏洞。体积也由112KB缩小到13KB。
下载附件:http://bcn.bathome.net/s/tool/index.html?key=EasyIFS
图片均为外链图
  1. /*
  2.         CONSOLE FRACTAL IFS, AUTHOR: ZH@EASYX.CN, MODIFYED BY HAPPY.
  3.         EASYIFS.EXE
  4. */

  5. #include    <string>
  6. #include    <vector>
  7. #include   <stdio.h>
  8. #include <windows.h>
  9. #include   <conio.h>
  10. #include    <time.h>
  11. #include      <io.h>

  12. using namespace std;

  13. // 对VC6.0、VS2010、GCC作兼容性编译
  14. #if defined (__GNUC__) || (_MSC_VER == 1200)
  15. extern "C" HWND WINAPI GetConsoleWindow();
  16. #endif

  17. // 宏名变量定义
  18. #define F_EXIST 4
  19. #define E_WITH  640
  20. #define E_HIGH  480

  21. // 定义帮助说明
  22. #define HELP_INFORMATION "\
  23. Usage: easyifs [file]\n\
  24. \n\
  25.     The probability of requiring each formula and equ 1\n\
  26.     IFS configuration file:\n\
  27. \n\
  28.     [Main]\n\
  29.     name = IFS name\n\
  30.     iterLimit = number of iterations\n\
  31.     color = drawing color\n\
  32.     minX = the lower limit of x\n\
  33.     maxX = the upper limit of x\n\
  34.     minY = the lower limit of y\n\
  35.     maxY = the upper limit of y\n\
  36.     condition = the number of formulas\n\
  37.     [Conditionx]\n\
  38.     p = probability\n"


  39. // IFS 全局参数
  40. struct MAIN
  41. {
  42.         string name;             // ifs 名称
  43.         int iterLimit;           // 迭代次数
  44.         int color;               // 绘图颜色
  45.         double scale;            // 相对绘图窗口的缩放比例
  46.         int offsetX, offsetY;    // 相对绘图窗口的偏移量
  47. };

  48. // 每个IFS公式的参数
  49. struct IFS
  50. {
  51.         int p;                   // 概率
  52.         double a, b, c, d, e, f; // IFS 公式参数
  53. };

  54. // 全局变量
  55. MAIN g_main;
  56. vector<IFS> g_ifs;

  57. // 从配置文件中获取 double 类型数据
  58. double GetPrivateProfileDouble(LPCTSTR lpAppName, LPCTSTR lpKeyName, double fDefault, LPCTSTR lpFileName)
  59. {
  60.         TCHAR d[50];
  61.         GetPrivateProfileString(lpAppName, lpKeyName, NULL, d, 50, lpFileName);
  62.         return (d[0]!=0)?atof(d):fDefault;
  63. }

  64. // 根据配置文件初始化 IFS 系统
  65. int initargs(int argc, char** argv)
  66. {
  67.         // 如果未指定参数,显示帮助信息
  68.         if (argc <= 1)
  69.         {
  70.                 fprintf(stdout, HELP_INFORMATION);
  71.                 exit(1);
  72.         }

  73.         // 定义变量
  74.         double ix, ax, iy, ay, sx, sy;
  75.         // 配置文件的文件名
  76.         char tmpFpath[MAX_PATH*2];
  77.         if(strrchr(argv[1], ':')==NULL)
  78.         {
  79.                 strcpy(tmpFpath, ".\");
  80.                 strcat(tmpFpath, argv[1]);
  81.         }
  82.         else
  83.         {
  84.                 strcpy(tmpFpath, argv[1]);
  85.         }

  86.         char* filename=tmpFpath;

  87.         // 如果参数指定的文件不存在或禁止访问,退出系统
  88.         if (_access(filename, F_EXIST) != 0)
  89.         {
  90.                 fprintf(stdout, "The file "%s" does not exist or is disabled.\n", argv[1]);
  91.                 exit(2);
  92.         }

  93.         // 获取 IFS 名称
  94.         char tmpName[MAX_PATH*2];
  95.         GetPrivateProfileStringA("main", "name", "noname", tmpName, 50, filename);
  96.         g_main.name = tmpName;

  97.         // 获取迭代次数
  98.         g_main.iterLimit = GetPrivateProfileIntA("main", "iterLimit", 0, filename);

  99.         // 获取绘图颜色
  100.         g_main.color = GetPrivateProfileIntA("main", "color", 0xff00, filename);

  101.         // 获取 x 方向上的缩放比例
  102.         ix = GetPrivateProfileDouble("main", "minX", 0, filename);
  103.         ax = GetPrivateProfileDouble("main", "maxX", 0, filename);
  104.         sx = E_WITH / (ax - ix);

  105.         // 获取 y 方向上的缩放比例
  106.         iy = GetPrivateProfileDouble("main", "minY", 0, filename);
  107.         ay = GetPrivateProfileDouble("main", "maxY", 0, filename);
  108.         sy = E_HIGH / (ay - iy);

  109.         // 根据绘图窗口尺寸,确定恰当的缩放比例及偏移量
  110.         if (sx > sy)
  111.         {
  112.                 g_main.scale = sy;
  113.                 g_main.offsetX = (int)(-ix * g_main.scale + (E_WITH - (ax - ix) * g_main.scale) / 2);
  114.                 g_main.offsetY = (int)(-iy * g_main.scale);
  115.         }
  116.         else
  117.         {
  118.                 g_main.scale = sx;
  119.                 g_main.offsetX = (int)(-ix * g_main.scale);
  120.                 g_main.offsetY = (int)(-iy * g_main.scale + (E_HIGH - (ay - iy) * g_main.scale) / 2);
  121.         }

  122.         // 获取公式数量
  123.         int n;
  124.         n = GetPrivateProfileIntA("main", "condition", 0, filename);

  125.         // 获取每一个公式的常数项及概率
  126.         IFS ifs;
  127.         int sump = 0;
  128.         char app[] = "condition?";
  129.         for(int i=0; i<n; i++)
  130.         {
  131.                 app[9] = ('1' + i);
  132.                 ifs.a = GetPrivateProfileDouble(app, "a", 0, filename);
  133.                 ifs.b = GetPrivateProfileDouble(app, "b", 0, filename);
  134.                 ifs.c = GetPrivateProfileDouble(app, "c", 0, filename);
  135.                 ifs.d = GetPrivateProfileDouble(app, "d", 0, filename);
  136.                 ifs.e = GetPrivateProfileDouble(app, "e", 0, filename);
  137.                 ifs.f = GetPrivateProfileDouble(app, "f", 0, filename);
  138.                 ifs.p = int(GetPrivateProfileDouble(app, "p", 0, filename) * 1000000 + 0.5);
  139.                 ifs.p += sump;
  140.                 sump = ifs.p;

  141.                 g_ifs.push_back(ifs);
  142.         }

  143.         // 如果各公式的概率和不等于 1,返回错误信息
  144.         if (sump != 1000000)
  145.         {
  146.                 fprintf(stdout, "IFS configuration file in the probability of the formula and not equal to 1, please check the configuration file.\n");
  147.                 exit(3);
  148.         }

  149.         return 0;
  150. }

  151. // 主函数
  152. int main(int argc, char* argv[])
  153. {
  154.         // 初始化IFS配置
  155.         initargs(argc, argv);
  156.         srand(unsigned(time(NULL)));

  157.         // 初始化图形窗口
  158.         HWND hCMD=GetConsoleWindow();
  159.         //刷新窗口
  160.         InvalidateRect(hCMD, NULL, TRUE);
  161.        
  162.         HDC  hDC=GetDC(hCMD);

  163.         // 创建变量
  164.         double x = 0, y = 0, tx;
  165.         int p, k;

  166.         //设置背景透明
  167.         //SetBkMode(hDC,TRANSPARENT);
  168.         //设置字体颜色
  169.         //SetTextColor(hDC, RGB(255,255,255));
  170.         // 显示名称
  171.         //TextOutA(hDC, 0,0, g_main.name.c_str(), strlen(g_main.name.c_str()));

  172.         // 迭代求解
  173.         for(int i=0; i<g_main.iterLimit; i++)
  174.         {
  175.                 // 生成概率
  176.                 p = int(double(rand()) / RAND_MAX * 1000000 + 0.5);

  177.                 // 根据概率获取选用的公式 k
  178.                 for(k=0; k< (int)g_ifs.size(); k++)
  179.                         if (p <= g_ifs[k].p)        break;

  180.                 // 根据公式 k 迭代
  181.                 tx = g_ifs[k].a * x + g_ifs[k].b * y + g_ifs[k].e;
  182.                 y  = g_ifs[k].c * x + g_ifs[k].d * y + g_ifs[k].f;
  183.                 x  = tx;

  184.                 // 画点(转换到屏幕坐标系)
  185.                 SetPixel(hDC, int(x * g_main.scale) + g_main.offsetX, E_HIGH - (int(y * g_main.scale) + g_main.offsetY),  g_main.color);
  186.         }
  187.         return 0;
  188. }
复制代码

评分

参与人数 1技术 +1 收起 理由
老刘1号 + 1 沉迷于绘图无法自拔的Happy兄

查看全部评分

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-16 23:51 , Processed in 0.017226 second(s), 9 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表