Plot a Sinewave to the Console

vegaseat 1 Tallied Votes 4K Views Share

An example how to plot the function y = sin(x) using the WinApi function SetPixel(). The plot is centered along a line at pixel y = 200. Add an x-axis and a couple of tickmarks and it could look impressive.

// plot a sinewave to the console window (cmd window)
// link with GDI32.lib or using Dev-C++ link libgdi32.a via
// Project>>Project Options>>Parameters>>Add Lib>>libgdi32.a
// this is a Windows Console Application   vegaseat  06mar2005

#include <cstdio>
#include <cmath>
#include <windows.h>

int main(void)
{
  int x, y;
  COLORREF yellow = RGB(255,255,0);
  COLORREF lightblue = RGB(173,216,230);
  
  // make sure the names match
  SetConsoleTitle("ConGraphics");
  HWND hWnd = FindWindow(NULL, "ConGraphics");

  HDC hDC = GetDC(hWnd);
  
  // draw a yellow sine curve
  for(x = 0; x < 700; x++)
  {
    // center at y = 200 pixels
    y = (int)(sin(x/100.0)*100 + 200);
    SetPixel(hDC, x, y, yellow);
  }
    
  // draw center line
  for(x = 0; x < 700; x++)
  {
    SetPixel(hDC, x, 200, lightblue);   
  }
  
  ReleaseDC(hWnd, hDC);
  DeleteDC(hDC);

  getchar();  // wait
  return 0;
}

Dani AI

Generated

Nice demo idea, vegaseat. A couple of gotchas that tripped up vallerie joy and others are worth calling out:

  • If you call SetPixel, you must link against Gdi32 and draw to a valid HDC (for example, from a real window’s paint routine). Linking errors like “undefined reference to __imp_SetPixel” usually mean the GDI library was not linked. In Visual Studio add Gdi32.lib to Linker -> Input, and with MinGW link with -lgdi32. Also note SetPixel requires a device context and will not persist reliably if you try to draw directly on the console window. (learn.microsoft.com, sourceforge.net)

  • kal_crazy’s Unicode/MBCS tip is on point. Alternatively, keep the project in Unicode and use the generic-text macros (TEXT(), TCHAR, etc.) so your code compiles cleanly in either setting. New Windows code should prefer Unicode. (learn.microsoft.com)

If you truly want a console-only plot (no GDI, no linking headaches), render an ASCII sine in a text buffer and print it:

#include <iostream>
#include <vector>
#include <cmath>

int main() {
    const int width = 80, height = 25;
    const double twoPi = 6.283185307179586, cycles = 2.0;
    std::vector<std::string> buf(height, std::string(width, ' '));
    const int mid = height / 2, amp = height / 2 - 2;

    for (int x = 0; x < width; ++x) {
        double t = cycles * twoPi * x / (width - 1);
        int y = mid - static_cast<int>(std::lround(amp * std::sin(t)));
        if (0 <= y && y < height) buf[y][x] = '*';
    }
    for (int x = 0; x < width; ++x) buf[mid][x] = (buf[mid][x] == '*') ? '*' : '-';
    for (const auto& row : buf) std::cout << row << "\n";
}

Sticking with GDI? For performance, avoid per-pixel calls where possible; draw a Polyline, or at least prefer SetPixelV over SetPixel. It is a faster variant and avoids returning the previous color. (learn.microsoft.com)

For completeness, if you want to manipulate the console screen buffer directly (characters and colors), look at WriteConsoleOutputCharacter and friends. (learn.microsoft.com)

u_rangith 0 Newbie Poster

two warnings on the program,
plase check that
<<snip>> this is my e mail id
i am learning C++ so if your have any aother solution please sent to my email
i am interest in learning C++ programing Language
so please if you have any learning codes please sent to my e mail id
THanking your
U.Rangith

vallerie joy 0 Newbie Poster

When I pasted your work on my C++ program, It said;"undefined reference" What should I do? I don't know your ouput yet, so I have to make sure that this one works. This is for the sake of my grade. Please help me.

kal_crazy 13 Junior Poster

I used Visual Studio 2012. And the following solved the problem.
-Go to project properties. Then in the project defaults change Character Set from Unicode to Multi-Byte.

jack.zgx 0 Newbie Poster

What can i do for you?

jack.zgx 0 Newbie Poster

I want to join you ,and i wondered hhow to learn C++ well.please help me,thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.