0

I was making a program for displaying a sine graph in C, here is a small part of program

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  static int cxClient, cyClient;
  HDC hdc;
  int i;
  PAINTSTRUCT ps;
  POINT apt[NUM];
  switch (message)
  {
  case WM_SIZE:
    cXClient = LOWORD(lParam);
    cyClient = HIWORD(lParam);
    return 0;
  case WM_PAINT:
    hdc = BeginPaint(hwnd, &ps);
    MoveToEx(hdc, 0, cyClient / 2, 0);
    LineTo(hdc, cxClient, cyClient / 2);
    for (i = 0; i < NUM; i++)
    {
      apt[i].x = i * cxClient / NUM;
      apt[i].y = (int) (height / 2 * (1 - sin(TWOPI * i / NUM)));
    }
    Polyline(hdc, apt, NUM);
    return 0;
  case WM_DESTROY:
    PostQuitMessage(0);
    return 0;
  }
  return DefWindowProc(hwnd, message, wParam, lParam);
}

where NUM is set to 1000 and cxClient is the width of client area iin low word and cyClient is height of client area in high word,TWOPI is defined globally as (2*3.1459)

My problem with the program is

1.I cannot understand the line apt[i].x and apt[i].y (including the sine form).

2.When I defined TWOPI as #define TWOPI (2*(22/7)) instead of(#define TWOPI (2*3.1459)) then the graph was square shaped , but both things are same ,instead this is more accurate so why did this happened.

These things are not explained in the book so I am asking you.

alk
  • 69,737
  • 10
  • 105
  • 255
Freedom911
  • 610
  • 2
  • 12
  • 26
  • 4
    To answer your second question (22/7) is 3 (integer division), (22.0 / 7.0) is an approximation of Pi (floating point division). – Jeff Foster Jun 27 '13 at 07:48
  • A better approximation to π is 3.14159 (3.1459 is badly erroneous, more than 0.13% too large), but you could use 355.0 / 113.0 too (it is a little more accurate than 3.14159). – Jonathan Leffler Jun 27 '13 at 07:53
  • 3
    FYI you [can have `M_PI` defined by ``](http://msdn.microsoft.com/en-us/library/4hwaceh6.aspx) – Roman R. Jun 27 '13 at 08:10

1 Answers1

1

I'll begin with 2, which is basically a duplicate of What is the behavior of integer division? .

In short: division operator takes both operands and returns a value in a type that is big and precise enough to hold any of the operands. Therefore, integer division will always yield integer result because any integer is precise enough to hold an integer. As you are aware, integer division may result in a real value, therefore at least one of the operands must be real. For example #define TWOPI (2*(22.0/7)) // implicit conversion or #define TWOPI (2*((float)22/7) // explicit conversion. This explains square shaped graph

  1. C offers two types of container variables: array and struct. Arrays are collections of values having the same type and structs are collections of values having arbitrary type. Members of array are accessed using [] operator and providing index of an element (array[0] for element offseted by 0, yielding the first element). Structs are accessed using . operator (or ->), providing the name of an element.
    So apt[i] basically means "take an element from array apt offseted by i", or in other words "tahe an i+1 th element from array named apt". Adding a .x means "okay, because element of apt contains other container take an element named x", which conveniently means x coordinate of the point in your sine wave.
    The same with .y, just there we have a mathematical representation of a sine function, with variable scale.

edit [explained the math]:
1. 1-sin(). Sine function has values in range R = [-1;1], thus 1-R = [2;0]. This results in function being inverted (no "visual" effect on a sine) and shifted up to positive values. In practice this allows to add a height multiplier without dealing with negative values. i.e. the function only "grows up" from a a zero point and does not "expand" in both direction from central point.
2. what's inside of sin(). You are iterating i from 0 to NUM. Inside of sin() you have something multiplied by i/NUM. If you analysed that on paper you would get that in every iteration TWOPI is multiplied by 1/10, 2/10, 3/10, etc.. sin() expects its argument in radians and a "full" sine wave (one period) is available in region [0; 2π]. So multiplication by 2 PI basically means that you are representing one period of a sine wave. Changing that to 4 PI would result in 2 periods of a sine wave.

Community
  • 1
  • 1
friendzis
  • 799
  • 6
  • 17
  • I cannot understand the part 1 - sin(TWOPI * i / NUM).Why are we subtracting 1 and why are we using 2pi instead of pi.Please,please can anybody help – Freedom911 Jun 28 '13 at 11:07