使用kivy GUI图形库可视化的数学函数。
函数是:
1.数学公式
2.python实现代码:
from kivy.app import App
from kivy.uix.image import Image
from kivy.uix.widget import Widget
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.graphics import Color, Ellipse, Line, Rectangle
import numpy as np
from random import randint
class MathLine:
@staticmethod
def get_grandient_color(start_color, end_color, i):
step = 1.0*i*10/10000
return (
start_color[0] + step * (end_color[0] - start_color[0]),
start_color[1] + step * (end_color[1] - start_color[1]),
start_color[2] + step * (end_color[2] - start_color[2])
)
@staticmethod
def fish_line(i):
r = 200
t = np.linspace(2 * np.pi * i/10000, 2 * np.pi * (i+1)/10000, 2)
# p = r * (0.7 + 2.6 * np.cos(t) + 1.3 * np.power(np.sin(90 * t), 3))
p = r * (0.7 + 2.6 * np.cos(t) + 2 * np.power(np.sin(90 * t), 3))
x = 1280 + p * np.sin(t)
y = 400 + p * np.cos(t)
points = [(x[i], y[i]) for i in range(len(x))]
return points
class MathCanvas(Image):
colors = [
(1.0, 0.0, 0.0),
(0.0, 1.0, 0.0),
(0.0, 0.0, 1.0),
(0.0, 1.0, 1.0),
(1.0, 1.0, 0.0),
(1.0, 0.0, 1.0)
]
index = 0
def __init__(self):
super(MathCanvas, self).__init__()
self.i = 0
self.j = 0
self.clk = Clock.schedule_interval(self.paint, 1.0 / 1000)
with self.canvas:
Color(0, 0, 0, 255)
Rectangle(pos=(0, 0), size=(2560, 1600))
self.paint()
# 图形绘制
def paint(self, dt=0):
self.i += 1
self.j += 1
if self.j % 1000 == 0:
self.index += 1
self.index %= 5
self.j = 0
#self.canvas.clear()
with self.canvas:
# Color(0, 0, 0, 255)
# Rectangle(pos=(0, 0), size=(2560, 1600))
clr = MathLine.get_grandient_color(
self.colors[self.index],
self.colors[self.index+1],
self.j)
# print(clr)
Color(clr[0], clr[1], clr[2], 1.0)
Line(points=MathLine.fish_line(self.i))
class MyApp(App):
def __init__(self, **kwargs):
super(MyApp, self).__init__(**kwargs)
Window.fullscreen = "auto"
self.math_canvas = MathCanvas()
def build(self):
return self.math_canvas
MyApp().run()
3.数学图形
数学图形