Connecting Tech Pros Worldwide Forums | Help | Site Map

recursively draw the line

Matt
Guest
 
Posts: n/a
#1: Nov 13 '05
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

Ben Pfaff
Guest
 
Posts: n/a
#2: Nov 13 '05

re: recursively draw the line


jrefactors@hotmail.com (Matt) writes:
[color=blue]
> I am writing a recursive program to draw the lines recursively, given the
> range[min,max] and number of intervals (n) between the range.[/color]

C has no facilities for drawing lines.
[color=blue]
> The problem is I don't know how to draw the line in point 0.375, as you see below.
> Please advise! Thanks![/color]
[color=blue]
> #include <iostream>[/color]

That's not C.
[color=blue]
> The program output:
> 0.5
> 0.25
> 0.125[/color]

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
Jens.Toerring@physik.fu-berlin.de
Guest
 
Posts: n/a
#3: Nov 13 '05

re: recursively draw the line


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

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>
[color=blue]
> void draw(double min, double max, int n);[/color]
[color=blue]
> int main()
> { draw(0,1,8);
> }[/color]

You forgot to have main() return an int...
[color=blue]
> void draw(double min, double max, int n)
> { if (n != 1)
> { double mid = (max - min)/2;[/color]

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).
[color=blue]
> cout << mid << endl;[/color]

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

printf( "%f\n", mid );
[color=blue]
> draw(min, mid, n/2);[/color]

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 );
[color=blue]
> }
> }[/color]
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| Jens.Toerring@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
Richard Heathfield
Guest
 
Posts: n/a
#4: Nov 13 '05

re: recursively draw the line


Matt wrote:
[color=blue]
> 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;[/color]

Try comp.lang.c++

--
Richard Heathfield : binary@eton.powernet.co.uk
"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
Barry Schwarz
Guest
 
Posts: n/a
#5: Nov 13 '05

re: recursively draw the line


On 5 Oct 2003 10:44:13 -0700, jrefactors@hotmail.com (Matt) wrote:
[color=blue]
>I am writing a recursive program to draw the lines recursively, given the
>range[min,max] and number of intervals (n) between the range.[/color]
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>>
Julian V. Noble
Guest
 
Posts: n/a
#6: Nov 13 '05

re: recursively draw the line


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

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


--
Julian V. Noble
Professor Emeritus of Physics
jvn@lessspamformother.virginia.edu
^^^^^^^^^^^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/

"Science knows only one commandment: contribute to science."
-- Bertolt Brecht, "Galileo".
Closed Thread


Similar C / C++ bytes