Keshan Pillay

Greenhorn

Posts: 29

posted 9 years ago

Hey I hope everyone is having a good evening/day so far. I was wondering if anyone could please point me in the right direction as to how I could write a function for this?

Many thanks,

as usual, I would appreciate it is someone could explain/help me get to the right answer rather than just giving it to me.

Many thanks,

as usual, I would appreciate it is someone could explain/help me get to the right answer rather than just giving it to me.

Keshan Pillay

Greenhorn

Posts: 29

Norm Radder

Rancher

Posts: 2240

28

posted 9 years ago

In pseudo code:

This is basically a translation from words to pseudo code. Now all that you will need to do is implement drawSimpleLine and rotate.

Considering you will be using Swing for this, you can do the drawing with a JPanel:

Graphics.drawLine will be your friend here.

One more hint: the Graphics object is usually an instance of Graphics2D. This class has methods to translate and rotate it which may be able to help you.

[ September 28, 2008: Message edited by: Rob Prime ]

Some more hints, after I've implemented this:

1) make the length parameter a double. Otherwise the rounding down will cause the figure to shrink horizontally.

2) don't go higher than order 6; on my machine, order 7 takes between half a second and a full second to paint. Order 8 takes around 4 seconds, 9 goes to 13+ seconds and 10 takes over a minute.

[ September 28, 2008: Message edited by: Rob Prime ]

This is basically a translation from words to pseudo code. Now all that you will need to do is implement drawSimpleLine and rotate.

Considering you will be using Swing for this, you can do the drawing with a JPanel:

Graphics.drawLine will be your friend here.

One more hint: the Graphics object is usually an instance of Graphics2D. This class has methods to translate and rotate it which may be able to help you.

[ September 28, 2008: Message edited by: Rob Prime ]

Some more hints, after I've implemented this:

1) make the length parameter a double. Otherwise the rounding down will cause the figure to shrink horizontally.

2) don't go higher than order 6; on my machine, order 7 takes between half a second and a full second to paint. Order 8 takes around 4 seconds, 9 goes to 13+ seconds and 10 takes over a minute.

[ September 28, 2008: Message edited by: Rob Prime ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions

Keshan Pillay

Greenhorn

Posts: 29

Gamini Sirisena

Ranch Hand

Posts: 378

Campbell Ritchie

Marshal

Posts: 56600

172

posted 9 years ago

It's not stack overflow quite as much as the risk of getting quadratic or exponential complexity in some instances. You need to be careful about the design; as long as you keep within reasonable bounds of size you only get stack overflow if you have an infinite recursion; whenever I have tried to get the stack size when it overflows it tends to be about 5000, so you oughtn't to get an overflow in most real-life situations.

Consider the usual Fibonacci algorithm which is exponential, but you can write algorithms quite simply which run in linear time, and there is a logarithmic time Fibonacci algorithm too. It is in Kaldewaij, A.,

Consider the usual Fibonacci algorithm which is exponential, but you can write algorithms quite simply which run in linear time, and there is a logarithmic time Fibonacci algorithm too. It is in Kaldewaij, A.,

*Programming: the derivation of algorithms*, Prentice Hall International series in computer series, Prentice Hall International, 1990. ISBN 0132041081 250 pages, I think page 98-99.
posted 9 years ago

Great book by the way, it's what we started with in University.

Originally posted by Campbell Ritchie:

It is in Kaldewaij, A.,Programming: the derivation of algorithms, Prentice Hall International series in computer series, Prentice Hall International, 1990. ISBN 0132041081 250 pages, I think page 98-99.

Great book by the way, it's what we started with in University.

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions

Ulf Dittmer

Rancher

Posts: 42972

73

posted 9 years ago

There's nothing wrong with recursion per se. Avoiding it no matter what seems like a premature optimization, which needs to be set against the fact that using a comparable iterative version also has a certain overhead in bookkeeping.

Recursion's perception also suffers from there being bad examples for it out there. Fibonacci numbers are a particularly egregious example, since it leads to tree recursion -which is much worse than linear recursion-, while a simple iterative solution is available.

Recursion depth (and the stack problems if it's too deep) is a real concern, but what's possible depends so much individual circumstances that dismissing recursion out of hand seems a bad idea.

Just my 2 cents.

Update: Pretty similar to Campbell's 2 cents, I see now :-)

[ September 29, 2008: Message edited by: Ulf Dittmer ]

Recursion's perception also suffers from there being bad examples for it out there. Fibonacci numbers are a particularly egregious example, since it leads to tree recursion -which is much worse than linear recursion-, while a simple iterative solution is available.

Recursion depth (and the stack problems if it's too deep) is a real concern, but what's possible depends so much individual circumstances that dismissing recursion out of hand seems a bad idea.

Just my 2 cents.

Update: Pretty similar to Campbell's 2 cents, I see now :-)

[ September 29, 2008: Message edited by: Ulf Dittmer ]

Campbell Ritchie

Marshal

Posts: 56600

172

Ulf Dittmer

Rancher

Posts: 42972

73

posted 9 years ago

Even then.

Pence > euro cents > dollar cents

Unfortunately for me, because I like to buy stuff online in the UK regularly. It would be sooo much better for me if the Pound became less valuable than the Euro

As it is, 1 Euro is still somewhere between 0.70 and 0.80 Pounds.

Pence > euro cents > dollar cents

Unfortunately for me, because I like to buy stuff online in the UK regularly. It would be sooo much better for me if the Pound became less valuable than the Euro

As it is, 1 Euro is still somewhere between 0.70 and 0.80 Pounds.

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions

Campbell Ritchie

Marshal

Posts: 56600

172

posted 9 years ago

The Euro is worth a bit more now; according to XE.com 1 Euro is now 0.80 Pounds.

But enough of this currency talk now! Back to recursion!

But enough of this currency talk now! Back to recursion!

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions

It is sorta covered in the JavaRanch Style Guide. |