正弦演绎

0 Comments

Python正弦动画演示

Python正弦动画演示

"""
   正弦演绎.py,正弦是三角形一个锐角的对边长度除以斜边长度的值,
   而余弦则是邻边长度除以斜边长度的值,请修改程序,显示余弦值,
   余弦记作cos(angle)
"""
import time
import turtle
from sympy import *

def mark(w,pos,char,bg,fg):
    """标注一个符号在圆点上,pos:坐标,char:字符,bg:背景色,fg:前景色"""
    c = w.pencolor()       # 记录画笔颜色
    w.penup()
    w.goto(pos)
    w.pencolor(fg)    
    w.dot(20,bg)
    w.sety(w.ycor()-8)
    w.setx(w.xcor()+2)    
    w.write(char,align='center',font=('',12,'normal'))
    w.sety(w.ycor()+8)
    w.setx(w.xcor()-2)
    w.pencolor(c)
    
def draw_line(w,l):
    p1,p2 = l.points
    p1,p2 = p1.evalf(),p2.evalf()     
    w.penup()
    w.goto(p1)
    w.pd()
    w.goto(p2)
    w.pu()
    
def draw_arc(w,radius,angle):
    """w是海龟,radius是弧的半径,angle是角度"""
    w.home();w.fd(radius);w.left(90)
    w.pd();w.circle(radius,angle);w.pu()
    
turtle.tracer(0,0)
turtle.speed(0);turtle.ht()
turtle.fd(300);turtle.stamp();turtle.bk(600);turtle.fd(300);
turtle.left(90)
turtle.fd(300);turtle.stamp();turtle.bk(600);turtle.home()

p0 = Point(0,0)
ab = Segment(p0,Point(250,0))         # 线段

turtle.sety(200);turtle.write('正 弦 演 绎',align='center',font=('',22,'bold'))

w = turtle.Turtle('blank')
w.speed(0)
draw_line(w,ab)

for angle in range(1,61):    # 角度制的度数
    w.clear()    
    ac = ab.rotate(angle*3.1415926/180,p0) # 绕p0旋转(如果用pi,会很慢)
    
    C = ac.points[1]         # C点
    B = Point(C.x,0)         # B点
    cb_len = C.y             # cb的长度就是C点y坐标    
    value = cb_len / ac.length   # 角CAB的正弦值,对边/斜边
    value = value.evalf()
    
    draw_line(w,ac)
    draw_line(w,Line(C,B))
    draw_arc(w,20,angle)
    w.goto(0,-100)
    s = "sin(" + str(angle) + ")= BC/AC = " + str(value)
    w.write(s,align='center',font=('',14,'normal'))
    mark(w,(-15,-15),"A","blue","white")
    mark(w,(B.x,-15),"B","blue","white")
    mark(w,(C.x,C.y+15),"C","blue","white")

    turtle.update()    



发表评论