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
  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);

  getchar();  // wait
  return 0;
11 Years
Discussion Span
Last Post by jack.zgx

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

Edited by Nick Evan: n/a


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.


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.

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.