[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[QUIZ] Fractals (#125

James Gray

5/25/2007 1:19:00 PM

The three rules of Ruby Quiz:

1. Please do not post any solutions or spoiler discussion for this quiz until
48 hours have passed from the time on this message.

2. Support Ruby Quiz by submitting ideas as often as you can:

http://www.rub...

3. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem helps everyone
on Ruby Talk follow the discussion. Please reply to the original quiz message,
if you can.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

by Drew Olson

When I learned about fractals in high school math class, I immediately found
them fascinating. For those of you unfamiliar with the concept, the definition
from Wikipedia is as follows: a fractal is "a rough or fragmented geometric
shape that can be subdivided in parts, each of which is (at least approximately)
a reduced-size copy of the whole".

At the end of the unit in which we were taught them, the fractal below was a
test question. In subsequent years, I began drawing it freehand to higher and
higher levels. The details and patterns that emerge are fascinating.

The goal is to create a ruby program which takes the level as an argument and
then draws the fractal shown below to the specified level. The fractal is
created by drawing the first level, then repeating the pattern such that each
base piece is replaced with the fractal from the higher level. Thus, to move
from level 1 to level 2, we replace each line with the shape at level 1. Notice
that the position changes as well, meaning if the line is vertical we replace it
with a vertically positioned shape of level 1 (right and left facing also
matter). I have shown the first 3 levels below (including the base component at
level 0). Feel free to use the console for output or get fancy with RMagick or
something similar.

_ <-- Level 0
_
_| |_ <-- Level 1
_
_| |_
_| |_ <-- Level 2
_|_ _|_
_| |_| |_| |_

_
_| |_
_| |_
_|_ _|_
_| |_| |_| |_
_| |_
_|_ _|_
_| |_| |_| |_ <-- Level 3
_| |_
_|_ _|_
_| |_|_ _ _ _|_| |_
_| |_|_|_| |_|_|_| |_
_|_ _|_|_ _|_|_ _|_
_| |_| |_| |_| |_| |_| |_| |_

17 Answers

Kyle Schmitt

5/25/2007 1:45:00 PM

0

If you want to create an image to view instead of on the console,
RMagic/RScience may be convenient, but never forget how plain old easy
it is to make a PPM image :)
You just write it out as text! This may be the easiest way to create
an image file from any language.

The header is 4 lines long, the data is written as r g b color values

Header:
First line is "P3"
Second line is a comment line that starts with "#" and goes however long
Third line contains two integers with a space between them, they
represent the size of the image
Fourth line is the maximum any color channel can be for any pixel.
For instance 7 will mean any pixel can have a red value 0-7, green
value of 0-7 and a blue value of 0-7. If you use 255 you're getting 8
bits of color per channel, which is what were used to now days.

The data itself:
Everything now is just an integer, separated by a \n or a space.
Every triplet of integers represents a pixel, with each integer in the
triplet representing a different channel: Red, Green, Blue.
The triplets are written in order, with no particular formatting, no
indication that a new line is starting, and no indication that the
file or data has ended. You just write them out, and it's up to the
viewer to read the header and know when the new line is, how many
integers to read, etc.
(There is a max length for any given line, and I don't remember what
it is, so be on the safe side and put in a \n after each pixel)

So this is a valid image file (1x3 pixels, one red one green one blue!)

P3
# comment line
1 3
255
255 0 0
0 255 0
0 0 255

Hope this helps some of you who want to write images, but don't want
to use a library (for some reason or another), or just don't want to
use something they don't understand.

--Kyle

PS:
http://netpbm.sourc... has more info than I possibly can, and
probably describes it better, but longer.

Kyle Schmitt

5/25/2007 1:47:00 PM

0

ACK!
"which is what were used to now days."
Sorry
Please read that as
"which is what we're used to now days."

James Gray

5/25/2007 1:55:00 PM

0

On May 25, 2007, at 8:44 AM, Kyle Schmitt wrote:

> If you want to create an image to view instead of on the console,
> RMagic/RScience may be convenient, but never forget how plain old easy
> it is to make a PPM image :)

I wrote pretty much the same thing in my summary to this previous quiz:

http://www.rubyquiz.com/qu...

I used a more compact PPM format though.

James Edward Gray II


Kyle Schmitt

5/25/2007 2:05:00 PM

0

Wrote it as binary?

Yea I guess that is quite a bit more compact :)

--Kyle

Morton Goldberg

5/25/2007 11:53:00 PM

0

On May 25, 2007, at 9:18 AM, Ruby Quiz wrote:

> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
> =-=-=-=-=-=-=
>
> by Drew Olson
>
> When I learned about fractals in high school math class, I
> immediately found
> them fascinating. For those of you unfamiliar with the concept, the
> definition
> from Wikipedia is as follows: a fractal is "a rough or fragmented
> geometric
> shape that can be subdivided in parts, each of which is (at least
> approximately)
> a reduced-size copy of the whole".
>
> At the end of the unit in which we were taught them, the fractal
> below was a
> test question. In subsequent years, I began drawing it freehand to
> higher and
> higher levels. The details and patterns that emerge are fascinating.
>
> The goal is to create a ruby program which takes the level as an
> argument and
> then draws the fractal shown below to the specified level. The
> fractal is
> created by drawing the first level, then repeating the pattern such
> that each
> base piece is replaced with the fractal from the higher level.
> Thus, to move
> from level 1 to level 2, we replace each line with the shape at
> level 1. Notice
> that the position changes as well, meaning if the line is vertical
> we replace it
> with a vertically positioned shape of level 1 (right and left
> facing also
> matter). I have shown the first 3 levels below (including the base
> component at
> level 0). Feel free to use the console for output or get fancy with
> RMagick or
> something similar.
>
> _ <-- Level 0
> _
> _| |_ <-- Level 1
> _
> _| |_
> _| |_ <-- Level 2
> _|_ _|_
> _| |_| |_| |_
>
> _
> _| |_
> _| |_
> _|_ _|_
> _| |_| |_| |_
> _| |_
> _|_ _|_
> _| |_| |_| |_ <-- Level 3
> _| |_
> _|_ _|_
> _| |_|_ _ _ _|_| |_
> _| |_|_|_| |_|_|_| |_
> _|_ _|_|_ _|_|_ _|_
> _| |_| |_| |_| |_| |_| |_| |_
>

I'm confused about the intent of this quiz. Is the main focus writing
code that will re-create the ASCII art shown above, or is it just to
draw the required fractal by any means of the solver's choice (for
example, using the turtle geometry classes developed in Quiz 104)? If
the latter is permitted, a simple, recursive turtle program will do
the job.

Regards, Morton

James Gray

5/26/2007 3:58:00 AM

0

On May 25, 2007, at 6:53 PM, Morton Goldberg wrote:

> I'm confused about the intent of this quiz. Is the main focus
> writing code that will re-create the ASCII art shown above, or is
> it just to draw the required fractal by any means of the solver's
> choice (for example, using the turtle geometry classes developed in
> Quiz 104)? If the latter is permitted, a simple, recursive turtle
> program will do the job.

You guys should know by now that I'm pretty light on rules.
Officially, the "intent of this quiz" is to learn something and/or
have some fun. ;)

I say, if you can solve it simply with turtle graphics, show us. I
doubt everyone will try that so it certainly has a unique edge.

James Edward Gray II

Morton Goldberg

5/26/2007 6:02:00 PM

0

On May 25, 2007, at 11:58 PM, James Edward Gray II wrote:

> On May 25, 2007, at 6:53 PM, Morton Goldberg wrote:
>
>> I'm confused about the intent of this quiz. Is the main focus
>> writing code that will re-create the ASCII art shown above, or is
>> it just to draw the required fractal by any means of the solver's
>> choice (for example, using the turtle geometry classes developed
>> in Quiz 104)? If the latter is permitted, a simple, recursive
>> turtle program will do the job.
>
> You guys should know by now that I'm pretty light on rules.
> Officially, the "intent of this quiz" is to learn something and/or
> have some fun. ;)
>
> I say, if you can solve it simply with turtle graphics, show us. I
> doubt everyone will try that so it certainly has a unique edge.

James, given the way the quiz is currently formulated, I do realize
_you_ would never dump on a turtle graphics solution. However, I
wonder whether or not the quiz author would welcome such a solution.
I suspect he thinks providing ASCII output is an integral part of his
quiz. I seek clarification because I think the quiz author's point of
view should be respected.

Regards, Morton

James Gray

5/26/2007 7:02:00 PM

0

On May 26, 2007, at 1:02 PM, Morton Goldberg wrote:

> On May 25, 2007, at 11:58 PM, James Edward Gray II wrote:
>
>> On May 25, 2007, at 6:53 PM, Morton Goldberg wrote:
>>
>>> I'm confused about the intent of this quiz. Is the main focus
>>> writing code that will re-create the ASCII art shown above, or is
>>> it just to draw the required fractal by any means of the solver's
>>> choice (for example, using the turtle geometry classes developed
>>> in Quiz 104)? If the latter is permitted, a simple, recursive
>>> turtle program will do the job.
>>
>> You guys should know by now that I'm pretty light on rules.
>> Officially, the "intent of this quiz" is to learn something and/or
>> have some fun. ;)
>>
>> I say, if you can solve it simply with turtle graphics, show us.
>> I doubt everyone will try that so it certainly has a unique edge.
>
> James, given the way the quiz is currently formulated, I do realize
> _you_ would never dump on a turtle graphics solution. However, I
> wonder whether or not the quiz author would welcome such a
> solution. I suspect he thinks providing ASCII output is an integral
> part of his quiz. I seek clarification because I think the quiz
> author's point of view should be respected.

Well, he did invite graphic solutions in the quiz description...

James Edward Gray II

Drew Olson

5/26/2007 7:16:00 PM

0

Morton Goldberg wrote:
> On May 25, 2007, at 11:58 PM, James Edward Gray II wrote:
>
>> Officially, the "intent of this quiz" is to learn something and/or
>> have some fun. ;)
>>
>> I say, if you can solve it simply with turtle graphics, show us. I
>> doubt everyone will try that so it certainly has a unique edge.
>
> James, given the way the quiz is currently formulated, I do realize
> _you_ would never dump on a turtle graphics solution. However, I
> wonder whether or not the quiz author would welcome such a solution.
> I suspect he thinks providing ASCII output is an integral part of his
> quiz. I seek clarification because I think the quiz author's point of
> view should be respected.
>
> Regards, Morton

Morton -

I'm with James here. The spirit of the quiz is the display the output in
any way you see fit. I wrote the quiz based on ASCII output as it would
allow those who didn't want to use any external libraries or tools the
opportunity to participate. I wrote my solution based on the ASCII, but
I would be excited to see you solve it using the turtle solution. In
short, I say use whatever tool you feel provides the best solution!

- Drew

--
Posted via http://www.ruby-....

Jesse Merriman

5/27/2007 1:49:00 PM

0


Content-Type: Multipart/Mixed;
boundary="Boundary-00=_0wYWGFyZ4Djt9UP"
My solution uses a simple recursive method to build a turtle-graphics string
for the fractal. I use three commands:

F: move forward one unit
L: turn left, but do not move
R: turn right, but do not move

So level 1 can be represented by "FLFRFRFLF". I thought of doing it more
compactly and letting L & R imply a move, so that level 1 would be
"FLRRL", but that made some of my code more complex, so I stuck with the
verbose way.

My code (in fractals.rb) can accept different level-0 and level-1 turtle
strings, so it can be used to build other fractals besides the one in the
quiz. The basic algorithm I use is:

For every char in level-1:
If the char is the level-0 char, insert level-n-1, else insert the char

This is a bit different than:

For every level-n-1 in self, insert self

But it seems to be equivalent. So the turtle-string-building code is pretty
short. Most of my code is for converting that string into different output
formats. These include the turtle string itself, text like that in the
original quiz email (turtle_text.rb), displayed on the screen using RMagick
(non-Windows only, if I remember the docs correctly), and any graphic format
that RMagick can write (turtle_draw.rb and turtle_image.rb).

Here's some examples:

$ ./fractals.rb 2
_
_| |_
_| |_
_|_ _|_
_| |_| |_| |_

$ ./fractals.rb 3 -format display # fractal display with ImageMagick

$ ./fractals.rb 3 -format png
Wrote level-3 fractal to fractal_F_FLFRFRFLF_3.png

$ ./fractals.rb 3 -l1 FLFFRF # alternate level-1 rule
_
|
_|
|_ _
|
|_ _
_| _ _
| | |_ _ | |_ _
_| _ _| | _ _| _|
|_ _ | |_ _ | |
|_| |_| _|
|_ _
|
|_ _
_|
|
_|

(See the comment at the top of fractals.rb for the full usage.)

The RMagick stuff went pretty well, but the one thing that annoyed me was that
setting Draw's affine transformation matrix does not affect previous drawing
calls like line(). So I have to trace over the entire fractal once beforehand
just to get the size of it to set up the affine.

Also, for my TurtleText.textize method, I wanted to be able to use
Turtle.each_coord, and convert graphics coords to text coords. I couldn't find
a nice way to do that, though. For example, here are the widths and heights of
some fractals using graphics and text:

Turtle Wg Wt Hg Ht
F 1 1 0 1
LF 0 1 1 1

So Wt (text width) can't be calculated using only Wg, and neither for Ht and
Hg. This led to some kind-of duplication of code spirit, rather than letter,
in textize() and each_coord(). I think that, for the fractal in the quiz,
the widths and heights for level L are:

Wt: 2*3**L - 1
Ht: (3**L / 2).ceil
Wg: 3**L
Hg: (3**L / 2).floor

Though since I wanted to be general for other fractals, I couldn't use these.

It was fun to play around with different base strings, and see the resulting
image. Here's a few interesting ones I found (you can infer the rules from the
filenames):

http://www.jessemer...images/ruby_quiz/125_fractals/fractal_F_...
http://www.jessemer...images/ruby_quiz/125_fractals/fractal_F_F...
http://www.jessemer...images/ruby_quiz/125_fractals/fractal_F_R...
http://www.jessemer...images/ruby_quiz/125_fractals/fractal_F_FLF...
http://www.jessemer...images/ruby_quiz/125_fractals/fractal_F_LFFFRF...
http://www.jessemer...images/ruby_quiz/125_fractals/fractal_F_RFR...
http://www.jessemer...images/ruby_quiz/125_fractals/fractal_F_FLFRF...
http://www.jessemer...images/ruby_quiz/125_fractals/fractal_F_...

--
Jesse Merriman
jessemerriman@warpmail.net
http://www.jessemer...