设为首页收藏本站订阅更新

无忧脚本

 找回密码
 加入无忧

QQ登录

只需一步,快速开始

查看: 2423|回复: 5

[讨论] 请高手来解决一个JS钟摆问题 [复制链接]

Rank: 3Rank: 3

升级  11.67%

注册时间
2006-2-6
威望
134
阅读权限
30
积分
235
帖子
113
精华
0
UID
46652
状态
当前离线
发表于 2008-2-29 20:32:55 |显示全部楼层 |串个门|加好友|打招呼|发消息 |
查看详细资料 QQ 查看个人网站
一键分享 一键分享
这是我自己写的一个,本来想做成一个钟摆的,就是用鼠标拉到某个位置后,自动掉下来,
按照能量守恒定律进行力学摆动。

但是因为公式不知道,还有一些莫名的数学BUG,实在写不下去了,请高手帮忙修改!!

  1. <html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <!--[if !mso]>
  5. <style>
  6. v\:*         { behavior: url(#default#VML) }
  7. o\:*         { behavior: url(#default#VML) }
  8. .shape       { behavior: url(#default#VML) }
  9. </style>
  10. <![endif]-->
  11. <style>
  12. #div{
  13.         position:absolute;
  14.         width:50px;
  15.         height:50px;
  16.         border:1px #333 solid;
  17.         background-color:#666699;
  18.         color:#FFF;
  19.         cursor:pointer;
  20.         filter: progid:DXImageTransform.Microsoft.Matrix(M11=1,M12=0,M21=0,M22=1,SizingMethod="auto expand");
  21.         z-index:2;
  22.         font-size:16px;
  23.         text-align:center;
  24.         line-height:50px;
  25.         overflow:hidden;
  26. }
  27. #line{
  28.         position:absolute;
  29.         left:0;
  30.         top:0;
  31.         z-index:1;
  32. }
  33. </style>
  34. <script language="javascript">
  35. var div=null;
  36. var line=null;
  37. function config()
  38. {
  39.         div=document.getElementById("div");
  40.         line=document.getElementById("line");
  41.        
  42.         div.width=parseInt(div.currentStyle.width);
  43.         div.height=parseInt(div.currentStyle.height);
  44.         div.q=0;
  45.        
  46.         line.strokecolor="#000";
  47.         line.strokeweight="1px";
  48.         line.from="200,200";
  49.         line.to="200,300";

  50.         line.pleft=300;
  51.         line.ptop=200;
  52.         line.plength=200;
  53.        
  54.         line.from=line.pleft+","+line.ptop;
  55.        
  56.         document.body.onselectstart=function(){event.returnValue=false;};
  57.         div.setpos=div_setpos;
  58.         div.setrote=div_setrote;
  59.         div.setpos(0,200,true);
  60.         div.setpos(200,200);
  61.         div.setrote();
  62.        
  63.         div.onmousedown=div_onmousedown;
  64.         div.onmousemove=div_onmousemove;
  65.         div.onmouseup=div_onmouseup;
  66.        
  67. }
  68. function div_onmousedown()
  69. {
  70.         div.setCapture();
  71.         div.preleft=div.xleft;
  72.         div.pretop=div.xtop;
  73.         div.prex=event.x;
  74.         div.prey=event.y;
  75. }
  76. function div_onmousemove()
  77. {
  78.         if(event.button!=1)
  79.                 return;
  80.         var x=event.x-div.prex+div.preleft;
  81.         var y=event.y-div.prey+div.pretop;
  82.         div.setpos(x,y);
  83. }
  84. function div_onmouseup()
  85. {
  86.         div.releaseCapture();
  87. }
  88. function div_setrote()
  89. {
  90.        
  91. }
  92. function div_setpos(xleft,xtop,byhand)
  93. {
  94.         div.xleft=xleft;
  95.         div.xtop=xtop;
  96.         if(byhand==true){
  97.                 div.style.left=line.pleft+xleft-div.width/2;
  98.                 div.style.top=line.ptop+xtop-div.height/2;
  99.                 line.to=parseInt(div.style.left)+div.width/2+","+parseInt(div.style.top);
  100.         }else{
  101.                 var qa=0;
  102.                 if(xtop==0){
  103.                         qa=Math.PI/2;
  104.                 }else{
  105.                         qa=Math.atan(xleft/xtop);
  106.                 }
  107.                 var lx=Math.abs(parseInt((div.width/2)*Math.cos(qa)));
  108.                 var ly=Math.abs(parseInt((div.width/2)*Math.sin(qa)));

  109.                 if(xleft>=0){
  110.                         div.style.left=line.pleft+xleft-lx;
  111.                         div.style.top=line.ptop+xtop-ly;
  112.                         line.to=(line.pleft+xleft)+","+(line.ptop+xtop);
  113.                 }else{
  114.                         var x1=Math.abs(div.width*Math.tan(qa));
  115.                         var x2=div.height-x1;
  116.                         var w2=x2*Math.sin(qa);
  117.                         var w1=x1/Math.sin(qa);
  118.                         var w=Math.abs(w1)+Math.abs(w2);

  119.                         div.style.left=line.pleft+xleft-(w-lx);
  120.                         div.style.top=line.ptop+xtop-ly;

  121.                         line.to=(line.pleft+xleft)+","+(line.ptop+xtop);
  122.                 }
  123.                 with(div.filters.item(0)){
  124.                         M11=Math.cos(qa);
  125.                         M12=Math.sin(qa);
  126.                         M21=-Math.sin(qa);
  127.                         M22=Math.cos(qa);
  128.                 }

  129.                
  130.         }       
  131. }
  132. window.attachEvent("onload",config);
  133. </script>
  134. </head>

  135. <body>

  136. <div id="div">重</div>
  137. <v:line id="line"></v:line>

  138. </body>
  139. </html>
复制代码运行代码另存代码
不用谢,大家爽,才是真的爽

超级版主

5毛发一贴,千里不留行。

Rank: 8Rank: 8

注册时间
2007-2-27
威望
3584
阅读权限
150
积分
8408
帖子
3597
精华
12
UID
65747
状态
当前离线
发表于 2008-3-1 10:38:48 |显示全部楼层 |串个门|加好友|打招呼|发消息 |
查看详细资料
公式就是能量守恒 m*g*h=m(v^2)/2

使用道具 举报

Rank: 3Rank: 3

升级  11.67%

注册时间
2006-2-6
威望
134
阅读权限
30
积分
235
帖子
113
精华
0
UID
46652
状态
当前离线
发表于 2008-3-6 14:20:37 |显示全部楼层 |串个门|加好友|打招呼|发消息 |
查看详细资料 QQ 查看个人网站
这个公式我知道,只是要让它动起来,我需要根据时间t来计算h

在知道时间t的情况下不知道怎样跟这个共识关联起来。
不用谢,大家爽,才是真的爽

使用道具 举报

超级版主

5毛发一贴,千里不留行。

Rank: 8Rank: 8

注册时间
2007-2-27
威望
3584
阅读权限
150
积分
8408
帖子
3597
精华
12
UID
65747
状态
当前离线
发表于 2008-3-6 14:48:03 |显示全部楼层 |串个门|加好友|打招呼|发消息 |
查看详细资料
就是想知道高度和时间的函数关系呗

使用道具 举报

超级版主

5毛发一贴,千里不留行。

Rank: 8Rank: 8

注册时间
2007-2-27
威望
3584
阅读权限
150
积分
8408
帖子
3597
精华
12
UID
65747
状态
当前离线
发表于 2008-3-6 15:14:52 |显示全部楼层 |串个门|加好友|打招呼|发消息 |
查看详细资料
是这样滴
单摆的运动方程基本上是解不出来滴 假如摆角是x,x''是x对时间的二阶导数 最后求出来的大概是这么个东西
x'' * l = - g * Sin x.
但是x比较小的时候可以Sin x ≈ x 这样单摆就变成和弹簧类似的东西了 然后可以很容易的解掉 所以只要你不把线拉得很远还是比较好办的
不过你如果很bt的话 完全可以用程序模拟这个微分方程 或者数值计算你比较在行的话(数值计算我是小白) 这个微分方程就不是问题了

使用道具 举报

Rank: 8Rank: 8

注册时间
2005-3-9
威望
1952
阅读权限
150
积分
4347
帖子
1577
精华
9
UID
24714
状态
当前离线
发表于 2008-3-6 17:17:02 |显示全部楼层 |串个门|加好友|打招呼|发消息 |
查看详细资料 QQ 查看个人网站
小摆角的单摆(<5度)可以用无阻尼简谐振动方程来近似:

x'' + x = 0

使用道具 举报

您需要登录后才可以回帖 登录 | 加入无忧

Archiver|手机版|无忧脚本 ( 苏ICP备05080427号 )|值班电话:027-62300445  

GMT+8, 2012-2-7 21:29 , Processed in 0.062043 second(s), 14 queries , Gzip On, Memcache On.

Powered by Discuz! X2

© 1999-2011 无忧脚本

回顶部