⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 plot.h

📁 differential evolution algorithm optimization
💻 H
📖 第 1 页 / 共 3 页
字号:
/**************************************************
The plot.h include file allows to directly output
graphics / perform simple drawings in a standard
Windows 95/NT window
More information at http://www.4p8.com/eric.brasseur/plot.html
Enhancements by: Rainer Storn
***************************************************/

#include <windows.h>
#include <fstream.h>
#include <stdio.h>
#include <crtdbg.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <strstrea.h>
#include <typeinfo.h>

#ifndef BITMAPWIDTH 
   #define BITMAPWIDTH 800
#endif
#ifndef BITMAPHEIGHT
   #define BITMAPHEIGHT 600
#endif

#define NEWLINE "\x0D\x0A"
// 64 bit integer type :
typedef __int64 malabar;
#define MALABAR(a,b) (((malabar) a) << 32) + (malabar) b
#define LOW32(a) (long)a
#define HIGH32(a) (long)(a >> 32)

//flag indicating that program is about to exit
int  gi_exit_flag = 0; //1: indicates that program is about to exit

malabar MilliTime ()
{
   malabar Time;
   static malabar PreviousTime = 0;
   static malabar Turns = 0;
   SYSTEMTIME t;
   GetSystemTime (&t);
   Time = 
         (malabar) t.wMilliseconds + 
         (malabar) t.wSecond * 1000 +
         (malabar) t.wMinute * 60000 +
         (malabar) t.wHour * 3600000 +
         (malabar) t.wDayOfWeek * 86400000;
   if (Time < PreviousTime) Turns++;
   PreviousTime = Time;
   Time += Turns * 604800000;
   return Time;
}

double Rnd ()
{
   static BOOL first = TRUE;
   if (first) 
   {
      srand ((int) MilliTime());
      first = FALSE;
   }
   return (double) rand() / ((double) RAND_MAX + 1);
}

#define CR  '\x0D'   // 13
#define LF  '\x0A'   // 10
#define BS  '\x08'   //  8
#define TAB '\x09'   //  9
#define FF  '\x0C'   // 12
#define CU  '\x0E'   // 14
#define CD  '\x0F'   // 15
#define CL  '\x10'   // 16
#define CI  '\x11'   // 17
char Inkey ();
void Refresh ();
//=========================================================================================================
//===========Start of this huge class named "Bitmap"=======================================================
//=========================================================================================================
class Bitmap 
{
public :
   BITMAPINFO           BitmapInfo;
   unsigned char*       BitmapArray;
   HDC                  hDC;
   HBITMAP              hBitmap;
   HBITMAP              hBitmapOld;
   RECT                 Rect;          // Area that must be redrawn because of changes
   int                  Width;
   int                  Height;
   int                  LineWidth;
   BYTE                 InkR;
   BYTE                 InkG;
   BYTE                 InkB;
   BYTE                 PaperR;
   BYTE                 PaperG;
   BYTE                 PaperB;
   int                  LocateL;
   int                  LocateC;
   double               TurtleX;
   double               TurtleY;
   double               TurtleDirection;
   double               Pi;
   BOOL                 TurtlePenTracing;
   int                  CharacterWidth;
   int                  CharacterHeight;
   int                  Lines;
   int                  Columns;

   void CreateDIBDC ()
   {
      HDC hdc1;
      hdc1 = CreateDC ("DISPLAY", NULL, NULL, NULL);
      hDC = CreateCompatibleDC (hdc1);
      DeleteDC (hdc1);
   }
   void DeleteDIBDC ()
   {
      DeleteDC (hDC);
   }

   Bitmap (int w, int h)
   {
      Width = w;
      Height = h;
      LineWidth = 0;
      InkR = 0;
      InkG = 0;
      InkB = 0;
      PaperR = 255;
      PaperG = 255;
      PaperB = 255;
      LocateL = 1;
      LocateC = 1;
      TurtleX = w / 2;
      TurtleY = h / 2;
      Pi = atan (1) * 4;
      TurtleDirection = Pi / 2;
      TurtlePenTracing = TRUE;
      CharacterWidth = 8;
      CharacterHeight = 15;
      Lines = Height / CharacterHeight;
      Columns = Width / CharacterWidth;
      // Initialization of DIB
      BitmapInfo.bmiHeader.biSize =          sizeof(BITMAPINFOHEADER);
      BitmapInfo.bmiHeader.biWidth =         w;
      BitmapInfo.bmiHeader.biHeight =        h;
      BitmapInfo.bmiHeader.biPlanes =        1;
      BitmapInfo.bmiHeader.biBitCount =      24;
      BitmapInfo.bmiHeader.biCompression =   BI_RGB;
      BitmapInfo.bmiHeader.biSizeImage =     w * h * 3;
      BitmapInfo.bmiHeader.biXPelsPerMeter = 0;
      BitmapInfo.bmiHeader.biYPelsPerMeter = 0;
      BitmapInfo.bmiHeader.biClrUsed =       0;
      BitmapInfo.bmiHeader.biClrImportant =  0;

      // BitmapArray = new unsigned char[Width * Height * 3];
      BitmapArray = NULL;
      hBitmap = CreateDIBSection (NULL,
                              &BitmapInfo,
                              DIB_RGB_COLORS,
                              (void **) &BitmapArray,
                              NULL, 
                              0);
//----requires crtdbg.h------------------------------------------------------------------
      if (BitmapArray == NULL) _RPT0 (_CRT_ERROR, "Not enough memory for BitmapArray");
      if (hBitmap == NULL) _RPT0 (_CRT_ERROR, "Could not create Bitmap");//---------------------------------------------------------------------------------------
      CreateDIBDC ();
      hBitmapOld = (struct HBITMAP__ *)SelectObject (hDC, hBitmap);
   }

   ~Bitmap()
   {
      SelectObject (hDC, hBitmapOld);
      DeleteDIBDC ();
      DeleteObject (hBitmap);
   }

   void Ink (BYTE r, BYTE g, BYTE b)
   {
      InkR = r;
      InkG = g;
      InkB = b;
   }
   void Paper (BYTE r, BYTE g, BYTE b)
   {
      PaperR = r;
      PaperG = g;
      PaperB = b;
   }
   void Plot (int x, int y, BYTE r, BYTE g, BYTE b)
   {
      long a, oy;
      if ((x >= 0) && (x < Width) && (y >= 0) && y < Height)
      {
         a = (x + Width * y) * 3;
         // if (IsBadWritePtr (BitmapArray, 4)) _RPT0 (_CRT_ERROR, "No access !");
         BitmapArray[a + 2] = r;
         BitmapArray[a + 1] = g;
         BitmapArray[a + 0] = b;
         if (Rect.left > x) Rect.left = x;
         if (Rect.right <= x) Rect.right = x + 2;
         oy = Height - y - 1;
         if (Rect.top > oy) Rect.top = oy;
         if (Rect.bottom <= oy) Rect.bottom = oy + 2;
      }
   }
   void Plot (int x, int y)
   {
      Plot (x, y, InkR, InkG, InkB);
   }
   void Pixel (int x, int y, BYTE &r, BYTE &g, BYTE &b)
   {
      long a;
      if ((x >= 0) && (x < Width) && (y >= 0) && y < Height)
      {
         a = (x + Width * y) * 3;
         r = BitmapArray[a + 2];
         g = BitmapArray[a + 1];
         b = BitmapArray[a + 0];
      }
      else
      {
         r = PaperR;
         g = PaperG;
         b = PaperB;
      }
   }
   BYTE PixelR (int x, int y)
   {
      long a;
      BYTE r;
      if ((x >= 0) && (x < Width) && (y >= 0) && y < Height)
      {
         a = (x + Width * y) * 3;
         r = BitmapArray[a + 2];
      }
      else r = PaperR;
      return r;
   }
   BYTE PixelG (int x, int y)
   {
      long a;
      BYTE g;
      if ((x >= 0) && (x < Width) && (y >= 0) && y < Height)
      {
         a = (x + Width * y) * 3;
         g = BitmapArray[a + 1];
      }
      else g = PaperG;
      return g;
   }
   BYTE PixelB (int x, int y)
   {
      long a;
      BYTE b;
      if ((x >= 0) && (x < Width) && (y >= 0) && y < Height)
      {
         a = (x + Width * y) * 3;
         b = BitmapArray[a + 0];
      }
      else b = PaperB;
      return b;
   }
   void Print (int x, int y, const char* s)
   {
      int height, oy, len, ol;
      RECT r;
      len = strlen (s);
      r.top = Height - y - 1; 
      r.left = x; 
      r.bottom = Height; 
      r.right = Width;
      HFONT hFont, hFontOld;
      hFont = CreateFont( 0, 0,
                          0, 0,
                          FW_NORMAL,
                          FALSE,
                          FALSE,
                          FALSE,
                          ANSI_CHARSET,
                          OUT_DEFAULT_PRECIS,
                          CLIP_DEFAULT_PRECIS,
                          DEFAULT_QUALITY,
                          FIXED_PITCH || FF_DONTCARE,
                          "fixedsys");
//----requires crtdbg.h------------------------------------------------------------------
      if (hFont == NULL) _RPT0 (_CRT_ERROR, "Could not create Font");
      hFontOld = (struct HFONT__ *)SelectObject (hDC, hFont);
//---------------------------------------------------------------------------------------
      SetTextColor (hDC, RGB (InkR, InkG, InkB));
      SetBkColor (hDC, RGB (PaperR, PaperG, PaperB));
      TextOut (hDC, x, Height - y - 1, s, len);
      SelectObject (hDC, hFontOld);
      DeleteObject (hFont);
      height = 20;
      if (Rect.left > x) Rect.left = x;
      ol = x + len * CharacterWidth + 2;
      if (Rect.right < ol) Rect.right = ol;
      oy = Height - y - 1;
      if (Rect.top > oy) Rect.top = oy;
      ol = oy + CharacterHeight + 2;
      if (Rect.bottom < ol) Rect.bottom = ol;
   }

   void FontPrint (int x, int y, char* s, char* fn, int h, BOOL b, BOOL i, BOOL u)
   {
      int height, oy, len, ol;
      RECT r;
      len = strlen (s);
      r.top = Height - y - 1; 
      r.left = x; 
      r.bottom = Height; 
      r.right = Width;
      HFONT hFont, hFontOld;
      hFont = CreateFont( h, 0,
                          0, 0,
                          b ? FW_BOLD : FW_NORMAL,
                          i,
                          u,
                          FALSE,
                          ANSI_CHARSET,
                          OUT_DEFAULT_PRECIS,
                          CLIP_DEFAULT_PRECIS,
                          DEFAULT_QUALITY,
                          DEFAULT_PITCH || FF_DONTCARE,
                          fn);
//----requires crtdbg.h------------------------------------------------------------------
      if (hFont == NULL) _RPT0 (_CRT_ERROR, "Could not create Font");
      hFontOld = (struct HFONT__ *)SelectObject (hDC, hFont);
//---------------------------------------------------------------------------------------

      SetTextColor (hDC, RGB (InkR, InkG, InkB));
      SetBkColor (hDC, RGB (PaperR, PaperG, PaperB));
      //height = DrawText (hDC, s, strlen (s), &r, DT_LEFT | DT_TOP);
      TextOut (hDC, x, Height - y - 1, s, len);
      SelectObject (hDC, hFontOld);
      DeleteObject (hFont);
      height = 20;
      if (Rect.left > x) Rect.left = x;
      ol = Width + 2;
      if (Rect.right < ol) Rect.right = ol;
      oy = Height - y - 1;
      if (Rect.top > oy) Rect.top = oy;
      ol = oy + h * 3 + 2;
      if (Rect.bottom < ol) Rect.bottom = ol;
   }

   void Locate (int l, int c)
   {
      LocateL = l;
      LocateC = c;
      if (LocateL < 1) LocateL = 1;
      if (LocateL > Lines) LocateL = Lines;
      if (LocateC < 1) LocateC = 1;
      if (LocateC > Columns) LocateC = Columns;
   }
   void Print (char *s)
   {
      char *t = s;
      char c;
      char d[1024];
      int i;
label:
      if (strlen (t) == 0) return;
      if (t[0] < 32 && t[0] >= 0)
      {
         c = t[0];
         t++;
         switch (c)
         {
         case BS:
            LocateC--;
            Print (" ");
            LocateC--;
            break;
         case CR:
            LocateC = 1;
            break;
         case LF:
            LocateC = 1;
            LocateL++;
            break;
         case TAB:
            LocateC += 12;
            LocateC = 12 * (LocateC / 12);
            break;
         case FF:
            Cls();
            break;
         case CU:
            LocateL--;
            break;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -