// Divide by 0 error possible on this
if(x 》 originX) {
return 0;
645
…………………………………………………………Page 647……………………………………………………………
}
else
return (float) Math。PI;
}
}
public float distanceFromPoint(int x1; int y1){
return (float) Math。sqrt(
Math。pow(x1 x; 2) +
Math。pow(y1 y; 2));
}
public Point position() {
return new Point(x; y);
}
// Beasts know how to draw themselves:
public void draw(Graphics g) {
g。setColor(color);
int directionInDegrees = (int)(
(currentDirection * 360) / (2 * Math。PI));
int startAngle = directionInDegrees
FieldOBeasts。halfFieldOfView;
int endAngle = 90;
g。fillArc(x; y; GSIZE; GSIZE;
startAngle; endAngle);
}
}
public class FieldOBeasts extends Applet
implements Runnable {
private Vector beasts;
static float
fieldOfView =
(float) (Math。PI / 4); // In radians
// Deceleration % per second:
decayRate = 1。0f;
minimumDistance = 10f; // In pixels
static int
halfFieldOfView = (int)(
(fieldOfView * 360) / (2 * Math。PI));
xExtent = 0;
yExtent = 0;
numBeasts = 50;
maxSpeed = 20; // Pixels/second
boolean uniqueColors = true;
Thread thisThread;
int delay = 25;
public void init() {
if (xExtent == 0 && yExtent == 0) {
xExtent = Integer。parseInt(
getParameter(〃xExtent〃));
yExtent = Integer。parseInt(
getParameter(〃yExtent〃));
}
646
…………………………………………………………Page 648……………………………………………………………
beasts =
makeBeastVector(numBeasts; uniqueColors);
// Now start the beasts a…rovin":
thisThread = new Thread(this);
thisThread。start();
}
public void run() {
while(true) {
for(int i = 0; i 《 beasts。size(); i++){
Beast b = (Beast) beasts。elementAt(i);
b。step();
}
try {
thisThread。sleep(delay);
} catch(InterruptedException ex){}
repaint(); // Otherwise it won"t update
}
}
Vector makeBeastVector(
int quantity; boolean uniqueColors) {
Vector newBeasts = new Vector();
Random generator = new Random();
// Used only if uniqueColors is on:
double cubeRootOfBeastNumber =
Math。pow((double)numBeasts; 1。0 / 3。0);
float colorCubeStepSize =
(float) (1。0 / cubeRootOfBeastNumber);
float r = 0。0f;
float g = 0。0f;
float b = 0。0f;
for(int i = 0; i 《 quantity; i++) {
int x =
(int) (generator。nextFloat() * xExtent);
if(x 》 xExtent Beast。GSIZE)
x …= Beast。GSIZE;
int y =
(int) (generator。nextFloat() * yExtent);
if(y 》 yExtent Beast。GSIZE)
y …= Beast。GSIZE;
float direction = (float)(
generator。nextFloat() * 2 * Math。PI);
int speed = (int)(
generator。nextFloat() * (float)maxSpeed);
if(uniqueColors) {
r += colorCubeStepSize;
if(r 》 1。0) {
r …= 1。0f;
g += colorCubeStepSize;
if( g 》 1。0) {
g …= 1。0f;
b += colorCubeStepSize;
if(b 》 1。0)
647
…………………………………………………………Page 649……………………………………………………………
b …= 1。0f;
}
}
}
newBeasts。addElement(
new Beast(this; x; y; direction; speed;
new Color(r;g;b)));
}
return newBeasts;
}
public Vector beastListInSector(Beast viewer) {
Vector output = new Vector();
Enumeration e = beasts。elements();
Beast aBeast = (Beast)beasts。elementAt(0);
int counter = 0;
while(e。hasMoreElements()) {
aBeast = (Beast) e。nextElement();
if(aBeast != viewer) {
Point p = aBeast。position();
Point v = viewer。position();
float bearing =
aBeast。bearingFromPointAlongAxis(
v。x; v。y; viewer。currentDirection);
if(Math。abs(bearing) 《 fieldOfView / 2)
output。addElement(aBeast);
}
}
return output;
}
public void paint(Graphics g) {
Enumeration e = beasts。elements();
while(e。hasMoreElements()) {
((Beast)e。nextElement())。draw(g);
}
}
public static void main(String'' args) {
FieldOBeasts field = new FieldOBeasts();
field。xExtent = 640;
field。yExtent = 480;
Frame frame = new Frame(〃Field "O Beasts〃);
// Optionally use a mand…line argument
// for the sleep time:
if(args。length 》= 1)
field。delay = Integer。parseInt(args'0');
frame。addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System。exit(0);
}
});
frame。add(field; BorderLayout。CENTER);
frame。setSize(640;480);
648
…………………………………………………………Page 650……………………………………………………………
field。init();
field。start();
frame。setVisible(true);
}
} ///:~
尽管这并非对Craig Reynold 的“Boids”例子中的行为完美重现,但它却展现出了自己独有的迷人之外。通
过对数字进行调整,即可进行全面的修改。至于与这种群聚行为有关的更多的情况,大家可以访问Craig
Reynold 的主页—?
小说推荐
- 软件工程实践者的思想(PDF格式)
- -Page 1-大 道 至 简—软件工程实践者的思想周爱民(Aimingoo 著-Page 2-序2004 年 11 月初爱民(Aimingoo)第一次把他的书稿给我,我翻看了一下,第一反应讲的是感想。这不错,在技
- 最新章:第26章
- 深入浅出MFC第2版(PDF格式)
- -Page 1-Page 2-山高月小山高月小 水落石出水落石出山高月小山高月小 水落石出水落石出-Page 3-深入淺出MFC(第版 使用Visual C 5.0 MFC 4.2)Dissecting MFC(Second Edition Using Visual C 5.0 MFC 4.2)侯俊
- 最新章:第309章
- VB2008从入门到精通(PDF格式英文版)
- -Page 1(R)The eXperT’s Voice inBeginningVB 2008From Novice to ProfessionalChristian Gross-Page 2-Page 3-Beginning VB 2008From Novice to Professional■C
- 最新章:第214章
- C语言游戏编程从入门到精通(PDF格式)
- -Page 1-Page 2-Page 3-Page 4-Page 5-Page 6-Page 7-Page 8-Page 9-Page 10-Page 11-Page 12-Page 13-Page 14
- 最新章:第4章
- JMS简明教程(PDF格式)
- -Page 1-JMS1.1规范中文版卫建军2007‐11‐22-Page 2
- 最新章:第28章
- C语言实例教程(PDF格式)
- -Page 1-前 言Visual C+是开发运行于Windows 95和Windows NT环境下的Win32应用程序的可视化编程工具中最重要的成员之一,它为软件开发人员提供了完整的编辑、编译和调试工具和建立于Win32 API(ApplicationProgramming Interface)基
- 最新章:第143章
- 神祗之眼 (正式版)第7卷(全文完)作者:百里芜虚
- 第五十一章离12月25日的圣诞节还有两天,纽约已经换上了节日的盛装,几天前的一场大雪令整个城市一片银白。人们笑逐颜开,都纷纷开始为节日做准备,商家为了在节日打开销路纷纷推出各种优惠促销手段,纽约的大街小巷全都这样热闹。依沙那被老婆打发出来买过节要用的杂货,而女儿则和凯妮一起到百货商场去买衣服去了。对
- 最新章:第23章
- SQL语言艺术(PDF格式)
- -Page 1-SQLSSQQLL语言艺术内容介绍本书分为12章,每一章包含许多原则或准则,并通过举例的方式对原则进行解释说明。这些例子大多来自于实际案例,对九种SQL经典查询场景以及其性能影响讨论,非常便于实践,为你数据库应用维护人员阅读。资深 SQL 专家 Stéphane Faroult倾力打
- 最新章:第27章
- php程序设计简明教程(DOC格式)
- -Page 1-PHP 程序设计简明教程PHP 讲义 第 1 页 共 90 页-Page 2-目录序 4第一章 PHP 简介 6
- 最新章:第31章