Results 1 to 4 of 4

Thread: question over sine wave

  1. #1
    Junior Member Newbie
    Join Date
    Jul 2009
    Posts
    2

    question over sine wave

    I am a newbie of OpenGL, here is my code desired to draw a sine wave in [0,2*pi), but there is only a line between (0,0)and(0,1), any advice is appreciated!

    here is my code

    //************************************************//
    #include <glut.h>
    #define PI 3.14159265 // _USE_MATH_DEFINES
    #include <math.h>
    #include <iostream>
    #define SIZE 2000

    void drawline(float x1, float y1, float x2, float y2);
    void sinewave();

    int main(int argc, char ** argv)
    { glutInit(&amp;argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
    glutInitWindowSize(400,400);
    glutInitWindowPosition(200,200);

    glutCreateWindow(" sinusoidal wave ");
    glutDisplayFunc(sinewave);
    glutMainLoop();
    return 0;
    }

    void sinewave()
    {
    float x1, x2, y1, y2;
    for (int i=0;i<SIZE;i++)
    {
    x1=i/SIZE;x2=(i+1)/SIZE;
    y1=sin(x1*2*PI);
    y2=sin(x2*2*PI);
    drawline(x1,y1,x2,y2);
    glutSwapBuffers();
    }

    }

    void drawline(float x1, float y1, float x2, float y2)
    {
    glBegin(GL_LINE_STRIP); // LINE_STRIP | POINTS
    glColor3f(1.0,0,0);
    glVertex2f(x1,y1);
    glVertex2f(x2,y2);
    glEnd();
    }

  2. #2
    Member Contributor
    Join Date
    Mar 2008
    Location
    Singapore
    Posts
    67

    Re: question over sine wave

    you are almost there.

    Just change this line:

    x1=i/SIZE;x2=(i+1)/SIZE;


    to something like:

    x1=(float)i/SIZE;
    x2=((float)i+1.0)/SIZE;


    You get a straight line because you are dividing integers.

  3. #3
    Member Newbie
    Join Date
    Oct 2009
    Posts
    34

    Re: question over sine wave

    Hi,

    The problem is with integer division:

    x1=i/SIZE;x2=(i+1)/SIZE
    will always give you 0 or 1 for x0 and x2. You have to cast to float (either i or SIZE or both).

  4. #4
    Junior Member Newbie
    Join Date
    Jul 2009
    Posts
    2

    Re: question over sine wave

    thanks a lot, I thought I was missing some window-configuration lines.

Similar Threads

  1. [GLSL] no matching overloaded function for cosine and sine
    By Matteo_ in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 07-16-2016, 04:51 AM
  2. Sine/Cosine wave for sunrise and sunset
    By chap19150 in forum OpenGL: General
    Replies: 1
    Last Post: 05-18-2012, 02:14 PM
  3. About sine wave animation
    By shirleydd in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 09-13-2010, 08:14 AM
  4. Are sine tables too 1990's or...
    By MarkS in forum OpenGL: General
    Replies: 9
    Last Post: 09-07-2009, 05:46 AM
  5. Problems Using and Implementing Sine and Cosine
    By NewbieGP in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 10-28-2003, 11:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean