Thread: Graphing a sine/cosine/tan curves in C???

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    13

    Question Graphing a sine/cosine/tan curves in C???

    http://www.sci.brooklyn.cuny.edu/~sd.../Program6.html

    the link above are specifications to making a so called "simple" program, but how would this be done? especially the graphing of sin, cos and tan?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    graph(sin,0.0,10.0,20);
    You create a loop that goes from 0.0 to 10.0 (the example in the link I think got it wrong by saying 0.0 to 20.0) in increments of (hi-lo)/(num of points) so:

    Code:
    void graph( double (*func)(double),double lo,double hi, int number )
    {
        double pt;
        double inc = (hi-lo)/number;
    
        for( pt = lo; lo <= hi; pt += inc )
        {
            /* Call the function for the given pt value, you would need to
            do something with the return value, i.e. store it in an array perhaps
            for later use */
            func(pt);
        }
    
    }
    Once you've got all the values stored in an array, you need to normalize them based on the height of the screen display. Then it is a simple matter of going through the stored values in the array and outputting a symbol at the appropriate height based on the normalized value.
    Last edited by hk_mp5kpdw; 12-13-2005 at 06:28 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    13
    how would you print an asterisk based on the height?

  4. #4
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    probably create a 2d char array:-

    Code:
    [ ][ ][ ][ ][ ][ ][ ]
    [ ][ ][ ][ ][ ][ ][ ]
    [ ][ ][ ][ ][ ][ ][ ]
    [ ][ ][ ][ ][ ][ ][ ]
    [ ][ ][ ][*][ ][ ][ ]
    [ ][*][*][ ][*][*][ ]
    [ ][*][ ][ ][ ][*][ ]
    Then print out the 2d array using a nested for loop.

    How you gets the stars into the array will be of course your main problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bezier Curves where y = f(x)
    By thetinman in forum Game Programming
    Replies: 11
    Last Post: 12-27-2008, 10:21 AM
  2. Default intersection of two curves on unit sphere
    By Zeeshan in forum Game Programming
    Replies: 1
    Last Post: 06-23-2008, 11:30 AM
  3. Open source graphing / plotting library?
    By sysop in forum C Programming
    Replies: 2
    Last Post: 06-05-2007, 12:20 PM
  4. Graphing Calculator..
    By willc0de4food in forum Windows Programming
    Replies: 12
    Last Post: 09-26-2005, 06:57 PM
  5. Graphing Calculators
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 11-17-2003, 08:23 AM