Deutsch   English   Français   Italiano  
<4ffeda4ce7116f70754bcfcaee87cb729081fac3.camel@gmail.com>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: news.eternal-september.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: wij <wyniijj5@gmail.com>
Newsgroups: comp.lang.c++
Subject: 'Graphics' of libwy
Date: Sun, 15 Dec 2024 18:04:50 +0800
Organization: A noiseless patient Spider
Lines: 140
Message-ID: <4ffeda4ce7116f70754bcfcaee87cb729081fac3.camel@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Injection-Date: Sun, 15 Dec 2024 11:04:51 +0100 (CET)
Injection-Info: dont-email.me; posting-host="71e0e87351af83703b92c0f021272a68";
	logging-data="562088"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19t7aRlwgIJTNuIdIKXgVf7"
User-Agent: Evolution 3.50.2 (3.50.2-1.fc39)
Cancel-Lock: sha1:b4EdVwEa/xk7cHZZK5cOCUjVnK0=

I had headache whenevr I think about graphics in C++. Why C++ does not prov=
ide
a graphics library (lots complaint about this), not even a simplest one for=
=C2=A0
demonstrating its 'power' of C++ itself? Then, I suddenly realized that the=
=C2=A0
minimal answer is already there because the resolution of modern text scree=
n=C2=A0
(emulator) is barely enough (width can be >320).
       =20
---------- t_txtwind.cpp
#include <Wy.stdio.h>
#include <Wy.math.h>
#include <Wy.signal.h>
#include <CSCall/Array2D.h>
#include <CSCall/ioctl.h>

using namespace Wy;

typedef float FuncValue;
constexpr FuncValue FMinX=3D-5;
constexpr FuncValue FMaxX=3D 5;

FuncValue func(FuncValue x) {  // function to plot
 return ::sin(x);
};

//---------------------------------------------
typedef char Pixel;
Array2D<Pixel> txtwin;

// [Syn] Plot func(FuncValue) in current terminal (text window)=20
//       (window size can change)
//
void plot() {
 constexpr Pixel BgChar=3D' ';
 constexpr Pixel FgChar=3D'x';
 Errno r;
 WinSize ws;

 if((r=3Dgetwinsize(cout,ws))!=3DOk) {
   WY_THROW(r);
 }

 Array<FuncValue> varr;
 FuncValue dx=3D (FMaxX-FMinX)/ws.col();
 FuncValue ymin=3D Limits<FuncValue>::Max;
 FuncValue ymax=3D Limits<FuncValue>::Min;
 for(FuncValue x=3DFMinX; x<=3DFMaxX; x+=3Ddx) {
   FuncValue v=3D func(x);
   if(v>ymax) {
     ymax=3Dv;
   }
   if(v<ymin) {
     ymin=3Dv;
   }
   varr << func(x);
 }

 if((r=3Dtxtwin.reset(ws.col(),ws.row()))!=3DOk) {
   WY_THROW(r);
 }
 ::memset(txtwin.header()._data(),BgChar,txtwin.size());
=20
 for(size_t x=3D0; x<varr.size(); ++x) {
   unsigned int y=3D((varr[x]-ymin)/(ymax-ymin))*(txtwin.height()-1);
   txtwin.header().write_point(x,y,FgChar);
 }
 for(size_t y=3D0; y<txtwin.height(); ++y) {
   cout << StrSeg(&txtwin.header().at(0,y), txtwin.header().width()) << WY_=
ENDL;
 }
};

void winch_handler(int, ::siginfo_t*, void*) {
 SignaledContext sigcontext;
 plot();
};

int main(int argc, const char* argv[])
try {
 Errno r;
 if((r=3Dsig_setact(SIGWINCH, SigAct(winch_handler) ))!=3DOk) {
   WY_THROW(r);
 }

 plot();
 for(int wcnt=3D0; wcnt<100; ++wcnt) { // wait for 100 win changes or Cntl-=
C
   ::pause();
 }
 cout << "OK" WY_ENDL;
 return 0;
}
catch(const Errno& e) {
 cerr << wrd(e) << WY_ENDL;
 return -1;  // e.c_errno();
}
catch(...) {
 cerr << "main() caught(...)" WY_ENDL;
 throw;
};
-------------------------

[]$ g++ t_txtwind.cpp -lwy
[]$ ./a.out             =20
                         xxxxxx                                            =
xxxxx
                        x      x                                          x
                       x        x                                        x
                      x          x                                      x
                     x            x                                    x
                    x
                                   x                                  x
                   x                x                                x
                  x          =20
                                     x                              x     =
=20
                 x                    x                 =20
                                                                   x
                x                      x                          x
  =20
               x                        x                        x
                             =20
              x                          x                      x
             x                                          =20
                                          x                    x
            x                              x
                                                              x  =20
           x                                x                x
          x                                  x
                                                            x
         x                                    x            x
        x                                      x          x
       x                                        x        x
      x                                          x      x
xx xxx                                            xxxxxx
  x                          =20