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