recursively draw the line

Discussion in 'C Programming' started by Matt, Oct 5, 2003.

  1. Matt

    Matt Guest

    I am writing a recursive program to draw the lines recursively, given the
    range[min,max] and number of intervals (n) between the range.

    The problem is I don't know how to draw the line in point 0.375, as you see below.
    Please advise! Thanks!


    #include <iostream>
    using namespace std;

    void draw(double min, double max, int n);

    int main()
    { draw(0,1,8);
    }

    void draw(double min, double max, int n)
    { if (n != 1)
    { double mid = (max - min)/2;
    cout << mid << endl;
    draw(min, mid, n/2);
    }
    }


    The program output:
    0.5
    0.25
    0.125

    Here's the expected output:
    0.5
    0.25
    0.125
    0.375
    0.75
    0.625
    0.875
     
    Matt, Oct 5, 2003
    #1
    1. Advertising

  2. Matt

    Ben Pfaff Guest

    (Matt) writes:

    > I am writing a recursive program to draw the lines recursively, given the
    > range[min,max] and number of intervals (n) between the range.


    C has no facilities for drawing lines.

    > The problem is I don't know how to draw the line in point 0.375, as you see below.
    > Please advise! Thanks!


    > #include <iostream>


    That's not C.

    > The program output:
    > 0.5
    > 0.25
    > 0.125


    That's not a line. Those are numbers.
    --
    "What is appropriate for the master is not appropriate for the novice.
    You must understand the Tao before transcending structure."
    --The Tao of Programming
     
    Ben Pfaff, Oct 5, 2003
    #2
    1. Advertising

  3. Matt

    -berlin.de Guest

    Matt <> wrote:
    > I am writing a recursive program to draw the lines recursively, given the
    > range[min,max] and number of intervals (n) between the range.


    > The problem is I don't know how to draw the line in point 0.375, as you see below.
    > Please advise! Thanks!


    > #include <iostream>
    > using namespace std;


    Sorry, but this a C newsgroup, there's also a C++ group in case you have
    problems with C++. So let's replace this with

    #include <stdio.h>

    > void draw(double min, double max, int n);


    > int main()
    > { draw(0,1,8);
    > }


    You forgot to have main() return an int...

    > void draw(double min, double max, int n)
    > { if (n != 1)
    > { double mid = (max - min)/2;


    You need to add the starting point of the interval here:

    double mid = ( max - min ) / 2 + min;

    (the mid-point between e.g. 4 and 5 is 4.5 and not just 0.5).

    > cout << mid << endl;


    Sorry, this won't work in C, use instead

    printf( "%f\n", mid );

    > draw(min, mid, n/2);


    Now you're "drawing" the lower half of the interval but you forget to
    also "draw" the upper half. You need an additional call:

    draw( mid, max, n / 2 );

    > }
    > }

    Regards, Jens
    --
    _ _____ _____
    | ||_ _||_ _| -berlin.de
    _ | | | | | |
    | |_| | | | | | http://www.physik.fu-berlin.de/~toerring
    \___/ens|_|homs|_|oerring
     
    -berlin.de, Oct 5, 2003
    #3
  4. Matt wrote:

    > I am writing a recursive program to draw the lines recursively, given the
    > range[min,max] and number of intervals (n) between the range.
    >
    > The problem is I don't know how to draw the line in point 0.375, as you
    > see below. Please advise! Thanks!
    >
    >
    > #include <iostream>
    > using namespace std;


    Try comp.lang.c++

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
     
    Richard Heathfield, Oct 5, 2003
    #4
  5. On 5 Oct 2003 10:44:13 -0700, (Matt) wrote:

    >I am writing a recursive program to draw the lines recursively, given the
    >range[min,max] and number of intervals (n) between the range.

    snip

    Answered in alt.math. Please don't post the same message under
    different subjects to multiple groups. If you must post to more than
    one group, put all the group names on one message.


    <<Remove the del for email>>
     
    Barry Schwarz, Oct 5, 2003
    #5
  6. Matt wrote:
    >
    > I am writing a recursive program to draw the lines recursively, given the
    > range[min,max] and number of intervals (n) between the range.
    >


    Search under Bresenham's Algorithm. Can also be used to draw ellipses ;-)


    --
    Julian V. Noble
    Professor Emeritus of Physics

    ^^^^^^^^^^^^^^^^^^
    http://galileo.phys.virginia.edu/~jvn/

    "Science knows only one commandment: contribute to science."
    -- Bertolt Brecht, "Galileo".
     
    Julian V. Noble, Oct 6, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QUNhdW50ZXI=?=

    draw line on image

    =?Utf-8?B?QUNhdW50ZXI=?=, Feb 22, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    476
    Peter Rilling
    Feb 22, 2005
  2. steve smith
    Replies:
    2
    Views:
    724
    Jim Sculley
    Jul 13, 2003
  3. Rick
    Replies:
    3
    Views:
    1,475
    Sandip Chitale
    Sep 30, 2003
  4. Yasir Khalifa
    Replies:
    3
    Views:
    917
    Thomas Weidenfeller
    Jun 29, 2004
  5. Matt
    Replies:
    2
    Views:
    564
    Pete Becker
    Oct 5, 2003
Loading...

Share This Page