C + +

Discussion in 'Programmer's Corner' started by isha, Jun 1, 2006.

  1. isha

    Thread Starter Active Member

    Sep 16, 2005
    36
    0

    how can we draw sine wave in C++
    1. without using graphics
    and
    2. with graphics
    (i need source code )
     
  2. BladeSabre

    Senior Member

    Aug 11, 2005
    105
    0
    1. What do you mean by "draw a sine wave ... without using graphics"?

    2. C++ does not have its own graphics, and the code varies wildly depending on whether you're using the operating system's routines or some cross-platform library. Have you used any kind of graphics before? What did you use?
     
  3. Papabravo

    Expert

    Feb 24, 2006
    10,163
    1,796
    We used to use FORTRAN programs on mainframe comnputers to plot graphs on line printers. Some SPICE programs still do this. It is just a matter of constructing the strings with blanks and asterisks and writing them to an output stream.
     
  4. isha

    Thread Starter Active Member

    Sep 16, 2005
    36
    0


    1. using graphics mean that
    in program including the header file
    " #include <graphics.h> "

    2. with out graphics mean that
    using loop to
    display a character in sine wave form
     
  5. BladeSabre

    Senior Member

    Aug 11, 2005
    105
    0
    OK, so the answer to my second question is that you're using "graphics.h" for your graphics. I know roughly what that is, though I can't help you with code for it.

    "Using loop to display a character in sine wave form" does not make any more sense to me than before. Are you intending to draw using a grid of characters, like what Papabravo suggested?
     
  6. isha

    Thread Starter Active Member

    Sep 16, 2005
    36
    0

    diplay character in sine wave form
    or grid of characters
     
  7. BladeSabre

    Senior Member

    Aug 11, 2005
    105
    0
    First, a little friendly feedback:

    1. Writing in large italic makes your posts hard to read. And it looks like you're shouting (or attention-seeking).

    2. We need more details of the problem if we're going to help you. You can help us to help you by telling us those details without being asked. Having to drag them out of you 10 words at a time is tiring. Most people won't bother with it, and I'm not going to bother any more after this post.

    3. Asking people to write code for you will not usually work - particularly if you can't explain what you want. This time is a rare exception. (I'm new still, and the problem looked like an interesting one for me to practise on.)



    That said, I had a quick bash at it. I have no idea if this is anything like what you want, because you didn't explain it very well. So, I defined the problem the way I liked it. (And this is C, by the way. But I think it will still work in C++.)

    Code ( (Unknown Language)):
    1. #include <math.h>
    2. #include <stdio.h>
    3.  
    4. #define ROWS 22
    5. #define COLS 72
    6. #define ZERO_LINE_ROW 10
    7.  
    8. #define VALUE_PER_ROW 0.1
    9. #define RADS_PER_COL 0.5
    10. #define STARTING_RADS 2.0
    11.  
    12. void drawwave (char grid[][COLS+1]) {
    13.   int row, col;
    14.   double x, y;
    15.   for (row = 0; row < ROWS; row++) {
    16.    for (col = 0; col < COLS; col++) {
    17.      grid[row][col] = ' ';
    18.    }
    19.    grid[row][COLS] = '\0';
    20.   }
    21.  
    22.   for (col = 0; col < COLS; col++) {
    23.    x = STARTING_RADS + (RADS_PER_COL * col);
    24.    y = sin(x);
    25.    row = ZERO_LINE_ROW - (int) (y / VALUE_PER_ROW);
    26.    if (row >= 0 && row < ROWS) {
    27.      grid[row][col] = '*';
    28.    }
    29.   }
    30. }
    31.  
    32. int main (int argc, char*argv[]) {
    33.   char grid[ROWS][COLS+1];
    34.   int row;
    35.   drawwave(grid);
    36.   for (row = 0; row < ROWS; row++) {
    37.    puts (grid[row]);
    38.   }
    39.   getchar();
    40.   return 0;
    41. }
     
Loading...