How to resolve the algorithm Sierpinski triangle/Graphical step by step in the Prolog programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sierpinski triangle/Graphical step by step in the Prolog programming language

Table of Contents

Problem Statement

Produce a graphical representation of a Sierpinski triangle of order N in any orientation.
An example of Sierpinski's triangle (order = 8) looks like this:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sierpinski triangle/Graphical step by step in the Prolog programming language

Source code in the prolog programming language

sierpinski(N) :-
	sformat(A, 'Sierpinski order ~w', [N]),
	new(D, picture(A)),
	draw_Sierpinski(D, N, point(350,50), 600),
	send(D, size, size(690,690)),
	send(D, open).

draw_Sierpinski(Window, 1, point(X, Y), Len) :-
	X1 is X - round(Len/2),
	X2 is X + round(Len/2),
	Y1 is Y + Len * sqrt(3) / 2,
	send(Window, display, new(Pa, path)),
        (
	   send(Pa, append, point(X, Y)),
	   send(Pa, append, point(X1, Y1)),
	   send(Pa, append, point(X2, Y1)),
	   send(Pa, closed, @on),
	   send(Pa, fill_pattern,  colour(@default, 0, 0, 0))
	).


draw_Sierpinski(Window, N, point(X, Y), Len) :-
	Len1 is round(Len/2),
	X1 is X - round(Len/4),
	X2 is X + round(Len/4),
	Y1 is Y + Len * sqrt(3) / 4,
	N1 is N - 1,
	draw_Sierpinski(Window, N1, point(X, Y), Len1),
	draw_Sierpinski(Window, N1, point(X1, Y1), Len1),
	draw_Sierpinski(Window, N1, point(X2, Y1), Len1).


:- dynamic top/1.

sierpinski_iterate(N) :-
	retractall(top(_)),
	sformat(A, 'Sierpinski order ~w', [N]),
	new(D, picture(A)),
	draw_Sierpinski_iterate(D, N, point(550, 50)),
	send(D, open).

draw_Sierpinski_iterate(Window, N, point(X,Y)) :-
	assert(top([point(X,Y)])),
	NbTours is 2 ** (N  - 1),
	% Size is given here to preserve the "small" triangles when N is big
	Len is 10,
	forall(between(1, NbTours, _I),
	       (   retract(top(Lst)),
		   assert(top([])),
		   forall(member(P, Lst),
			  draw_Sierpinski(Window, P, Len)))).

draw_Sierpinski(Window, point(X, Y), Len) :-
	X1 is X - round(Len/2),
	X2 is X + round(Len/2),
	Y1 is Y + round(Len * sqrt(3) / 2),
	send(Window, display, new(Pa, path)),
        (
	   send(Pa, append, point(X, Y)),
	   send(Pa, append, point(X1, Y1)),
	   send(Pa, append, point(X2, Y1)),
	   send(Pa, closed, @on),
	   send(Pa, fill_pattern,  colour(@default, 0, 0, 0))
	),
	retract(top(Lst)),
	(   member(point(X1, Y1), Lst) -> select(point(X1,Y1), Lst, Lst1)
	;   Lst1 = [point(X1, Y1)|Lst]),

	(   member(point(X2, Y1), Lst1) -> select(point(X2,Y1), Lst1, Lst2)
	;   Lst2 = [point(X2, Y1)|Lst1]),

	assert(top(Lst2)).


  

You may also check:How to resolve the algorithm Look-and-say sequence step by step in the K programming language
You may also check:How to resolve the algorithm Loops/Wrong ranges step by step in the Maple programming language
You may also check:How to resolve the algorithm Prime numbers whose neighboring pairs are tetraprimes step by step in the Phix programming language
You may also check:How to resolve the algorithm Stack step by step in the Elisa programming language
You may also check:How to resolve the algorithm Guess the number step by step in the VBScript programming language