From: LuisGLopez
Subject: Koch figures
Date: 
Message-ID: <1127920777.876222.10880@g44g2000cwa.googlegroups.com>
Hi!

Again with a simple code ;)

It's about plotting Koch figures (curve, snowflake... and more!).
(http://en.wikipedia.org/wiki/Koch_curve).

I did a search in c.l.l., but didn't find anything. Maybe too simple?
Anyway, I must confess that I am *very* *proud* of it! :P
Really... I just wanted to draw the Koch curve, but then realize that,
with the *same* code I made, I could draw *any* koch figure of my
choice! I think that that's due to the way that lisp is 'shaping' my
mind... well, I would like to think so! :)

(Graphics initialization code omited):
------------------------------------------

(defun x (pt) (car pt))
(defun y (pt) (cdr pt))
(defun point (x y) (cons x y))

(defun draw-line (A B)
  (xlib:draw-line *win* *gctxt*
		  (round (x A)) (round (y A))
		  (round (x B)) (round (y B))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Koch curve code

(defconstant +tan60+ (tan (/ pi 3)))

(defun punto-a-un-tercio-entre (A B)
  "Yields the point at one third the distance between A and B."
  (point (/ (+ (x B) (* 2 (x A))) 3)
	 (/ (+ (y B) (* 2 (y A))) 3)))

(defun punto-equilátero (A B)
  "Yields the point that forms an equilateral triangle with A and B.
Well, one of them: the one 'at the left of' A."
  (let ((D (point (/ (+ (x A) (x B)) 2)
		  (/ (+ (y A) (y B)) 2))))
    (point (- (x D) (* +tan60+ (- (y D) (y A))))
	   (+ (y D) (* +tan60+ (- (x D) (x A)))))))

(defun koch (puntos &key (clear t))
  (let ((puntos-nuevos (make-array (1+ (* 4 (1- (length puntos))))
:fill-pointer 0)))
    (dotimes (i (1- (length puntos)))
      (let* ((A  (elt puntos i))
	     (B  (elt puntos (1+ i)))
	     (A1 (punto-a-un-tercio-entre A B))
	     (B1 (punto-a-un-tercio-entre B A)))
	(draw-line A B)
	(vector-push A puntos-nuevos)
	(vector-push A1 puntos-nuevos)
	(vector-push (punto-equilátero A1 B1) puntos-nuevos)
	(vector-push B1 puntos-nuevos)))
    (vector-push (elt puntos (1- (length puntos))) puntos-nuevos)
    (xlib:display-force-output *dpy*)
    (sleep 1)
    (when clear (clear-window))
    (koch puntos-nuevos :clear clear)))

(defun dibujar-koch (puntos &key (clear t))
  (open-window)
  (koch puntos :clear clear))
--------------------------------------
suggested examples:

(dibujar-koch (vector (point 500 250) (point 100 250)))

(dibujar-koch (vector (point 100 400) (point 400 400)
		      (point 400 100) (point 100 100)
		      (point 100 400)))

(dibujar-koch (vector (point 100 400) (point 500 400)
		      (punto-equilátero (point 500 400) (point 100 400))
		      (point 100 400)))

Any C&C? :-)

Luis.

From: Raph
Subject: Re: Koch figures
Date: 
Message-ID: <1127927028.475971.107990@z14g2000cwz.googlegroups.com>
Very nice.

For the CLISP users out there (Windows or Linux), I have added the new
Koch drawing code to my little Canvas Demo Application. (Your original
"Beautiful Patterns" post forced me to write it, because I wanted to
see what you were talking about!)  :)

The code requires CLISP and GTK, and works on Linux and Windows.

See http://www.gotlisp.com/gtk-drawing.html for more details.

Thanks, Luis!

Raph
From: A.L.
Subject: Re: Koch figures
Date: 
Message-ID: <vinlj1h81c6qgtj808i8nlchbv5i7kfamg@4ax.com>
On 28 Sep 2005 08:19:37 -0700, "LuisGLopez" <············@gmail.com>
wrote:

>Hi!
>
>Again with a simple code ;)
>
>It's about plotting Koch figures (curve, snowflake... and more!).
>(http://en.wikipedia.org/wiki/Koch_curve).
>
>I did a search in c.l.l., but didn't find anything. Maybe too simple?
>Anyway, I must confess that I am *very* *proud* of it! :P
    

And below is in Java. Could somebody, using both examples, explain why
"Lisp is better"?...

A.L.

public class Koch {

    // Koch curve of order n
    public static void koch(int n, double size) {
        if (n == 0) StdDraw.forward(size);
        else {
            koch(n-1, size);
            StdDraw.rotate(60);
            koch(n-1, size);
            StdDraw.rotate(-120);
            koch(n-1, size);
            StdDraw.rotate(60);
            koch(n-1, size);
        }
    }

    public static void main(String args[]) {
        int N = Integer.parseInt(args[0]);
        int width = 512;
        int height = (int) (2 * width / Math.sqrt(3));
        double size = width / Math.pow(3.0, N);
        StdDraw.create(width, height);
        StdDraw.setScale(0, 0, width, height);

        // three Koch curves in the shape of an equilateral triangle
        StdDraw.moveTo(0, width * Math.sqrt(3) / 2);
        koch(N, size);
        StdDraw.rotate(-120);
        koch(N, size);
        StdDraw.rotate(-120);
        koch(N, size);
        StdDraw.show();
    }
}

Taken rom

http://www.cs.princeton.edu/introcs/27recursion/Koch.java.html
From: Joe Marshall
Subject: Re: Koch figures
Date: 
Message-ID: <hdc4u99x.fsf@alum.mit.edu>
A.L. <············@kalabambuko.com> writes:

> And below is in Java. Could somebody, using both examples, explain why
> "Lisp is better"?...

This example is too small to show off the strengths of Lisp.
From: Emre Sevinc
Subject: Re: Koch figures
Date: 
Message-ID: <87ek78aj82.fsf@ileriseviye.org>
Joe Marshall <·········@alum.mit.edu> writes:

> A.L. <············@kalabambuko.com> writes:
>
>> And below is in Java. Could somebody, using both examples, explain why
>> "Lisp is better"?...
>
> This example is too small to show off the strengths of Lisp.
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I think that is a very important sentence. 

Trying to find the "right-sized" example is very difficult
indeed unless you know your audience's background very well.

Pascal Costanza mentions the same thing, "to understand the
power you need something big",  maybe that's the
reason why he, instead of tackling toy problems, dived into
CL headlong and developed AspectL, ContextL, etc. using 
CLOS, MOP, etc. As a very experienced Java programmer he says
he preferred CL.

Another example: SWCLOS

http://iswc2004.semanticweb.org/demos/32/

http://international-lisp-conference.org/2005/tutorials.html#swclos_semantic_web_processing_in_clos

What does the above example mean to a Java programmer?
To a C# programmer? What about PHP programmers? Do PHP
programmers talk about OWL and logical reasoning nowadays?
Is MetaObject Protocol "simply something like reflection in
Java/C#"? How can you make people understand?

A question: Can you impress a C, C++, Java, Perl, etc.
programmer by saying: "Imagine that we have Lisp and
no OO functionality, how can we put OO in it by
simply using closures, lambdas, etc.? How could
you do it for your language?" Is this a right-sized
example? 

For experienced other-language programmers intermediate level
examples are not very impressive. For beginners complex
examples simply do not mean much. The spectrum is wide and
deep and this situation feeds lots of hot (and generally
not very meaningful) debate and trolls.

I wonder if other comp.lang.* groups receive so many
posts titled "Why *?" :) 

-- 
Emre Sevinc

eMBA Software Developer         Actively engaged in:
http:www.bilgi.edu.tr           http://ileriseviye.org
http://www.bilgi.edu.tr         http://fazlamesai.net
Cognitive Science Student       http://cazci.com
http://www.cogsci.boun.edu.tr
From: Emre Sevinc
Subject: Re: Koch figures
Date: 
Message-ID: <87achwaiyh.fsf@ileriseviye.org>
Emre Sevinc <·····@bilgi.edu.tr> writes:

> Joe Marshall <·········@alum.mit.edu> writes:
>
>> A.L. <············@kalabambuko.com> writes:
>>
>>> And below is in Java. Could somebody, using both examples, explain why
>>> "Lisp is better"?...
>>
>> This example is too small to show off the strengths of Lisp.
>   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>
> I think that is a very important sentence. 
>
> Trying to find the "right-sized" example is very difficult
> indeed unless you know your audience's background very well.
>
> Pascal Costanza mentions the same thing, "to understand the
> power you need something big",  maybe that's the
> reason why he, instead of tackling toy problems, dived into
> CL headlong and developed AspectL, ContextL, etc. using 
> CLOS, MOP, etc. As a very experienced Java programmer he says
> he preferred CL.
>
> Another example: SWCLOS
>
> http://iswc2004.semanticweb.org/demos/32/
>
> http://international-lisp-conference.org/2005/tutorials.html
>
> What does the above example mean to a Java programmer?
> To a C# programmer? What about PHP programmers? Do PHP
> programmers talk about OWL and logical reasoning nowadays?
> Is MetaObject Protocol "simply something like reflection in
> Java/C#"? How can you make people understand?


Another example:

Can you impress people by saying that CL lets you do
things like that:

http://www.cs.utexas.edu/users/moore/acl2/

What kind of programmers would be impressed? Could
you impress database programmers? GUI designers? PHP
programmers? Analysts? Ruby programmers? How would
experienced C or C++ programmers react? 

Sun Microsystems and AMD seems to be impressed (or
aren't they? ;-)


> A question: Can you impress a C, C++, Java, Perl, etc.
> programmer by saying: "Imagine that we have Lisp and
> no OO functionality, how can we put OO in it by
> simply using closures, lambdas, etc.? How could
> you do it for your language?" Is this a right-sized
> example? 
>
> For experienced other-language programmers intermediate level
> examples are not very impressive. For beginners complex
> examples simply do not mean much. The spectrum is wide and
> deep and this situation feeds lots of hot (and generally
> not very meaningful) debate and trolls.
>
> I wonder if other comp.lang.* groups receive so many
> posts titled "Why *?" :) 


-- 
Emre Sevinc

eMBA Software Developer         Actively engaged in:
http:www.bilgi.edu.tr           http://ileriseviye.org
http://www.bilgi.edu.tr         http://fazlamesai.net
Cognitive Science Student       http://cazci.com
http://www.cogsci.boun.edu.tr
From: Marc Battyani
Subject: Re: Koch figures
Date: 
Message-ID: <11jm1qe8mqttdc5@corp.supernews.com>
"A.L." wrote:
> On 28 Sep 2005 08:19:37 -0700, "LuisGLopez" <············@gmail.com>
> wrote:
>
> >It's about plotting Koch figures (curve, snowflake... and more!).
> >(http://en.wikipedia.org/wiki/Koch_curve).
> >
> >I did a search in c.l.l., but didn't find anything. Maybe too simple?
> >Anyway, I must confess that I am *very* *proud* of it! :P
>
>
> And below is in Java. Could somebody, using both examples, explain why
> "Lisp is better"?...
>
> A.L.

The Koch figure is the example 8 of the cl-pdf examples:
http://www.fractalconcept.com:8000/public/open-source/cl-pdf/examples/exampl
es.lisp

Here it is:

(defun vk-fractal (l level)
  (pdf:with-saved-state
      (if (zerop level)
          (progn
            (pdf:move-to 0 0)
            (pdf:line-to l 0)
            (pdf:stroke))
          (loop with l3 = (/ l 3.0) and l-1 = (1- level)
                for angle in '(nil 60 -120 60)
                do (when angle (pdf:rotate angle))
                   (vk-fractal l3 l-1))))
  (pdf:translate l 0))

The with-saved-state/translate stuff is to avoid the floating point rounding
accumulation errors in the pdf readers for high order values.

Compared to the Java one, at least this one generates a cool pdf file:
http://www.fractalconcept.com:8000/public/open-source/cl-pdf/examples/ex8.pd
f

You can zoom the level 6 with your pdf reader to see the deeper levels.
(Don't try with the Java version you would be disappointed ;-)

> public class Koch {
>
>     // Koch curve of order n
>     public static void koch(int n, double size) {
>         if (n == 0) StdDraw.forward(size);
>         else {
>             koch(n-1, size);
>             StdDraw.rotate(60);
>             koch(n-1, size);
>             StdDraw.rotate(-120);
>             koch(n-1, size);
>             StdDraw.rotate(60);
>             koch(n-1, size);
>         }
>     }

Creating a class just to draw a simple curve like this is a perfect example
of bad taste.
And being forced to declare it as public, static and void is well...
pathetic. ;-)

Marc
[Sorry, I should not feed the trolls especially during the troll season
(every fall ;-) ]
From: Raph
Subject: Re: Koch figures
Date: 
Message-ID: <1127944012.176746.264300@o13g2000cwo.googlegroups.com>
If I (defun yucky-draw-line (length) ... ) as a function that draws
"length" pixels from global_point_x, global_point_y in the direction of
the current global_angle, then I can reproduce the results of the Java
code easily:

(defun my_koch (num size)
  (if (= 0 num)
      (yucky-draw-line size)
    (loop
      for angle in '(nil 60 -120 60)
      do (when angle (setq global_angle (+ global_angle angle)))
      (my_koch (- num 1) size))))

(defun the_main_thingy (koch-level)
  (setf my-width 512)
  (setf my-height (/ (* 2 my-width) (sqrt 3)))
  (setf my-size (/ (float my-width) (expt 3 koch-level)))
  (setq global_point_x 0)
  (setq global_point_y (round (/ (* (float my-width) (sqrt 3)) 2)))
  (loop
    for angle in '(nil -120 -120)
    do (when angle   (setq global_angle (+ global_angle angle)))
    (my_koch koch-level my-size)))

That's it. Happy to post the entire working code (meaning, the
yucky-draw-line function) if you want it.

That's *not* the point. The point is that the above code ONLY draws the
snowflake Koch curve. Luis specifically said "Really... I just wanted
to draw the Koch curve, but then realize that, with the *same* code I
made, I could draw *any* koch figure of my choice!"

Look at his examples:

This draws a Koch Curve based on a line...

   (dibujar-koch (vector (point 500 250) (point 100 250)))

This draws a Koch Curve based on a square...

   (dibujar-koch (vector (point 100 400) (point 400 400)
                      (point 400 100) (point 100 100)
                      (point 100 400)))

And this is the snowflake...

   (dibujar-koch (vector (point 100 400) (point 500 400)
                      (punto-equilátero (point 500 400) (point 100
400))
                      (point 100 400)))

Could you do it in Java? Sure. The point is not that you can't do this
in Java, but that Luis' relatively small code segment allows Koch
curves to be drawn out of vectors of points. I think the code size and
complexity required to do this in Java would be larger.

If we're going to do language trolling, let's at least do it with the
same example problem. It's not about just drawing the Koch snowflake.

Thanks!

Raph
From: LuisGLopez
Subject: Re: Koch figures
Date: 
Message-ID: <1127953158.522940.138860@o13g2000cwo.googlegroups.com>
Hi!!!

Thank *you*, Raph!!! :-)

Raph ha escrito:

> That's *not* the point. The point is that the above code ONLY draws the
> snowflake Koch curve. Luis specifically said "Really... I just wanted
> to draw the Koch curve, but then realize that, with the *same* code I
> made, I could draw *any* koch figure of my choice!"

Exactly. And I can't exagerate on this. I had in mind only to achieve
the Koch curve. I only thought as a possible *future* *extension* the
ability to draw the famous kornflake. But then... I just *saw* that the
original code could achieve both with no changes... and then more! It
was very exciting. For a moment, I felt more like a 'discoverer' than a
programmer (well, in fact, I'm not a programmer either; just a hobbist.
But I love this). And I sincerely feel that this was posible due to the
'lisp way of thinking'. I mean... lisp is making me think in a
different way, and code in a different way. I used to code in java a
lot, and I'm sure I would never get such an elegant and general
solution to this problem. So I would say that, at least in my case,
lisp is better not just for its own strenght (which I'm barely
beginning to touch), but for the way it's making me think about
algorithms. Don't get me wrong either: I still like java, but...men:
lisp is *another* thing... ;)

> If we're going to do language trolling, let's at least do it with the
> same example problem.

LOL! :)

Thank you!!!!

Luis.
From: Frank Buss
Subject: Re: Koch figures
Date: 
Message-ID: <1j0tpxua9yems$.344jv3eqqngg$.dlg@40tude.net>
LuisGLopez wrote:

> Again with a simple code ;)
> 
> It's about plotting Koch figures (curve, snowflake... and more!).
> (http://en.wikipedia.org/wiki/Koch_curve).
> 
> I did a search in c.l.l., but didn't find anything. Maybe too simple?

You didn't search for the right words:

http://groups.google.de/group/comp.lang.lisp/browse_thread/thread/05631fa93379bca8/248b67466ca2aa7d

The snowflake can be created like this:

(postscript-l-system '(("F"."F+F--F+F")) "F--F--F" 60 6) 

Converted to PDF: http://www.frank-buss.de/tmp/koch.pdf

-- 
Frank Bu�, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: LuisGLopez
Subject: Re: Koch figures
Date: 
Message-ID: <1128037436.505081.209230@g47g2000cwa.googlegroups.com>
Frank... wow! That code is certainly much more general! I love the pdf
result!!!!
From: Frank Buss
Subject: Re: Koch figures
Date: 
Message-ID: <5rvxfjuwo7j1$.f383tkve7hcz$.dlg@40tude.net>
>> http://groups.google.de/group/comp.lang.lisp/browse_thread/thread/05631fa93379bca8/248b67466ca2aa7d 

LuisGLopez wrote:

> Frank... wow! That code is certainly much more general! I love the pdf
> result!!!!

thanks, but after looking again at it, the code is not very good, because
in the meantime I've learned a bit more CL. For example there is something
like this in it:

(if (sometest) 
   nil 
 (someresult)) 

which should be written (unless (sometest) (someresult)). And (loop for i
from 0 to (1- x) should be rewritten with "below" etc.

Besides this minor things I'm sure the code structure could be simplified.
I don't have the time to clean it up, but perhaps someone in this newsgroup
wants to do it?

-- 
Frank Bu�, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: Marc Battyani
Subject: Re: Koch figures
Date: 
Message-ID: <11jqa6k5i1d0o23@corp.supernews.com>
"Frank Buss" <··@frank-buss.de> wrote:
>>>
http://groups.google.de/group/comp.lang.lisp/browse_thread/thread/05631fa933
79bca8/248b67466ca2aa7d
>
> thanks, but after looking again at it, the code is not very good, because
> in the meantime I've learned a bit more CL. For example there is something
> like this in it:
>
> (if (sometest)
>    nil
>  (someresult))
>
> which should be written (unless (sometest) (someresult)). And (loop for i
> from 0 to (1- x) should be rewritten with "below" etc.
>
> Besides this minor things I'm sure the code structure could be simplified.
> I don't have the time to clean it up, but perhaps someone in this
newsgroup
> wants to do it?

I don't have the time for this but I just converted it to use cl-pdf instead
of Postscript (deprecated ;-) and I added it to the cl-pdf examples
directory
http://www.fractalconcept.com:8000/public/open-source/cl-pdf/examples/l-syst
ems.lisp

The cl-pdf generated examples are here:
http://www.fractalconcept.com/fcweb/download/l-system1.pdf
http://www.fractalconcept.com/fcweb/download/l-system2.pdf
http://www.fractalconcept.com/fcweb/download/l-system3.pdf

Marc
From: Frank Buss
Subject: Re: Koch figures
Date: 
Message-ID: <1pszv18x0ix$.18gr6ftcocuvx$.dlg@40tude.net>
Marc Battyani wrote:

> I don't have the time for this but I just converted it to use cl-pdf instead
> of Postscript (deprecated ;-) and I added it to the cl-pdf examples
> directory
> http://www.fractalconcept.com:8000/public/open-source/cl-pdf/examples/l-systems.lisp

thanks, now I don't have a chance to get a reputation for being a good Lisp
programmer any more :-)

-- 
Frank Bu�, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: Marc Battyani
Subject: Re: Koch figures
Date: 
Message-ID: <11jrc0vb0hg5d6f@corp.supernews.com>
"Frank Buss" <··@frank-buss.de> wrote:
> Marc Battyani wrote:
>
> > I don't have the time for this but I just converted it to use cl-pdf
instead
> > of Postscript (deprecated ;-) and I added it to the cl-pdf examples
> > directory
> >
http://www.fractalconcept.com:8000/public/open-source/cl-pdf/examples/l-syst
ems.lisp
>
> thanks, now I don't have a chance to get a reputation for being a good
Lisp
> programmer any more :-)

Improvements welcome...
I'm sure you didn't know what to do this rainy (assuming you are in Germany)
week-end ;-)

BTW if anybody have some cool cl-pdf examples, I would be happy to add them
in the examples directory.

Marc