求优化算法。。。
从点击DRAW到显示出9999,需要的时间一分钟还要多。
求优化算法。。。
程序代码:
#pragma once #include<cmath> struct _Point { int _X; int _Y; }; struct MyStruct { _Point inPoint; int fontSize; }; namespace DrawNumberString { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; /// <summary> /// Form1 摘要 /// </summary> public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); // //TODO: 在此处添加构造函数代码 // } protected: /// <summary> /// 清理所有正在使用的资源。 /// </summary> ~Form1() { if (components) { delete components; } } private: System::Windows::Forms::PictureBox^ pictureBox1; private: System::Windows::Forms::Button^ button1; protected: private: /// <summary> /// 必需的设计器变量。 /// </summary> System::ComponentModel::Container ^components; #pragma region Windows Form Designer generated code /// <summary> /// 设计器支持所需的方法 - 不要修改 /// 使用代码编辑器修改此方法的内容。 /// </summary> void InitializeComponent(void) { this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox()); this->button1 = (gcnew System::Windows::Forms::Button()); (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox1))->BeginInit(); this->SuspendLayout(); // // pictureBox1 // 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)), static_cast<System::Int32>(static_cast<System::Byte>(192))); this->pictureBox1->Location = System::Drawing::Point(0, 0); this->pictureBox1->Name = L"pictureBox1"; this->pictureBox1->Size = System::Drawing::Size(200, 200); this->pictureBox1->TabIndex = 0; this->pictureBox1->TabStop = false; // // button1 // 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)), static_cast<System::Int32>(static_cast<System::Byte>(0))); this->button1->Font = (gcnew System::Drawing::Font(L"宋体", 14.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(134))); this->button1->ForeColor = System::Drawing::Color::Blue; this->button1->Location = System::Drawing::Point(48, 203); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(104, 39); this->button1->TabIndex = 1; this->button1->Text = L"D R A W"; this->button1->UseVisualStyleBackColor = false; this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click); // // Form1 // this->AutoScaleDimensions = System::Drawing::SizeF(6, 12); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(200, 246); this->Controls->Add(this->button1); this->Controls->Add(this->pictureBox1); this->Name = L"Form1"; this->Text = L"Form1"; (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox1))->EndInit(); this->ResumeLayout(false); } #pragma endregion private: int calc_left(Image^ img) { Bitmap^ tempmap = gcnew Bitmap(img); int x = tempmap->Width; int y = tempmap->Height; Color Backcolor = tempmap->GetPixel(0, 0); Color Newcolor; for (int x = 0; x < tempmap->Width; x++) for (int y = 0; y < tempmap->Height; y++) { Newcolor = tempmap->GetPixel(x,y); if (Newcolor != Backcolor) { tempmap->~Bitmap(); return x; } } } private: int calc_right(Image^ img) { Bitmap^ tempmap = gcnew Bitmap(img); int x = tempmap->Width; int y = tempmap->Height; Color Backcolor = tempmap->GetPixel(0, 0); Color Newcolor; for (int x = 1; x <= tempmap->Width; x++) for (int y = 0; y < tempmap->Height; y++) { Newcolor = tempmap->GetPixel(tempmap->Width - x, y); if (Newcolor != Backcolor) return tempmap->Width - x; } } private: int calc_top(Image^ img) { Bitmap^ tempmap = gcnew Bitmap(img); int x = tempmap->Width; int y = tempmap->Height; Color Backcolor = tempmap->GetPixel(0, 0); Color Newcolor; for (int y = 0; y < tempmap->Height; y++) for (int x = 0; x < tempmap ->Width; x++) { Newcolor = tempmap->GetPixel(x, y); if (Newcolor != Backcolor) { tempmap->~Bitmap(); return y; } } } private: int calc_bottom(Image^ img) { Bitmap^ tempmap = gcnew Bitmap(img); int x = tempmap->Width; int y = tempmap->Height; Color Backcolor = tempmap->GetPixel(0, 0); Color Newcolor; for (int y = 1; y <= tempmap->Height; y++) for (int x = 0; x < tempmap->Width; x++) { Newcolor = tempmap->GetPixel(x, tempmap->Height - y); if (Newcolor != Backcolor) return tempmap->Height - y; } } private: int calc_catercorner(Image^ img) { int w = (calc_right(img) - calc_left(img)) + 1; int h = (calc_bottom(img) - calc_top(img)) + 1; return std::sqrt(w * w + h * h); } private: MyStruct calc_format(Point^ inpoint, int wide, int num) { int w = 0; if (num < 10) { num = 8; w = wide; } else if (num < 100) { num = 88; w = 2 * wide; } else if (num < 1000) { num = 888; w = 3 * wide; } else if (num < 10000) { num = 8888; w = 4 * wide; } Image^ tempimg = gcnew Bitmap(w, (wide*3)/2); Graphics^ g = Graphics::FromImage(tempimg); SolidBrush^ tempBrush = gcnew SolidBrush(Color::White); g->FillRectangle(tempBrush, Rectangle(0, 0, w, (wide * 3) / 2)); int fontsize = 0; tempBrush->Color = Color::Black; System::Drawing::Font^ drawFont = gcnew System::Drawing::Font("宋体",wide+ fontsize); g->DrawString(num.ToString(), drawFont, tempBrush, Point(0, 0)); int catercorner = calc_catercorner(tempimg); //计算字符尺寸对角线 while (catercorner < wide * 4 / 5) { fontsize++; tempBrush->Color = Color::White; g->FillRectangle(tempBrush, Rectangle(0, 0, w, (wide * 3) / 2)); tempBrush->Color = Color::Black; drawFont = gcnew System::Drawing::Font("宋体", wide + fontsize); g->DrawString(num.ToString(), drawFont, tempBrush, Point(0, 0)); catercorner = calc_catercorner(tempimg); } while (wide * 4 / 5- catercorner <=2) { fontsize--; tempBrush->Color = Color::White; g->FillRectangle(tempBrush, Rectangle(0, 0, w, (wide * 3) / 2)); tempBrush->Color = Color::Black; drawFont = gcnew System::Drawing::Font("宋体", wide + fontsize); g->DrawString(num.ToString(), drawFont, tempBrush, Point(0, 0)); catercorner = calc_catercorner(tempimg); } MyStruct _format; _format.fontSize = wide + fontsize; _format.inPoint._X = inpoint->X + (wide -(calc_right(tempimg) - calc_left(tempimg)))/2 - calc_left(tempimg); _format.inPoint._Y = inpoint->Y + (wide - (calc_bottom(tempimg) - calc_top(tempimg))) / 2 - calc_top(tempimg); return _format; } private: Void draw_num_str(Point^ inpoint, int wide, int num) { MyStruct drawformat = calc_format(inpoint, wide, num); System::Drawing::Font^ drawFont = gcnew System::Drawing::Font("宋体", drawformat.fontSize); Image^ drawimg = gcnew Bitmap(wide, wide); Graphics^ g = Graphics::FromImage(drawimg); SolidBrush^ MyBrush = gcnew SolidBrush(Color::Cyan); g->FillRectangle(MyBrush, Rectangle(inpoint->X + wide / 20, inpoint->X + wide / 20, (wide * 9) / 10, (wide * 9) / 10)); MyBrush->Color = Color::Black; g->FillEllipse(MyBrush, Rectangle(inpoint->X + wide / 10, inpoint->Y + wide / 10, (wide * 4) / 5, (wide * 4) / 5)); MyBrush->Color = Color::Cyan; g->DrawString(num.ToString(), drawFont, MyBrush, Point(drawformat.inPoint._X, drawformat.inPoint._Y)); this->pictureBox1->Image = drawimg; } private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { draw_num_str(Point(0, 0), 200, 9999); } }; }
代码文件
代码文件.rar
(2.44 KB)