| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1111 人关注过本帖
标题:求优化算法。。。
只看楼主 加入收藏
追梦人zmrghy
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:406
专家分:190
注 册:2021-4-9
结帖率:97.26%
收藏
已结贴  问题点数:20 回复次数:5 
求优化算法。。。
图片附件: 游客没有浏览图片的权限,请 登录注册

图片附件: 游客没有浏览图片的权限,请 登录注册

从点击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)
搜索更多相关主题的帖子: System num int Color img 
2022-07-22 17:09
apull
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:三体星系
等 级:版主
威 望:216
帖 子:1506
专家分:9241
注 册:2010-3-16
收藏
得分:20 
修改了一下,供参考。
Form1.rar (2.04 KB)



图片附件: 游客没有浏览图片的权限,请 登录注册


[此贴子已经被作者于2022-7-23 12:49编辑过]

2022-07-23 12:45
追梦人zmrghy
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:406
专家分:190
注 册:2021-4-9
收藏
得分:0 
回复 2楼 apull
没看懂,晚上,我测试一下效果。


        draw_num_str(Point(0, 0), 200, i);

        if (i < 10)
            i += 1;
        else if (i < 100)
            i += 18;
        else if (i < 1000)
            i += 128;
        else if (i < 10000)
            i += 1024;
        else
            i = 0;

这成了固定值了吗。。。。
以前是根据图片大小计算出来的。。。


晚上,我测试一下代码效果
2022-07-23 17:50
apull
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:三体星系
等 级:版主
威 望:216
帖 子:1506
专家分:9241
注 册:2010-3-16
收藏
得分:0 
这个是我产生随机数的。整个if和timer都可以删除。
2022-07-23 19:44
追梦人zmrghy
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:406
专家分:190
注 册:2021-4-9
收藏
得分:0 
回复 4楼 apull
看懂,这里了,i是显示的数字。。。。

输出,数字 i 时字体大小,和位置坐标。是如何获得???
还没看懂。一会我试一下程序程序代码。。。

[此贴子已经被作者于2022-7-23 20:30编辑过]

2022-07-23 20:28
追梦人zmrghy
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:406
专家分:190
注 册:2021-4-9
收藏
得分:0 
回复 2楼 apull
效果,很不错。。。

好多,功能函数和语法之前之前根本不知道。。。
private: MyStruct get_format(Point^ inpoint, int wide, int num)
不用再扫描像素,来计算,左、右、上、下边缘所在的位置了。。。。

程序代码照抄,根据需要,修改变量参数都可以。
想理解、弄懂,变成自己的知识。
还需要很长时间。。。。。
长时间坚持,可以变成自己的知识。应用时便可得心应手。。。
如果,不坚持,这些知识点不久就会被遗忘。。。。
2022-07-24 09:45
快速回复:求优化算法。。。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026980 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved