How to resolve the algorithm Koch curve step by step in the Prolog programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Koch curve step by step in the Prolog programming language

Table of Contents

Problem Statement

Draw a Koch curve. See details: Koch curve

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Koch curve step by step in the Prolog programming language

Source code in the prolog programming language

main:-
    write_koch_snowflake('koch_snowflake.svg').

write_koch_snowflake(File):-
    open(File, write, Stream),
    koch_snowflake(Stream, 600, 5),
    close(Stream).

koch_snowflake(Stream, Size, N):-
    format(Stream, "<svg xmlns='http://www.w3.org/2000/svg' width='~d' height='~d'>\n",
           [Size, Size]),
    write(Stream, "<rect width='100%' height='100%' fill='black'/>\n"),
    write(Stream, "<path stroke-width='1' stroke='white' fill='none' d='"),
    Sqrt3_2 = 0.86602540378444,
    Length is Size * Sqrt3_2 * 0.95,
    X0 is (Size - Length)/2,
    Y0 is Size/2 - Length * Sqrt3_2/3,
    X1 is X0 + Length/2,
    Y1 is Y0 + Length * Sqrt3_2,
    X2 is X0 + Length,
    format(Stream, 'M ~g,~g ', [X0, Y0]),
    koch_curve(Stream, X0, Y0, X1, Y1, N),
    koch_curve(Stream, X1, Y1, X2, Y0, N),
    koch_curve(Stream, X2, Y0, X0, Y0, N),
    write(Stream, "z'/>\n</svg>\n").

koch_curve(Stream, _, _, X1, Y1, 0):-
    !,
    format(Stream, 'L ~g,~g\n', [X1, Y1]).
koch_curve(Stream, X0, Y0, X1, Y1, N):-
    N > 0,
    Sqrt3_2 = 0.86602540378444,
    N1 is N - 1,
    Dx is X1 - X0,
    Dy is Y1 - Y0,
    X2 is X0 + Dx/3,
    Y2 is Y0 + Dy/3,
    X3 is X0 + Dx/2 - Dy * Sqrt3_2/3,
    Y3 is Y0 + Dy/2 + Dx * Sqrt3_2/3,
    X4 is X0 + 2 * Dx/3,
    Y4 is Y0 + 2 * Dy/3,
    koch_curve(Stream, X0, Y0, X2, Y2, N1),
    koch_curve(Stream, X2, Y2, X3, Y3, N1),
    koch_curve(Stream, X3, Y3, X4, Y4, N1),
    koch_curve(Stream, X4, Y4, X1, Y1, N1).


  

You may also check:How to resolve the algorithm Josephus problem step by step in the Lua programming language
You may also check:How to resolve the algorithm Mutual recursion step by step in the Io programming language
You may also check:How to resolve the algorithm Convert seconds to compound duration step by step in the Delphi programming language
You may also check:How to resolve the algorithm Factors of an integer step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Sorting algorithms/Comb sort step by step in the jq programming language