2

This program uses WinApi function SetPixel() to plot math functions y = sin(x) and y = cos(x) and y = sin(x)*cos(x) along a y-axis centered on the console screen. Make sure that gdi32.lib is included in the libraries to be linked.

// plot y = sin(x), y = cos(x) and y = sin(x)*cos(x) to a console window
// tested with Pelles C free at: http://smorgasbordet.com/pellesc/index.htm
// this C package comes with a great IDE
// a Windows Console Application  by  vegaseat 18mar2005

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <windows.h>

int main(void)
{
  int x, y;
  char str1[] = " yellow is sin(x), red is cos(x), green is sin(x)*cos(x) ";
  COLORREF yellow = RGB(255,255,0);
  COLORREF red    = RGB(255,0,0);
  COLORREF green  = RGB(0,200,0);
  COLORREF blue   = RGB(0,0,255);
  
  // get the window's handle, make sure the names match
  SetConsoleTitle("ConGraphics");
  HWND hWnd = FindWindow(NULL, "ConGraphics");

  // get the handle to Device Context
  HDC hDC = GetDC(hWnd);
  
  TextOut(hDC, 10, 20, str1, strlen(str1));
  
  // 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 a red cosine curve
  for(x = 0; x < 700; x++)
  {
    // center at y = 200 pixels
    y = (int)(cos(x/100.0)*100 + 200);
    SetPixel(hDC, x, y, red);
  }
  
  // draw a green sin(x)*cos(x) curve
  for(x = 0; x < 700; x++)
  {
    // center at y = 200 pixels
    y = (int)(sin(x/100.0) * cos(x/100.0) * 100 + 200);
    SetPixel(hDC, x, y, green);
  }
      
  // draw center line
  for(x = 0; x < 700; x++)
  {
    SetPixel(hDC, x, 200, blue);   
  }
  
  // free up the resources
  ReleaseDC(hWnd, hDC);
  DeleteDC(hDC);

  getchar();  // wait
  return 0;
}
2
Contributors
1
Reply
6
Views
11 Years
Discussion Span
Last Post by brale
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.