Koch Curve

Could anyone help me? I need to represent the Koch Curve using Opengl. I’m not good at maths so I’m having problems with it, so please if anyone knows how to implement it let me know Thanks a lot

Ohhh I forget to say a thing use opengl on Borland Builder C++.
Thanks a lot

Hello,

what part of the problem are you having difficulty with? you could try the simple recursive algorithm:

  • a function takes in a depth parameter d (d>=0) and the two end-points (p1, p2) of a line in two-space.
  • if d==0 then the line p1…p2 is drawn and the function returns, otherwise
  • the line is somehow “deformed” and the function is recursively called with the new line segments and with d:=d-1.

How you deform the line is up to you–deforming it in different ways will give you different diagrams. If you’re after something like the koch snowflake, then you could’d want to deform the line like so:

from

p1---------------p2

to

      pB
     /  \

p1-----pA pC-----p2

then you’d recursively call the function with d:=d-1 (so d is ultimately 0 so the line is drawn) with the line segments p1…pA, pA…pB, pB…pC, pC…p2

finding pA…pC shouldn’t be too difficult.

cheers,
John

I have some code… which you may have to modify for your mean. It isn’t all that well documented, and it has a deprecated method for computing the koch curve down to a finite length. It is meant to make the koch snowflake… http://69-144-231-14.client.bresnan.net/index.html

Thanks I’m still trying it. I can’t see http://69-144-231-14.client.bresnan.net/index.html it seems that the server is down

there is an article with a small listing which does the trick:
http://www.game-face.de/imprimer.php3?id_article=21

Maybe it will help. The article is in german and for goods sake the listing is a picture, but may be you can figure it out…
It looks easy and I think it is…
just recursion
Greetings,
Peter

Here’s an incomplete code listing:

typedef GLfloat twodimpoint[2];
int limit = 10; // more than this gets ugly

void kochcurve (twodimpoint begin, twodimpoint end, int n)
{
if(n <= 0)
{
glColor3f(1.0,1.0,1.0);
glBegin(GL_LINES);
{
glVertex2f(begin[0],begin[1]);
glVertex2f(end[0],end[1]);
}
glEnd();
}
else
{
// begin
twodimpoint pointA = {begin[0],begin[1]};
// 2/3 of the way
twodimpoint pointB = {(2.0 * begin[0]+end[0])/3.0,(2.0 * begin[1]+end[1])/3.0 };
// point above line at 1/2 way
twodimpoint pointC = {(begin[0]+end[0])/2.0 - sqrt(3.0)/6.0 *(end[1]-begin[1]),
(begin[1]+end[1])/2.0 + sqrt(3.0)/6.0 *(end[0]-begin[0])};
// 1/3 of the way
twodimpoint pointD = {(begin[0]+2.0 * end[0])/3.0,(begin[1]+2.0 *end[1])/3.0 };
// end
twodimpoint pointE = {end[0],end[1]};

kochcurve (pointA, pointB, n-1);
//line a to b
kochcurve (pointB, pointC, n-1);
//line b to c
kochcurve (pointC, pointD, n-1);
//line c to d
kochcurve (pointD, pointE, n-1);
//line d to e

}
}

void kochsnowflake(twodimpoint point1, twodimpoint point2, twodimpoint point3)
{
kochcurve(point1, point2, limit);
kochcurve(point2, point3, limit);
kochcurve(point3, point1, limit);
} // end of kochsnowflake

Basically the math in here is just using the fact that the points from B to C and C to D form an equilateral triangle. So if you divide that triangle into two even triangles, they become 30/60/90 right triangles… the math gets easier after that.

So we can imagine:
(using code to help format)

    C
   / \
  /   \

A----B D----E

This recurses and draws the smallest stuff first using the limit as tada, a limit.
There is another way to do it where you don’t put how many times, but instead how long in ‘space’ you want the smallest lines to be drawn. If I get some requests for it I’ll put it up, but this method is pretty straight forward.

Sorry about the server, it has been flakey.

Tried to edit the formatting… but who knows…

[This message has been edited by vasagotempest (edited 02-11-2004).]

Sorry but I’ve been abroad and I had no conexion. Thanks to everyone