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

Accueil Tutoriels Exemples guidés

New : Program a MathGraph32 figure in Python

modification vendredi 29 mars 2024.

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



New version 7.9.1 aloows you now to online create objects in a MathGraph32 figure using language Python.

To highlight this new functionnality we will show here to examples.

Other articles wll follow soon.

Please just notice that, above the window for the Python code you have a link on the MathGraph32 API documentation.

Firts basic example : copy the Python code here under and paste it in the black code area (code Python) then click on button Execute under the code editing frame :

from mathgraph import *
# don't modify the line above
# aso you ca use MathGraph32 API functions, for instance
# A = addPointXY(3, 3, 'A')
A = addPointXY(-5, 4, 'A') # Equivalent à A = addPointXY({'x': -5, 'y': 4, 'name': 'A'})
B = addFreePoint(6, -3, 'B')
d1 = addLineAB(A, B)
d2 = addLinePerp(A, d1, '', 'blue', '.', 3)
C = addMidpoint(A, B)
c1 = addCircleOA(C, A, 'red', '--', 2)
D = addPointXY(7, 8, 'D', 'red')
poly = addPolygon([A, B, D], '#facc2e')
setThickness(poly, 3)
addSurface(poly, 'yellow')
AB = addLengthMeasure(A, B)
c2 = addCircleOr(C, 'AB*sqrt(2)/2', 'maroon', '-', 2)

To be noticed : this Python code needs a figure with a frame (system of axis). You can change the basic figure on which the Python code is applied expanding the choice list under the figure.

In line B = addFreePoint(6, -3, ’B’), the coordonates 6 et -3 give tj=he starting position of the free point libre B but then you can move it where you want (point B is not llinked to the frame).

You will notice that the figure is dynamic : if you capture point B B, the circle of center C and radius AB*sqrt(2)/2 is updated dynamically. This is obtained because the code line AB = addLengthMeasure(A, B) has created in MathGraph32 a measure of length AB ans so we can use the following formula in the radius of the next circle , defined by formula ’AB*sqrt(2)/2’.

You will find at the bottom of this page a more sophisticated example.

Once the Python code executed, two buttons under the figure allow you to :

- get the base64 code of the figure (button Copy the figure code)

- save the figure in an mgj file (MathGraph32 file) (button Download the figure) (this method is recommended for big figures like the secodn example).

Then you can open the generated figure in MathGraph32 software
Vous pourrez ainsi ouvrir ensuite la figure générée avec le logiciel MathGraph32.

Here is now a more sophisticated example that will, in a recursive way, create nested circles.

Copy the code underneath and paste in in the code area after erasing it’s content. Tjen click on button Execute.

You will notice that the figure is dynamic : you can capture point A. Then you can replace profmax = 3 by profmax = 4. With an upper value the number of objects will exceed the limit (250 000 objects foor now).
With profmax = 4 lthe number of created objects is already around 56 000(and each time prof max is increased by 1, the number of objects is multiplicated by 6).

The code Python to paste in the Python code area :

from mathgraph import *
# Don't go over profMax = = 4
profmax = 3
colors = ['red', 'yellow', 'cyan', 'red', 'blue', 'red']
def color(index):
  i = (profmax - index) % len(colors)
  return colors[i]
dim = getFigDim()
width = dim[0]
height = dim[1]
k1 = addCalc('k1', '4-2*sqrt(3)') #Quicker to pass a MathGraph32 object than a Python numeric value
k2 = addCalc('k2', '(12-2*sqrt(3))/11') # same remark
ang60 = addCalc('ang60', '60') #idem

def cerclesTangents(O, A, prof):
  ce = addCircleOA(O, A)
  col = color(prof)
  addSurface(ce, col)
  P = addImPointRotation(A, O, ang60)
  setHidden(P)
  B = addImPointRotation(P, O, ang60)
  setHidden(B)
  Q = addImPointRotation(B, O, ang60)
  setHidden(Q)
  C = addImPointRotation(Q, O, ang60)
  setHidden(C)
  R = addImPointRotation(C, O, ang60)
  setHidden(R)
  I = addImPointDilation(A, O, k1)
  setHidden(I)
  E = addImPointDilation(P, O, k2)
  setHidden(E)
  J = addImPointDilation(B, O, k1)
  setHidden(J)
  F = addImPointDilation(Q, O, k2)
  setHidden(F)
  K = addImPointDilation(C, O, k1)
  setHidden(K)
  G = addImPointDilation(R, O, k2)
  setHidden(G)
  c1 = addCircleOA(I, A)
  c2 = addCircleOA(E, P)
  c3 = addCircleOA(J, B)
  c4 = addCircleOA(F, Q)
  c5 = addCircleOA(K, C)
  c6 = addCircleOA(G, R)
  col = color(prof - 1)
  addSurface(c1, col)
  addSurface(c2, col)
  addSurface(c3, col)
  addSurface(c4, col)
  addSurface(c5, col)
  addSurface(c6, col)
 
  if (prof > 0):
      cerclesTangents(I, A, prof - 1)
      cerclesTangents(J, B, prof - 1)
      cerclesTangents(K, C, prof - 1)
      cerclesTangents(E, P, prof - 1)
      cerclesTangents(F, Q, prof - 1)
      cerclesTangents(G, R, prof - 1)

O1 = addFreePoint({'absCoord': True, 'x': width/2, 'y': height/2, 'name': 'Ω'})
A = addFreePoint({'absCoord': True, 'x': width/2, 'y': 40, 'name': 'A'})
setPointNameOffset(A, 0, -25) # ON décale le nom le décalage est donné en coordonnées SVG
cerclesTangents(O1, A, profmax)
addZoomButtons(1.1)

A lats remark :

You can replace the 3 Python code lines :

k1 = addCalc('k1', '4-2*sqrt(3)')
k2 = addCalc('k2', '(12-2*sqrt(3))/11')
ang60 = addCalc('ang60', '60')

by these ones (but you have to add a line import math at the beginning of the Pyton code) :

k1 = 4-2*math.sqrt(3)
k2 = (12-2*math.sqrt(3))/11')
ang60 = 60

But the execution of this code will consume more memory because in each code line such as I = addImPointDilation(A, O, k1), a new MathGraph32 calculation will be added. In the first version of the code, this line uses an only MathGraph32 calculation calculated before enterint the recursive loops.