首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当我运行它时,改变弹簧长度的循环不会改变任何东西

当我运行它时,改变弹簧长度的循环不会改变任何东西
EN

Stack Overflow用户
提问于 2017-05-22 21:58:02
回答 1查看 62关注 0票数 0

我正在做的项目应该是模拟一个脉冲沿着一组由弹簧连接的球体移动。当脉冲沿着链条移动时,我试图减少弹簧长度,但当我运行它时,什么也没有发生。

下面是我的代码:

代码语言:javascript
复制
    from visual import *

one = sphere(pos=(-10,0,0), radius = 0.5, color = color.red)
two = sphere(pos=(-8,0,0), radius = 0.5, color = color.orange)
three = sphere(pos=(-6,0,0), radius = 0.5, color = color.yellow)
four = sphere(pos=(-4,0,0), radius = 0.5, color = color.green)
five = sphere(pos=(-2,0,0), radius = 0.5, color = color.blue)
six = sphere(pos=(0,0,0), radius = 0.5, color = color.cyan)
seven = sphere(pos=(2,0,0), radius = 0.5, color = color.magenta)
eight = sphere(pos=(4,0,0), radius = 0.5, color = color.white)
nine = sphere(pos=(6,0,0), radius = 0.5, color = color.red)
ten = sphere(pos=(8,0,0), radius = 0.5, color = color.orange)

spring1 = helix(pos = (-10, 0, 0), length = 2, radius = 0.3,
   thickness = 0.05, color = color.red)
spring2 = helix(pos = (-8, 0, 0), length = 2, radius = 0.3,
   thickness = 0.05, color = color.orange)
spring3 = helix(pos = (-6, 0, 0), length = 2, radius = 0.3,
   thickness = 0.05, color = color.yellow)
spring4 = helix(pos = (-4, 0, 0), length = 2.0, radius = 0.3,
   thickness = 0.05, color = color.green)
spring5 = helix(pos = (-2, 0, 0), length = 2.0, radius = 0.3,
   thickness = 0.05, color = color.blue)
spring6 = helix(pos = (0, 0, 0), length = 2.0, radius = 0.3,
   thickness = 0.05, color = color.cyan)
spring7 = helix(pos = (2, 0, 0), length = 2.0, radius = 0.3,
   thickness = 0.05, color = color.magenta)
spring8 = helix(pos = (4, 0, 0), length = 2.0, radius = 0.3,
   thickness = 0.05, color = color.white)
spring9 = helix(pos = (6, 0, 0), length = 2.0, radius = 0.3,
   thickness = 0.05, color = color.red)

masses = [one, two, three, four, five, six, seven, eight, nine, ten]
springs = [spring1, spring2, spring3, spring4, spring5, spring6, spring7,
           spring8, spring9]

while True:
    n=0
    deltax=.2
    while n < 10:
        rate(30)
        masses[n].pos.x = masses[n].pos.x + deltax
        if n < 9:
            springs[n].pos = masses[n].pos
            springs[n].axis = masses[n+1].pos-masses[n].pos
        n=n+1

    n = n-1
    while n >= 0:
        rate(30)
        masses[n].pos.x = masses[n].pos.x - deltax
        if n < 0:
            springs[n-1].pos = masses[n-1].pos - deltax
            springs[n-1].axis = masses[n].pos-masses[n-1].pos
        n = n-1


while True:
    m=0
    deltat=.2
    while m<9:
        rate(30)
        springs[m].length = springs[m].length - deltat
        springs[m].length = springs[m].length + deltat
        m=m+1

    m=m-1
    while n>=0:
        rate(30)
        springs[m].length = springs[m].length - deltat
        springs[m].length = springs[m].length + deltat
        m=m-1
EN

回答 1

Stack Overflow用户

发布于 2017-06-23 04:24:14

使质量具有不透明度= 0.3,并将第一速率语句替换为scene.waitfor(‘单击’)。你会看到当你向右移动第n个质量时,弹簧确实变短了,当移动第(n+1)个质量时,右边的弹簧变短了,在第n个弹簧和第(n+1)个弹簧之间留下了一个间隙。

顺便说一句,我不明白哪里有第二个"while True“。它永远不会到达。

我将在VPython论坛上发布一个更好的提出VPython问题的地方:

https://groups.google.com/forum/?fromgroups&hl=en#!forum/vpython-users

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44114790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档