VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 307|回复: 0

求优化算法。。。

[复制链接]
38_avatar_middle
最佳答案
0 
在线会员 发表于 2022-7-22 17:18:04 | 显示全部楼层 |阅读模式
求优化算法。。。


求优化算法。。。

求优化算法。。。

从点击DRAW 到显示出 9999 需要时间一分钟还要多


  1. #pragma once
  2. #include<cmath>

  3. struct _Point
  4. {
  5.         int _X;
  6.         int _Y;
  7. };
  8. struct MyStruct
  9. {
  10.         _Point inPoint;
  11.         int fontSize;
  12. };
  13. namespace DrawNumberString {

  14.         using namespace System;
  15.         using namespace System::ComponentModel;
  16.         using namespace System::Collections;
  17.         using namespace System::Windows::Forms;
  18.         using namespace System::Data;
  19.         using namespace System::Drawing;

  20.         /// <summary>
  21.         /// Form1 摘要
  22.         /// </summary>
  23.         public ref class Form1 : public System::Windows::Forms::Form
  24.         {
  25.         public:
  26.                 Form1(void)
  27.                 {
  28.                         InitializeComponent();
  29.                         //
  30.                         //TODO:  在此处添加构造函数代码
  31.                         //
  32.                 }

  33.         protected:
  34.                 /// <summary>
  35.                 /// 清理所有正在使用的资源。
  36.                 /// </summary>
  37.                 ~Form1()
  38.                 {
  39.                         if (components)
  40.                         {
  41.                                 delete components;
  42.                         }
  43.                 }
  44.         private: System::Windows::Forms::PictureBox^ pictureBox1;
  45.         private: System::Windows::Forms::Button^ button1;
  46.         protected:

  47.         private:
  48.                 /// <summary>
  49.                 /// 必需的设计器变量。
  50.                 /// </summary>
  51.                 System::ComponentModel::Container ^components;

  52. #pragma region Windows Form Designer generated code
  53.                 /// <summary>
  54.                 /// 设计器支持所需的方法 - 不要修改
  55.                 /// 使用代码编辑器修改此方法的内容。
  56.                 /// </summary>
  57.                 void InitializeComponent(void)
  58.                 {
  59.                         this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
  60.                         this->button1 = (gcnew System::Windows::Forms::Button());
  61.                         (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox1))->BeginInit();
  62.                         this->SuspendLayout();
  63.                         //
  64.                         // pictureBox1
  65.                         //
  66.                         this->pictureBox1->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(255)), static_cast<System::Int32>(static_cast<System::Byte>(224)),
  67.                                 static_cast<System::Int32>(static_cast<System::Byte>(192)));
  68.                         this->pictureBox1->Location = System::Drawing::Point(0, 0);
  69.                         this->pictureBox1->Name = L"pictureBox1";
  70.                         this->pictureBox1->Size = System::Drawing::Size(200, 200);
  71.                         this->pictureBox1->TabIndex = 0;
  72.                         this->pictureBox1->TabStop = false;
  73.                         //
  74.                         // button1
  75.                         //
  76.                         this->button1->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(255)), static_cast<System::Int32>(static_cast<System::Byte>(128)),
  77.                                 static_cast<System::Int32>(static_cast<System::Byte>(0)));
  78.                         this->button1->Font = (gcnew System::Drawing::Font(L"宋体", 14.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
  79.                                 static_cast<System::Byte>(134)));
  80.                         this->button1->ForeColor = System::Drawing::Color::Blue;
  81.                         this->button1->Location = System::Drawing::Point(48, 203);
  82.                         this->button1->Name = L"button1";
  83.                         this->button1->Size = System::Drawing::Size(104, 39);
  84.                         this->button1->TabIndex = 1;
  85.                         this->button1->Text = L"D R A W";
  86.                         this->button1->UseVisualStyleBackColor = false;
  87.                         this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
  88.                         //
  89.                         // Form1
  90.                         //
  91.                         this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
  92.                         this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
  93.                         this->ClientSize = System::Drawing::Size(200, 246);
  94.                         this->Controls->Add(this->button1);
  95.                         this->Controls->Add(this->pictureBox1);
  96.                         this->Name = L"Form1";
  97.                         this->Text = L"Form1";
  98.                         (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox1))->EndInit();
  99.                         this->ResumeLayout(false);

  100.                 }
  101. #pragma endregion
  102.         private: int calc_left(Image^ img)
  103.         {
  104.                 Bitmap^ tempmap = gcnew Bitmap(img);
  105.                 int x = tempmap->Width;
  106.                 int y = tempmap->Height;
  107.                 Color Backcolor = tempmap->GetPixel(0, 0);
  108.                 Color Newcolor;
  109.                 for (int x = 0; x < tempmap->Width; x++)
  110.                         for (int y = 0; y < tempmap->Height; y++)
  111.                         {
  112.                                 Newcolor = tempmap->GetPixel(x,y);
  113.                                 if (Newcolor != Backcolor)
  114.                                 {
  115.                                         tempmap->~Bitmap();
  116.                                         return x;
  117.                                 }
  118.                         }
  119.         }
  120.         private: int calc_right(Image^ img)
  121.         {
  122.                 Bitmap^ tempmap = gcnew Bitmap(img);
  123.                 int x = tempmap->Width;
  124.                 int y = tempmap->Height;
  125.                 Color Backcolor = tempmap->GetPixel(0, 0);
  126.                 Color Newcolor;
  127.                 for (int x = 1; x <= tempmap->Width; x++)
  128.                         for (int y = 0; y < tempmap->Height; y++)
  129.                         {
  130.                                 Newcolor = tempmap->GetPixel(tempmap->Width - x, y);
  131.                                 if (Newcolor != Backcolor)
  132.                                         return tempmap->Width - x;
  133.                         }
  134.         }
  135.         private: int calc_top(Image^ img)
  136.         {
  137.                 Bitmap^ tempmap = gcnew Bitmap(img);
  138.                 int x = tempmap->Width;
  139.                 int y = tempmap->Height;
  140.                 Color Backcolor = tempmap->GetPixel(0, 0);
  141.                 Color Newcolor;
  142.                 for (int y = 0; y < tempmap->Height; y++)
  143.                         for (int x = 0; x < tempmap ->Width; x++)
  144.                         {
  145.                                 Newcolor = tempmap->GetPixel(x, y);
  146.                                 if (Newcolor != Backcolor)
  147.                                 {
  148.                                         tempmap->~Bitmap();
  149.                                         return y;
  150.                                 }
  151.                         }
  152.         }
  153.         private: int calc_bottom(Image^ img)
  154.         {
  155.                 Bitmap^ tempmap = gcnew Bitmap(img);
  156.                 int x = tempmap->Width;
  157.                 int y = tempmap->Height;
  158.                 Color Backcolor = tempmap->GetPixel(0, 0);
  159.                 Color Newcolor;
  160.                 for (int y = 1; y <= tempmap->Height; y++)
  161.                         for (int x = 0; x < tempmap->Width; x++)
  162.                         {
  163.                                 Newcolor = tempmap->GetPixel(x, tempmap->Height - y);
  164.                                 if (Newcolor != Backcolor)
  165.                                         return tempmap->Height - y;
  166.                         }
  167.         }
  168.         private: int calc_catercorner(Image^ img)
  169.         {
  170.                 int w = (calc_right(img) - calc_left(img)) + 1;
  171.                 int h = (calc_bottom(img) - calc_top(img)) + 1;
  172.                 return std::sqrt(w * w + h * h);
  173.         }
  174.         private: MyStruct calc_format(Point^ inpoint, int wide, int num)
  175.         {
  176.                 int w = 0;
  177.                 if (num < 10)
  178.                 {
  179.                         num = 8;
  180.                         w = wide;
  181.                 }
  182.                 else if (num < 100)
  183.                 {
  184.                         num = 88;
  185.                         w = 2 * wide;
  186.                 }
  187.                 else if (num < 1000)
  188.                 {
  189.                         num = 888;
  190.                         w = 3 * wide;
  191.                 }
  192.                 else if (num < 10000)
  193.                 {
  194.                         num = 8888;
  195.                         w = 4 * wide;
  196.                 }

  197.                 Image^ tempimg = gcnew Bitmap(w, (wide*3)/2);
  198.                 Graphics^ g =  Graphics::FromImage(tempimg);
  199.                 SolidBrush^ tempBrush = gcnew SolidBrush(Color::White);
  200.                 g->FillRectangle(tempBrush, Rectangle(0, 0, w, (wide * 3) / 2));
  201.                 int fontsize = 0;
  202.                 tempBrush->Color = Color::Black;
  203.                 System::Drawing::Font^ drawFont = gcnew System::Drawing::Font("宋体",wide+ fontsize);
  204.                 g->DrawString(num.ToString(), drawFont, tempBrush, Point(0, 0));
  205.                 int catercorner = calc_catercorner(tempimg);  //计算字符尺寸对角线
  206.                 while (catercorner < wide * 4 / 5)
  207.                 {
  208.                         fontsize++;
  209.                         tempBrush->Color = Color::White;
  210.                         g->FillRectangle(tempBrush, Rectangle(0, 0, w, (wide * 3) / 2));
  211.                         tempBrush->Color = Color::Black;
  212.                         drawFont = gcnew System::Drawing::Font("宋体", wide + fontsize);
  213.                         g->DrawString(num.ToString(), drawFont, tempBrush, Point(0, 0));
  214.                         catercorner = calc_catercorner(tempimg);
  215.                 }
  216.                 while (wide * 4 / 5- catercorner <=2)
  217.                 {
  218.                         fontsize--;
  219.                         tempBrush->Color = Color::White;
  220.                         g->FillRectangle(tempBrush, Rectangle(0, 0, w, (wide * 3) / 2));
  221.                         tempBrush->Color = Color::Black;
  222.                         drawFont = gcnew System::Drawing::Font("宋体", wide + fontsize);
  223.                         g->DrawString(num.ToString(), drawFont, tempBrush, Point(0, 0));
  224.                         catercorner = calc_catercorner(tempimg);
  225.                 }
  226.                 MyStruct _format;
  227.                 _format.fontSize = wide + fontsize;
  228.                 _format.inPoint._X = inpoint->X + (wide -(calc_right(tempimg) - calc_left(tempimg)))/2 - calc_left(tempimg);
  229.                 _format.inPoint._Y = inpoint->Y + (wide - (calc_bottom(tempimg) - calc_top(tempimg))) / 2 - calc_top(tempimg);

  230.                 return _format;
  231.         }
  232.         private: Void draw_num_str(Point^ inpoint, int wide, int num)
  233.         {
  234.                 MyStruct drawformat = calc_format(inpoint, wide, num);
  235.                 System::Drawing::Font^ drawFont = gcnew System::Drawing::Font("宋体", drawformat.fontSize);
  236.                 Image^ drawimg = gcnew Bitmap(wide, wide);
  237.                 Graphics^ g = Graphics::FromImage(drawimg);
  238.                 SolidBrush^ MyBrush = gcnew SolidBrush(Color::Cyan);
  239.                 g->FillRectangle(MyBrush, Rectangle(inpoint->X + wide / 20, inpoint->X + wide / 20, (wide * 9) / 10, (wide * 9) / 10));
  240.                 MyBrush->Color = Color::Black;
  241.                 g->FillEllipse(MyBrush, Rectangle(inpoint->X + wide / 10, inpoint->Y + wide / 10, (wide * 4) / 5, (wide * 4) / 5));
  242.                 MyBrush->Color = Color::Cyan;
  243.                
  244.                 g->DrawString(num.ToString(), drawFont, MyBrush, Point(drawformat.inPoint._X, drawformat.inPoint._Y));
  245.                 this->pictureBox1->Image = drawimg;
  246.         }
  247.         private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
  248.         {
  249.                 draw_num_str(Point(0, 0), 200, 9999);
  250.         }
  251.         };
  252. }
复制代码



求优化算法。。。 代码文件.rar (2.44 KB, 下载次数: 0)




上一篇:求助, 什么叫函数中的 “函数”,不理解,不会用。
下一篇:求助:DrawString 函数, 如何输出如图效果。。。。。
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

×【发帖 友情提示】
1、请回复有意义的内容,请勿恶意灌水;
2、纯数字、字母、表情等无意义的内容系统将自动删除;
3、若正常回复后帖子被自动删除,为系统误删的情况,请重新回复其他正常内容或等待管理员审核通过后会自动发布;
4、感谢您对VC驿站一如既往的支持,谢谢合作!

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号-4 )|网站地图wx_jqr

GMT+8, 2023-12-8 16:55

Powered by CcTry.CoM

© 2009-2021 cctry.com

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