From: Earl F. Glynn Subject: Re: Von koch kurve... Newsgroups: sci.fractals View complete thread (3 articles) Date: 1997/02/19 Anders Öhrt wrote: > > Tjaba! > > Could someone show me how to get the point of the middle triangle if the > line streches from x1,y1 to x2,y2...? I need the math behind x1,y1... > Can't get mine to work... > > /\<= x3, y3 > / \ > -------/ \-------- > x1,y1 x2,y2 > > // Anders > > Ps. C++ syntax could be nice... Ds. Will you take Pascal code? I'll bet you can figure it out: PROCEDURE CreateKochSnowflake; TYPE Point = RECORD x: DOUBLE; y: DOUBLE END; VAR a,b,c : Point; draw : INTEGER; loop : WORD; w : GraphicsWindow; { s a r t b } PROCEDURE NextSegments (a,b: Point; n: WORD); CONST factor = 0.288675135; { SQRT(3) / 6 } VAR middle: Point; xDelta: DOUBLE; yDelta: DOUBLE; r,s,t: Point; BEGIN IF n > 0 THEN BEGIN r.x := (2.0*a.x + b.x) / 3.0; r.y := (2.0*a.y + b.y) / 3.0; t.x := ( a.x + 2.0*b.x) / 3.0; t.y := ( a.y + 2.0*b.y) / 3.0; middle.x := ( a.x + b.x) / 2.0; middle.y := ( a.y + b.y) / 2.0; xDelta := b.x - a.x; yDelta := b.y - a.y; s.x := middle.x + factor*yDelta; s.y := middle.y - factor*xDelta; SetColor (LightBlue); w.MoveTo2D (a.x,a.y); {blank this line} w.LineTo2D (b.x,b.y); SetColor (draw); w.MoveTo2D (a.x,a.y); {add new lines} w.LineTo2D (r.x,r.y); w.LineTo2D (s.x,s.y); w.LineTo2D (t.x,t.y); w.LineTo2D (b.x,b.y); NextSegments (a,r, n-1); NextSegments (r,s, n-1); NextSegments (s,t, n-1); NextSegments (t,b, n-1); END END {NextSegments}; PROCEDURE KochSnowflake (a,b,c: Point; n: WORD); BEGIN SetLineStyle (SolidLn, 0, NormWidth); SetColor (Draw); w.MoveTo2D (a.x,a.y); w.LineTo2D (b.x,b.y); NextSegments (a,b, n); w.MoveTo2D (b.x,b.y); w.LineTo2D (c.x,c.y); NextSegments (b,c, n); w.MoveTo2D (c.x,c.y); w.LineTo2D (a.x,a.y); NextSegments (c,a, n); END {KochSnowflake}; BEGIN w.init; w.viewport (0.0, 0.0, 1.0, 1.0); w.WorldCoordinatesRange (-12,12, -10,8); SetFillStyle (SolidFill, LightBlue); BARIJ (0,GetMaxX, 0,GetMaxY); a.x := -6.0; a.y := -3*SQRT(3.0); b.x := 6.0; b.y := -3*SQRT(3.0); c.x := 0.0; c.y := 3*SQRT(3.0); FOR loop := 0 TO 6 DO BEGIN { SetFillStyle (SolidFill, LightBlue); BARIJ (0,GetMaxX, 0,GetMaxY); } IF loop MOD 2 = 0 THEN draw := white ELSE draw := black; KochSnowflake (a,b,c, loop); SetFillStyle (SolidFill, draw); FloodFillIJ (GetMaxX DIV 2, GetMaxY DIV 2, draw); DELAY (3000); END END {CreateKochSnowflake}; -- Earl F. Glynn EFG Software