Plot a Sinewave to the Console

vegaseat

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.

2,047 Views
About the Author

Scientist

// 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;
}

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

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.

What can i do for you?

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 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.