Mason 入门例子3 --- 让学生动起来
2016-03-26 09:49
183 查看
现在我们给学生增加 forceToSchoolMultiplier 和 randomMultiplier 两种力。
学生会有往操场中心行走的趋势和随机漫游的趋势。
学生类实现steppable接口,每个时间步长点会按照随机顺序调用所有Student的step方法。
我们从State中抽取yard,然后找到当前Student对象的位置。
我们使用MutableDouble来计算综合的位移,最后设置Student的新位置。
学生会有往操场中心行走的趋势和随机漫游的趋势。
package com.mason.learn; import sim.engine.*; import sim.util.*; import sim.field.continuous.*; public class Students extends SimState { private static final long serialVersionUID = 1L; public Continuous2D yard = new Continuous2D(1.0, 100, 100); public int numStudents = 50; double forceToSchoolMultiplier = 0.01; double randomMultiplier = 0.1; public Students(long seed) { super(seed); } public void start() { super.start(); // clear the yard yard.clear(); // add some students to the yard for (int i = 0; i < numStudents; i++) { Student student = new Student(); yard.setObjectLocation(student, new Double2D(yard.getWidth() * 0.5 + random.nextDouble() - 0.5, yard.getHeight() * 0.5 + random.nextDouble() - 0.5)); schedule.scheduleRepeating(student); } } public static void main(String[] args) { doLoop(Students.class, args); System.exit(0); } }
学生类实现steppable接口,每个时间步长点会按照随机顺序调用所有Student的step方法。
package com.mason.learn; import sim.engine.*; import sim.field.continuous.Continuous2D; import sim.util.*; public class Student implements Steppable { private static final long serialVersionUID = 1L; @Override public void step(SimState state) { Students students = (Students) state; Continuous2D yard = students.yard; Double2D me = students.yard.getObjectLocation(this); MutableDouble2D sumForces = new MutableDouble2D(); // add in a vector to the "teacher" -- the center of the yard, so we // don’t go too far away sumForces.addIn(new Double2D((yard.width * 0.5 - me.x) * students.forceToSchoolMultiplier, (yard.height * 0.5 - me.y) * students.forceToSchoolMultiplier)); // add a bit of randomness sumForces.addIn(new Double2D(students.randomMultiplier * (students.random.nextDouble() * 1.0 - 0.5), students.randomMultiplier * (students.random.nextDouble() * 1.0 - 0.5))); sumForces.addIn(me); students.yard.setObjectLocation(this, new Double2D(sumForces)); } }
我们从State中抽取yard,然后找到当前Student对象的位置。
我们使用MutableDouble来计算综合的位移,最后设置Student的新位置。
相关文章推荐
- DNW烧写FL2440 NAND Flash分区
- HTML中属性ID和属性NAME的区别(转)
- IOS开发之——保存图片到相册的功能实现
- bootstrap中container类和container-fluid类的区别
- 笔试练习一
- poj 1006 剩余定理
- Java起手的过程比较漫长
- qt中使用opencv处理图片 QImage 和 IplImage 相互之间转换问题
- yaha分词
- spring4.2中引入的Excel处理类
- 「能写代码」是愚公移山,「会写代码」是女娲补天
- Windows环境下的golang的环境安装和设置
- 深入分析RFID与NFC两种技术的关系
- 一种对网页进行适配处理的方法及网页适配装置
- coursera特征缩放
- .net是绑定于一种系统的多语言平台
- 可定制的分词库——Yaha(哑哈)分词
- U盘问题,插入时只有声音,没有显示,在”计算机管理”的“磁盘管理”中,上面看不到盘符”H:”,下面可以看到”磁盘1 可移动无媒
- Java虚拟机学习之多态性实现机制——静态分派与动态分派
- AlphaGo 的首尔之战带来的启示