How to resolve the algorithm Sierpinski pentagon step by step in the XPL0 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sierpinski pentagon step by step in the XPL0 programming language

Table of Contents

Problem Statement

Produce a graphical or ASCII-art representation of a Sierpinski pentagon (aka a Pentaflake) of order 5. Your code should also be able to correctly generate representations of lower orders: 1 to 4.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sierpinski pentagon step by step in the XPL0 programming language

Source code in the xpl0 programming language

def  Order = 5;         \can also set this to 1, 2, 3, or 4
def  Width=640, Height=640;
def  Pi = 3.14159265358979323846;
def  Deg72 = 72.*Pi/180.;       \72 degrees in radians
def  HW = Width/2;
def  Margin = 20;
def  Radius = HW - 2*Margin;
real ScaleFactor;
int  ColorIndex;

proc DrawPentagon(X, Y, Side, Depth);
real X, Y, Side; int Depth;
real Angle, Dist;
int  I;
[Angle:= 3. * Deg72;
if Depth = 0 then
        [Move(fix(X), fix(Y));
        for I:= 0 to 4 do
            [X:= X + Cos(Angle) * Side;
             Y:= Y - Sin(Angle) * Side;
             Line(fix(X), fix(Y), ColorIndex+9);
             Angle:= Angle + Deg72;
            ];
        ColorIndex:= ColorIndex+1;
        if ColorIndex >= 5 then ColorIndex:= 0;
        ]
else    [Side:= Side * ScaleFactor;
        Dist:= Side * (1. + Cos(Deg72) * 2.);
        for I:= 0 to 4 do
            [X:= X + Cos(Angle) * Dist;
             Y:= Y - Sin(Angle) * Dist;
             DrawPentagon(X, Y, Side, Depth-1);
             Angle:= Angle + Deg72;
            ];
        ];
];

real Side;
[SetFB(Width, Height, 8);
ScaleFactor:= 1. / (2. + Cos(Deg72) * 2.);
ColorIndex:= 0;
Side:= float(Radius) * Sin(Pi/5.) * 2.;
DrawPentagon(float(HW), float(3*Margin), Side, Order-1);
]

  

You may also check:How to resolve the algorithm Parse an IP Address step by step in the Nim programming language
You may also check:How to resolve the algorithm Factorial step by step in the Picat programming language
You may also check:How to resolve the algorithm Doubly-linked list/Element definition step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Doubly-linked list/Element definition step by step in the Rust programming language
You may also check:How to resolve the algorithm Bitwise operations step by step in the 6502 Assembly programming language