Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #121206
| From | wij <wyniijj5@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | 'Graphics' of libwy |
| Date | 2024-12-15 18:04 +0800 |
| Organization | A noiseless patient Spider |
| Message-ID | <4ffeda4ce7116f70754bcfcaee87cb729081fac3.camel@gmail.com> (permalink) |
I had headache whenevr I think about graphics in C++. Why C++ does not provide
a graphics library (lots complaint about this), not even a simplest one for
demonstrating its 'power' of C++ itself? Then, I suddenly realized that the
minimal answer is already there because the resolution of modern text screen
(emulator) is barely enough (width can be >320).
---------- 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=-5;
constexpr FuncValue FMaxX= 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)
// (window size can change)
//
void plot() {
constexpr Pixel BgChar=' ';
constexpr Pixel FgChar='x';
Errno r;
WinSize ws;
if((r=getwinsize(cout,ws))!=Ok) {
WY_THROW(r);
}
Array<FuncValue> varr;
FuncValue dx= (FMaxX-FMinX)/ws.col();
FuncValue ymin= Limits<FuncValue>::Max;
FuncValue ymax= Limits<FuncValue>::Min;
for(FuncValue x=FMinX; x<=FMaxX; x+=dx) {
FuncValue v= func(x);
if(v>ymax) {
ymax=v;
}
if(v<ymin) {
ymin=v;
}
varr << func(x);
}
if((r=txtwin.reset(ws.col(),ws.row()))!=Ok) {
WY_THROW(r);
}
::memset(txtwin.header()._data(),BgChar,txtwin.size());
for(size_t x=0; x<varr.size(); ++x) {
unsigned int y=((varr[x]-ymin)/(ymax-ymin))*(txtwin.height()-1);
txtwin.header().write_point(x,y,FgChar);
}
for(size_t y=0; 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=sig_setact(SIGWINCH, SigAct(winch_handler) ))!=Ok) {
WY_THROW(r);
}
plot();
for(int wcnt=0; 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
xxxxxx xxxxx
x x x
x x x
x x x
x x x
x
x x
x x x
x
x x
x x
x
x x x
x x x
x x x
x
x x
x x
x
x x x
x x
x
x x x
x x x
x x x
x x x
xx xxx xxxxxx
x
Back to comp.lang.c++ | Previous | Next — Next in thread | Find similar | Unroll thread
'Graphics' of libwy wij <wyniijj5@gmail.com> - 2024-12-15 18:04 +0800
Re: 'Graphics' of libwy Bonita Montero <Bonita.Montero@gmail.com> - 2024-12-15 13:43 +0100
Re: 'Graphics' of libwy wij <wyniijj5@gmail.com> - 2024-12-16 06:57 +0800
Re: 'Graphics' of libwy Ross Finlayson <ross.a.finlayson@gmail.com> - 2024-12-15 15:12 -0800
Re: 'Graphics' of libwy David Brown <david.brown@hesbynett.no> - 2024-12-16 10:29 +0100
Re: 'Graphics' of libwy Lynn McGuire <lynnmcguire5@gmail.com> - 2024-12-16 18:47 -0600
Re: 'Graphics' of libwy David Brown <david.brown@hesbynett.no> - 2024-12-17 08:49 +0100
Re: 'Graphics' of libwy Chris Ahlstrom <OFeem1987@teleworm.us> - 2024-12-17 11:54 -0500
Re: 'Graphics' of libwy Michael S <already5chosen@yahoo.com> - 2024-12-17 22:52 +0200
Re: 'Graphics' of libwy David Brown <david.brown@hesbynett.no> - 2024-12-18 08:34 +0100
Re: 'Graphics' of libwy Lynn McGuire <lynnmcguire5@gmail.com> - 2024-12-16 18:41 -0600
Re: 'Graphics' of libwy Paavo Helde <eesnimi@osa.pri.ee> - 2024-12-17 16:38 +0200
Re: 'Graphics' of libwy scott@slp53.sl.home (Scott Lurndal) - 2024-12-17 15:34 +0000
Re: 'Graphics' of libwy Muttley@DastardlyHQ.org - 2024-12-17 16:02 +0000
Re: 'Graphics' of libwy David Brown <david.brown@hesbynett.no> - 2024-12-17 17:30 +0100
Re: 'Graphics' of libwy Paavo Helde <eesnimi@osa.pri.ee> - 2024-12-17 18:53 +0200
Re: 'Graphics' of libwy David Brown <david.brown@hesbynett.no> - 2024-12-18 08:38 +0100
Re: 'Graphics' of libwy Muttley@DastardlyHQ.org - 2024-12-17 16:53 +0000
Re: 'Graphics' of libwy David Brown <david.brown@hesbynett.no> - 2024-12-18 09:09 +0100
Re: 'Graphics' of libwy Muttley@DastardlyHQ.org - 2024-12-18 09:16 +0000
Re: 'Graphics' of libwy David Brown <david.brown@hesbynett.no> - 2024-12-18 13:58 +0100
Re: 'Graphics' of libwy Muttley@DastardlyHQ.org - 2024-12-18 14:07 +0000
Re: 'Graphics' of libwy Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-12-18 13:12 -0800
Re: 'Graphics' of libwy wij <wyniijj5@gmail.com> - 2024-12-19 10:08 +0800
Re: 'Graphics' of libwy Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-12-18 18:29 -0800
Re: 'Graphics' of libwy wij <wyniijj5@gmail.com> - 2024-12-19 10:51 +0800
Re: 'Graphics' of libwy Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-12-18 20:14 -0800
Re: 'Graphics' of libwy Ross Finlayson <ross.a.finlayson@gmail.com> - 2024-12-19 16:21 -0800
Re: 'Graphics' of libwy Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-12-19 17:07 -0800
Re: 'Graphics' of libwy Ross Finlayson <ross.a.finlayson@gmail.com> - 2024-12-19 17:38 -0800
Re: 'Graphics' of libwy Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-12-19 18:09 -0800
Re: 'Graphics' of libwy wij <wyniijj5@gmail.com> - 2024-12-20 11:10 +0800
Re: 'Graphics' of libwy wij <wyniijj5@gmail.com> - 2024-12-23 02:50 +0800
Re: 'Graphics' of libwy "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-12-22 15:26 -0800
Re: 'Graphics' of libwy wij <wyniijj5@gmail.com> - 2024-12-23 14:32 +0800
Re: 'Graphics' of libwy Muttley@dastardlyhq.com - 2024-12-23 09:59 +0000
Re: 'Graphics' of libwy wij <wyniijj5@gmail.com> - 2024-12-25 23:51 +0800
Re: 'Graphics' of libwy Ross Finlayson <ross.a.finlayson@gmail.com> - 2024-12-19 19:53 -0800
Re: 'Graphics' of libwy Lynn McGuire <lynnmcguire5@gmail.com> - 2024-12-19 21:57 -0600
Re: 'Graphics' of libwy "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-12-19 21:01 -0800
Re: 'Graphics' of libwy Ross Finlayson <ross.a.finlayson@gmail.com> - 2024-12-19 21:39 -0800
Re: 'Graphics' of libwy "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-12-19 21:48 -0800
Re: 'Graphics' of libwy scott@slp53.sl.home (Scott Lurndal) - 2024-12-17 17:08 +0000
Re: 'Graphics' of libwy "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-12-17 12:32 -0800
csiph-web