Risifo
June 6th, 2002, 07:12 AM
Hey, i'm having a big problem working with Recursion in computer math, my teacher don't know how to solve it so hopefully one of you will. Here is what I have to do, it's called Cantor Dust, I give it 4 points, later to be user defined and then it draws it like this, or should at least
---------------
---- ----
- - - -
(Note spacing not right, supposed to cut it in thirds and what not)
Here is my code, note this is the first time I've used recursion. It uses CMU Graphics to draw the lines.
#include <cmugraphics.h>
#include <iostream.h>
int line(window &win,int p1,int p2,int p4,int y1,int y2);
int main()
{
window win(800,600,0,0);
int p1=50,p2=243,p4=679, // The Individual Points
y1=10,y2=10; // X and Y Points on Line
win.ChangeTitle("Cantor Dust");
win.SetPen(BLACK);
line(win,p1,p2,p4,y1,y2);
return 0;
}
int line(window &win,int p1,int p2,int p4,int y1,int y2)
{
int temp=p4;
if(p4 < p1)
return 1;
else
{
return line(win,p1,p2=p1+temp)/3,p4,y1+5,y2+5);
}
}
I'm pretty know I shouldn't increment the y values each time the function loops and should regulate it it to how much, the power of 2 to the number of rows are going down, because that yields how many lines should be drawn on that row, but can't really figure it out, hopefully one of you can help. Thanks.
---------------
---- ----
- - - -
(Note spacing not right, supposed to cut it in thirds and what not)
Here is my code, note this is the first time I've used recursion. It uses CMU Graphics to draw the lines.
#include <cmugraphics.h>
#include <iostream.h>
int line(window &win,int p1,int p2,int p4,int y1,int y2);
int main()
{
window win(800,600,0,0);
int p1=50,p2=243,p4=679, // The Individual Points
y1=10,y2=10; // X and Y Points on Line
win.ChangeTitle("Cantor Dust");
win.SetPen(BLACK);
line(win,p1,p2,p4,y1,y2);
return 0;
}
int line(window &win,int p1,int p2,int p4,int y1,int y2)
{
int temp=p4;
if(p4 < p1)
return 1;
else
{
return line(win,p1,p2=p1+temp)/3,p4,y1+5,y2+5);
}
}
I'm pretty know I shouldn't increment the y values each time the function loops and should regulate it it to how much, the power of 2 to the number of rows are going down, because that yields how many lines should be drawn on that row, but can't really figure it out, hopefully one of you can help. Thanks.