📜  使用Python可视化正弦波

📅  最后修改于: 2022-05-13 01:54:36.038000             🧑  作者: Mango

使用Python可视化正弦波

正弦波是最基本的三角周期曲线,也称为正弦曲线。在这里,我们将看到正弦函数如何与圆相关。尽管正弦函数是函数,它更多的是一个圆圈,而不是一个三角形。

考虑一个简单的圆方程:

x^2 + y^2 = r^2

其中 r 是圆心在原点 (0,0) 的圆的半径,如下图所示。

正弦就是随着角度的增加,从原点开始的 y 位移的测量,如下图所示:



我们可以使用Pythonpygame模块可视化这个正弦定义。 Pygame 是一个开源的Python包,主要用于创建视频游戏。

方法:

  • 创建一个半径为r的圆。
  • 绘制圆的半径。半径的端点将是 (0,0) 和 (r*cos a, r*sin a),其中点 (r*cos a, r*sin a) 将始终是圆的。
  • 绘制正弦曲线
  • 然后画一条线,将连接正弦波的起点和圆的半径终点。为简单起见,该长度称为间隙

注:这里的半径位置是指圆的半径的头部位置。腹肌

绘制正弦曲线

绘制一个圆并为其半径设置动画,使半径的端点将覆盖圆圆周上的所有点。这可以使用无限 while 循环并从圆心画一条线到 ( r * cos t,r * sin t ) 来完成。然后声明一个列表Ys来存储列表开头的所有r*sin t值。

这将基本上跟踪从半径的初始位置到其当前新位置的所有r*sin t值。稍后将使用这些值来显示正弦曲线。创建一个 for 循环来遍历Ys的元素,然后绘制一个半径为 1 和宽度为 1 的圆,横坐标从 0 开始到 len(Ys),纵坐标将是相应的 Ys 值,即Ys[i] ,其中 index i[0, len(Ys)-1]。横坐标需要移动一定量,足以使动画整洁。

在动画中,间隙由用户可以增加或减少的黑线显示。

下面是实现:

Python3
import numpy as np
import pygame
from pygame.locals import *
 
 
class trig:
 
    # to collect all the ordinates
    Ys = []
 
    def __init__(self, width=1600, height=900,
                 gap=100, fps=60, radius=100):
 
        # width of the window
        self.width = width
 
        # height of the window
        self.height = height
 
        # frame rate per second
        self.fps = fps
        self.screen = pygame.display.set_mode((self.width,
                                               self.height))
 
        # setting the screen dimensions
        self.clock = pygame.time.Clock()
 
        # the distance between the radius
        self.gap = gap
 
        #  pointer and the starting point of the curve
 
        # the will be the x axis
        self.t = 0
 
        # length of the radius of the circle
        self.r = radius
 
        self.run = True
        while self.run:
            self.clock.tick(self.fps)
 
            # filling the whole canvas with white background
            self.screen.fill('white')
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.run = False
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    pygame.quit()
 
            # center of the circle
            x, y = 400, 400
 
            x += (self.r * np.cos(self.t))
            y += (self.r * np.sin(self.t))
            pygame.draw.line(self.screen, 'grey',
                             (400, 400),
                             (400+1000, 400), 3)
 
            # this will create a horizontal line
 
            pygame.draw.line(self.screen,
                             'grey',
                             (400, 400 + self.r),
                             (400+1000, 400+self.r), 3)
 
            # this will create a horizontal line above the circle
            pygame.draw.line(self.screen,
                             'grey',
                             (400, 400 - self.r),
                             (400+1000, 400-self.r),
                             3)
 
            # this will create a horizontal
            # line below the circle
            pygame.draw.circle(self.screen,
                               'blue',
                               (400, 400),
                               self.r, 5)
 
            # this will create a circle
            # with center (400,400)
            pygame.draw.line(self.screen,
                             'green',
                             (400, 400),
                             (x, y), 3)
 
            # this will draw the radius of the circle
 
            # inserting the y values
            # at the beginning of the Ys list
            self.Ys.insert(0, y)
 
            if len(self.Ys) > 1100 - self.gap:
                self.Ys.pop()
 
            # this will restrict the length
            # of the Ys to a certain limit
            # so that the animation
            # doesn't get out of the screen
 
            pygame.draw.line(self.screen, 'black', (x, y),
                             (400+self.gap, self.Ys[0]), 3)
 
            # this will create the joining line
            # between the curve and the circle's radius
 
            for i in range(len(self.Ys)):
                pygame.draw.circle(self.screen, 'red',
                                   (i+400+self.gap, self.Ys[i]), 1, 1)
 
                # this will create the sin curve
                # it will create bunch of small circles
                # with varying centers in such a
                # way that it will trajectory of
                # the centers of all those small circles
                # will give rise to a sine curve
 
            if event.type == KEYDOWN:
                if event.key == K_RIGHT:
                    self.gap += 1
                if event.key == K_LEFT:
                    self.gap -= 1
 
             # this part of code gives the user
             # the freedom to set the speed of the
             # animation and also set the gap
             # between the circle and the sine curve
 
            self.t += 0.01
            pygame.display.update()
 
 
if __name__ == '__main__':
    sin = trig()
    pygame.quit()


输出:

注意:使用左右箭头减小或增大圆与正弦曲线之间的间隙。使用ECS退出窗口或 QUIT。