JS 的 Math.sin() 与 Math.cos() 用法(小球的曲线运动)

本文介绍了如何使用JavaScript的Math.sin()和Math.cos()函数实现红色小球沿预设轨迹进行顺时针旋转,通过计算每个时刻的位置来模拟运动效果。

JS 的 Math.sin() 与 Math.cos() 用法(小球沿轨迹做运动)

功能实现后红色小球会沿着黑线做顺时针运动
在这里插入图片描述
上代码
在这里插入图片描述

## 功能快捷键

在这里插入图片描述

<html>

<head>

    <style type="text/css">
        .warp {
            position: relative;
            width: 500px;
            height: 120px;
            border-radius: 60px;
            margin: 200px auto;
            border: 1px solid #999
        }

        .box {
            width: 36px;
            height: 36px;
            position: absolute;
            top: -18px;
            left: 42px;
            background-color: red;
            border-radius: 50%;
        }
    </style>
    <script type="text/javascript" src="./jquery.min.js"></script>

</head>

<body>

    <div class="warp">
        <div class="box"></div>
    </div>


</body>

<script type="text/javascript">
    /** 
     * 公式: Math.sin(x) 和 Math.cos(x)  其中 x 是弧度
            弧度 = 角度 *(PI/180)
             a,b是圆心坐标,即(a,b);  r是圆半径;  
            一个圆为360°,一分钟为60秒,所以平均每秒的转动角度为 360°/60 = 6°,那么半圆的角度就是 3°

        综上:X坐标(left) = a + Math.sin(3 * (Math.PI / 180)) * r
              Y坐标(top) = b + Math.cos(3 * (Math.PI / 180)) * r
              注意:此处的“+”或“-”号,是可以控制运动方向的,即是顺时针还是逆时针
    **/
    var r = 60;  //半径  60-36/2=42   500-42-42=416    
    var n = 60;  // 左半圆的圆心坐标为(42,42)  右半圆的圆形坐标为(416,42)
    $(".box").animate({ left: 416, }, 3000, "linear", function () {
        var move1 = function () {
            $(".box").animate({
                left: 416 + Math.sin(3 * (Math.PI / 180) * (n)) * r,
                top: 42 + Math.cos(3 * (Math.PI / 180) * (n)) * r
            }, 30, "linear", function () {
                n--;
                if (n < 1) {
                    n = 60
                    $(".box").animate({ left: 42, }, 3000, "linear", function () {
                        var move2 = function () {
                            $(".box").animate({
                                left: 42 - Math.sin(3 * (Math.PI / 180) * (n)) * r,
                                top: 42 - Math.cos(3 * (Math.PI / 180) * (n)) * r
                            }, 30, "linear", function () {
                                n--;
                                if (n < 1) {
                                    n = 60
                                    $(".box").animate({ left: 416, }, 3000, "linear", function () {
                                        // move1();//回调函数
                                    })
                                } else {
                                    move2();//回调函数
                                }
                            });
                        }
                        move2();
                    });
                } else {
                    move1();//回调函数
                }
            });
        }
        move1();
    });
</script>

</html>
; ========== 1. 模型初始化 ========== model new model title "具有级配特征的骨架结构模型生成" ; 设置模型域范围匹配马歇尔试件尺寸 (单位:米) model domain extent -0.06 0.06 -0.06 0.06 -0.01 0.08 ; 设置随机数种子以保证结果可重复 model random 10001 ; ========== 2. 创建圆柱形墙 ========== wall generate name "sample_cylinder" cylinder ... base 0 0 0 ... ; 圆柱底面中心点坐标 (原点) axis 0 0 1 ... ; 圆柱的中心轴方向 (Z轴) height 0.065 ... ; 圆柱的高度 (65 mm) radius 0.0505 ... ; 圆柱的半径 (50.5 mm) cap true true ; 生成顶部和底部端盖 ; ========== 3. 导入STL文件并生成簇模板 ========== geometry import '1.stl' geometry import '2.stl' geometry import '3.stl' geometry import '4.stl' geometry import '5.stl' geometry import '6.stl' geometry import '7.stl' ; 为每个几何体创建簇模板 clump template create name 'aggregate1' geometry '1' ... bubblepack ratio 0.1 distance 150 ... surfcalculate clump template create name 'aggregate2' geometry '2' ... bubblepack ratio 0.1 distance 150 ... surfcalculate clump template create name 'aggregate3' geometry '3' ... bubblepack ratio 0.1 distance 150 ... surfcalculate clump template create name 'aggregate4' geometry '4' ... bubblepack ratio 0.1 distance 150 ... surfcalculate clump template create name 'aggregate5' geometry '5' ... bubblepack ratio 0.1 distance 150 ... surfcalculate clump template create name 'aggregate6' geometry '6' ... bubblepack ratio 0.1 distance 150 ... surfcalculate clump template create name 'aggregate7' geometry '7' ... bubblepack ratio 0.1 distance 150 ... surfcalculate ; ========== 4. 在模具内投放骨料颗粒 ========== fish define generate_aggregates local radius = 0.0505 ; 单位:米 ; 生成aggregate1(135个) loop i (1,135) local r1 = radius * math.sqrt(math.random.uniform) ; 确保均匀分布在圆内 local theta1 = 2.0 * math.pi * math.random.uniform local x1 = r1 * math.cos(theta1) local y1 = r1 * math.sin(theta1) local z1 = 0.005 + 0.055 * math.random.uniform ; z范围从5mm到60mm command clump replicate template 'aggregate1' position [x1] [y1] [z1] end_command end_loop ; 生成aggregate2(38个) loop i (1,38) local r2 = radius * math.sqrt(math.random.uniform) local theta2 = 2.0 * math.pi * math.random.uniform local x2 = r2 * math.cos(theta2) local y2 = r2 * math.sin(theta2) local z2 = 0.005 + 0.055 * math.random.uniform command clump replicate template 'aggregate2' position [x2] [y2] [z2] end_command end_loop ; 生成aggregate3(78个) loop i (1,78) local r3 = radius * math.sqrt(math.random.uniform) local theta3 = 2.0 * math.pi * math.random.uniform local x3 = r3 * math.cos(theta3) local y3 = r3 * math.sin(theta3) local z3 = 0.005 + 0.055 * math.random.uniform command clump replicate template 'aggregate3' position [x3] [y3] [z3] end_command end_loop ; 生成aggregate4(79个) loop i (1,79) local r4 = radius * math.sqrt(math.random.uniform) local theta4 = 2.0 * math.pi * math.random.uniform local x4 = r4 * math.cos(theta4) local y4 = r4 * math.sin(theta4) local z4 = 0.005 + 0.055 * math.random.uniform command clump replicate template 'aggregate4' position [x4] [y4] [z4] end_command end_loop ; 生成aggregate5(59个) loop i (1,59) local r5 = radius * math.sqrt(math.random.uniform) local theta5 = 2.0 * math.pi * math.random.uniform local x5 = r5 * math.cos(theta5) local y5 = r5 * math.sin(theta5) local z5 = 0.005 + 0.055 * math.random.uniform command clump replicate template 'aggregate5' position [x5] [y5] [z5] end_command end_loop ; 生成aggregate6(43个) loop i (1,43) local r6 = radius * math.sqrt(math.random.uniform) local theta6 = 2.0 * math.pi * math.random.uniform local x6 = r6 * math.cos(theta6) local y6 = r6 * math.sin(theta6) local z6 = 0.005 + 0.055 * math.random.uniform command clump replicate template 'aggregate6' position [x6] [y6] [z6] end_command end_loop ; 生成aggregate7(14个) loop i (1,14) local r7 = radius * math.sqrt(math.random.uniform) local theta7 = 2.0 * math.pi * math.random.uniform local x7 = r7 * math.cos(theta7) local y7 = r7 * math.sin(theta7) local z7 = 0.005 + 0.055 * math.random.uniform command clump replicate template 'aggregate7' position [x7] [y7] [z7] end_command end_loop end [generate_aggregates] ; ========== 5. 设置材料属性 ========== clump attribute density 2500 damp 0.7 ; ========== 6. 设置接触模型 ========== contact cmat default model linear ... method deformability emod 1e9 kratio 1.5 ... property fric 0.5 ; 启用自动时间步长(修正后的语法) model mechanical timestep auto ; ========== 7. 施加重力并消除初始重叠 ========== model gravity 0 0 -9.81 model cycle 1000 calm 10 ; 使用较宽松的收敛标准进行初始平衡 model solve ratio 1e-3 io.out('=== 初始平衡完成 ===') ; ========== 8. 模拟压实过程 ========== ; 设置顶部墙体向下移动的速度 (单位:m/s) wall attribute velocity-z -0.001 range id [wall.id('top')] ; 使用时间限制代替复杂的FISH函数控制(简化版) fish define stop_compaction local top_z = wall.pos.z(wall.find('top')) io.out('当前顶部墙体高度:' + string(top_z*1000) + ' mm') if top_z <= 0.0635 ; 63.5 mm wall.vel.z(wall.find('top')) = 0 io.out('压实完成!当前高度:' + string(top_z*1000) + ' mm') exit section endif end ; 开始压实过程(先使用时间限制进行测试) model solve time 30 io.out('=== 压实过程完成 ===') ; ========== 9. 投放砂浆小球 ========== ; 在圆柱形模具内生成小球(先减少数量进行测试) ball generate radius 0.0005 box -0.0505 0.0505 -0.0505 0.0505 0 0.065 number 1000 ball attribute density 2300 ; 删除骨料重叠的小球 ball delete range clump count not 0 io.out('=== 砂浆小球投放完成 ===') ; ========== 10. 设置小球之间的接触属性 ========== ; 先选择所有小球,然后设置它们的接触属性 ball group 'mortar' range radius 0.0004 0.0006 ; 使用更简单的方法设置接触属性 contact method deformability emod 1e8 kratio 1.5 contact property fric 0.3 io.out('=== 接触模型重新应用完成 ===') ; ========== 11. 最终平衡 ========== model cycle 1000 ; 使用更严格的收敛标准进行最终平衡 model solve ratio 1e-5 io.out('=== 最终平衡完成 ===') ; ========== 12. 保存模型 ========== model save 'compacted_mixture.p3dat' io.out('马歇尔试件模型生成完成!')这是我的pfc3d6.0的代码,你先阅读一下,了解一下,接下来我会问你几个问题
最新发布
10-14
### 使用 `Math.cos` 和 `Math.sin` 方法 在 JavaScript 中,`Math.cos()` 函数返回给定角度的余弦值,而 `Math.sin()` 函数则返回正弦值。这两个函数都接受以弧度表示的角度作为参数[^2]。 #### 参数说明 - **角 度 (radians)**: 这两个方法都需要传入一个数值型参数,该参数是以弧度为单位的角度。如果要转换成弧度,可以使用公式:`radian = degree * Math.PI / 180`[^3]。 #### 返回值 - **cos(x)** : 返回 `-1` 到 `1` 之间的浮点数,代表指定角度的余弦值。 - **sin(x)** : 同样返回 `-1` 到 `1` 的浮点数,对应于输入角度的正弦值。 #### 示例代码 下面是一些简单的例子展示了如何利用 `Math.cos()` 和 `Math.sin()` 来计算不同角度下的三角函数值: ```javascript // 将度数转为弧度 function degToRad(degrees) { return degrees * (Math.PI / 180); } let angleInDegrees = 45; // 定义角度为45度 console.log(`Cosine of ${angleInDegrees}° is`, Math.cos(degToRad(angleInDegrees))); console.log(`Sine of ${angleInDegrees}° is`, Math.sin(degToRad(angleInDegrees))); // 输出 Cosine and Sine values for common angles like 0, 90, 180 etc. const specialAngles = [0, 30, 45, 60, 90]; specialAngles.forEach((degree) => { console.log( `${degree}° -> cos=${Math.cos(degToRad(degree)).toFixed(2)}, sin=${Math.sin(degToRad(degree)).toFixed(2)}` ); }); ``` 这段程序会先定义一个辅助函数用于将角度从度数转换到弧度,接着分别调用了 `Math.cos()` 和 `Math.sin()`. 最后遍历几个特殊角度并打印其对应的余弦和正弦值[^4].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值