How to Graph a Sine Wave in C++

Computer programming is complex, but if you're well-versed in a programming language such as C++, the possible applications are virtually infinite. Even Microsoft Windows runs on C++. Graphing a sine wave using C++ is one exercise that will help familiarize you with the language. In trigonometry, a sine curve is represented by the formula f (x) = a*sin (bx + c) and has a repeating, undulating, trough-to-peak pattern.

Instructions

    • 1

      Open the code editor.

    • 2

      Type in, "#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)."

    • 3

      Add the code, "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)."

    • 4

      Finish by typing, "// draw center line

      for(x = 0; x < 700; x++)

      {

      SetPixel(hDC, x, 200, lightblue);

      }

      ReleaseDC(hWnd, hDC);

      DeleteDC(hDC);

      getchar(); // wait

      return 0."

Related Searches:

References

Comments

Related Ads

Featured