Logiciel libre de géométrie, d'analyse et de simulation multiplateforme par Yves Biton

Accueil Tutoriels API Javascript, Python

Programming a figure in Python : nested polygons

modification mercredi 26 mars 2025.

Toutes les versions de cet article : [English] [français]



Since version 7.9.1, MathGraph32 allows the creation of objects in Python language.

The figure underneath creates four free points A, B, C and D, then calls in a recursive way the function named poly. this functione adds to the previous polygon a new inscripted polygone using image points by a dilation.

If you change the Python code undernath, click on the button Execute.

To be noted : the figure created is dynamic : you ca capture points A, B, C and D.

You will find underneath the HTML code that allowed this HTML age using a HTML tag (tag specific to MathGraph32).

Please note thatn once the Python code executd, a click on the button Download the figure will allow you to save the generated figure in a mgj file.

See some explications on the used Python code.

This figure does’nt need a frame (set of axes) to be executed..
Line [width, height] = getFigDim() returns in width and height the figure dimensions (dimensions of the SVG element containing the figure).

The four following lines :

A = addFreePoint({'x':width/8, 'y':7*height/8, 'name':'A', 'color':'black', 'absCoord': True})
B = addFreePoint({'x':7*width/8, 'y':7*height/8, 'name':'B', 'color':'black', 'absCoord': True})
C = addFreePoint({'x':7*width/8, 'y':height/8, 'name':'C', 'color':'black', 'absCoord': True})
D = addFreePoint({'x':width/8, 'y':height/8, 'name':'D', 'color':'black', 'absCoord': True})

create four free points A, B, C and D defined by their coordinates relative to the SVG containing the figure (origine at the top left corner, abscissas increasing from lleft to right, ordinates increasing from top to bottom).

Here we use the object syntax in order to be able to use parameter absCoord.

Parameter ’absCoord’ set to True implies that we don not use the coordinates in a frame (set of axis), event if a frame is present in the figure.

The line k = addCalc('k', '1/20') adds to the MathGraph32 figure a calculation named k and this calculation will be used by all the dilations used in the poly function.

Here is the HTML used to generate this web page :

<script type = "text/mtgPython" id = "PythonCodeId">
[width, height] = getFigDim()
A = addFreePoint({'x':width/8, 'y':7*height/8, 'name':'A', 'color':'black', 'absCoord': True})
B = addFreePoint({'x':7*width/8, 'y':7*height/8, 'name':'B', 'color':'black', 'absCoord': True})
C = addFreePoint({'x':7*width/8, 'y':height/8, 'name':'C', 'color':'black', 'absCoord': True})
D = addFreePoint({'x':width/8, 'y':height/8, 'name':'D', 'color':'black', 'absCoord': True})
k = addCalc('k', '1/20')
profmax = 100
colors = ['red', 'yellow', 'cyan', 'grey', 'magenta', 'maroon']
def color(index):
    i = index % len(colors)
    return colors[i]
polyInit = [A, B, C, D]
def poly(tab, prof):
    pol = addPolygon(tab, 'black')
    addSurface(pol, color(profmax - prof))
    a = tab[0]
    b = tab[1]
    c = tab[2]
    d = tab[3]
    M = addImPointDilation(b, a, k)
    setHidden(M)
    N = addImPointDilation(c, b, k)
    setHidden(N)
    P = addImPointDilation(d, c, k)
    setHidden(P)
    Q = addImPointDilation(a, d, k)
    setHidden(Q)
    if (prof > 0):
        poly([M, N, P, Q], prof - 1)

poly([A, B, C, D], profmax)

</script>
<script async src="https://www.mathgraph32.org/js/mtgLoad/mathgraphElements.js"></script>
<mathgraph-player width="800" height="600" python-code-id="PythonCodeId" >
</mathgraph-player>