Numworks Epsilon  1.4.1
Graphing Calculator Operating System
script_template.cpp
Go to the documentation of this file.
1 #include "script_template.h"
2 
3 namespace Code {
4 
5 constexpr ScriptTemplate emptyScriptTemplate(".py", R"(from math import *
6 )");
7 
8 constexpr ScriptTemplate factorialScriptTemplate("factorial.py", R"(def factorial(n):
9  if n == 0:
10  return 1
11  else:
12  return n * factorial(n-1))");
13 
14 constexpr ScriptTemplate fibonacciScriptTemplate("fibonacci.py", R"(def fibo(n):
15  a=0
16  b=1
17  for i in range(1,n+1):
18  c=a+b
19  a=b
20  b=c
21  return a
22 
23 def fibo2(n):
24  if n==0:
25  return 0
26  elif n==1 or n==2:
27  return 1
28  return fibo2(n-1)+fibo2(n-2))");
29 
30 constexpr ScriptTemplate mandelbrotScriptTemplate("mandelbrot.py", R"(# This script draws a Mandelbrot fractal set
31 # N_iteration: degree of precision
32 import kandinsky
33 def mandelbrot(N_iteration):
34  for x in range(320):
35  for y in range(222):
36 # Compute the mandelbrot sequence for the point c = (c_r, c_i) with start value z = (z_r, z_i)
37  z = complex(0,0)
38 # Rescale to fit the drawing screen 320x222
39  c = complex(3.5*x/319-2.5, -2.5*y/221+1.25)
40  i = 0
41  while (i < N_iteration) and abs(z) < 2:
42  i = i + 1
43  z = z*z+c
44 # Choose the color of the dot from the Mandelbrot sequence
45  rgb = int(255*i/N_iteration)
46  col = kandinsky.color(int(rgb),int(rgb*0.75),int(rgb*0.25))
47 # Draw a pixel colored in 'col' at position (x,y)
48  kandinsky.set_pixel(x,y,col))");
49 
50 constexpr ScriptTemplate polynomialScriptTemplate("polynomial.py", R"(from math import *
51 # roots(a,b,c) computes the solutions of the equation a*x**2+b*x+c=0
52 def roots(a,b,c):
53  delta = b*b-4*a*c
54  if delta == 0:
55  return -b/(2*a)
56  elif delta > 0:
57  x_1 = (-b-sqrt(delta))/(2*a)
58  x_2 = (-b+sqrt(delta))/(2*a)
59  return x_1, x_2
60  else:
61  return None)");
62 
64  return &emptyScriptTemplate;
65 }
66 
69 }
70 
71 const ScriptTemplate * ScriptTemplate::Fibonacci() {
73 }
74 
75 const ScriptTemplate * ScriptTemplate::Mandelbrot() {
77 }
78 
79 const ScriptTemplate * ScriptTemplate::Polynomial() {
81 }
82 
83 }
84 
85 
constexpr ScriptTemplate fibonacciScriptTemplate("fibonacci.py", R"(def fibo(n): a=0 b=1 for i in range(1,n+1): c=a+b a=b b=c return a def fibo2(n): if n==0: return 0 elif n==1 or n==2: return 1 return fibo2(n-1)+fibo2(n-2))")
static const ScriptTemplate * Empty()
constexpr ScriptTemplate polynomialScriptTemplate("polynomial.py", R"(from math import * # roots(a,b,c) computes the solutions of the equation a*x**2+b*x+c=0 def roots(a,b,c): delta = b*b-4*a*c if delta == 0: return -b/(2*a) elif delta > 0: x_1 = (-b-sqrt(delta))/(2*a) x_2 = (-b+sqrt(delta))/(2*a) return x_1, x_2 else: return None)")
Definition: app.cpp:7
constexpr ScriptTemplate emptyScriptTemplate(".py", R"(from math import * )")
static const ScriptTemplate * Mandelbrot()
static const ScriptTemplate * Fibonacci()
constexpr ScriptTemplate mandelbrotScriptTemplate("mandelbrot.py", R"(# This script draws a Mandelbrot fractal set # N_iteration: degree of precision import kandinsky def mandelbrot(N_iteration): for x in range(320): for y in range(222): # Compute the mandelbrot sequence for the point c = (c_r, c_i) with start value z = (z_r, z_i) z = complex(0,0) # Rescale to fit the drawing screen 320x222 c = complex(3.5*x/319-2.5, -2.5*y/221+1.25) i = 0 while (i < N_iteration) and abs(z) < 2: i = i + 1 z = z*z+c # Choose the color of the dot from the Mandelbrot sequence rgb = int(255*i/N_iteration) col = kandinsky.color(int(rgb),int(rgb*0.75),int(rgb*0.25)) # Draw a pixel colored in 'col' at position (x,y) kandinsky.set_pixel(x,y,col))")
static const ScriptTemplate * Factorial()
constexpr ScriptTemplate factorialScriptTemplate("factorial.py", R"(def factorial(n): if n == 0: return 1 else: return n * factorial(n-1))")
static const ScriptTemplate * Polynomial()