- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在修改一个项目的植绒模拟。添加对象没问题,但是当我尝试删除对象时,我在第 240 行收到“函数 size() 不存在”错误。问题可能出在 AdjSize() 和 subBoid() 中,但我不知道是什么原因造成的。我已经稍微简化了代码,所以它只是 ArrayList 植绒的东西。
Flock flock;
float k, l;
int previous = 0;
int test = 0;
void setup()
{
size(1920, 1080);
flock = new Flock();
for (int i = 0; i < 150; i++)
{
flock.addBoid(new Boid(width/2,height/2));
}
}
void draw()
{
background(0);
flock.run();
flock.AdjSize();
}
// The Boid class
class Boid
{
PVector location;
PVector velocity;
PVector acceleration;
float r;
float maxforce; // Maximum steering force
float maxspeed; // Maximum speed
Boid(float x, float y)
{
acceleration = new PVector(0, 0);
float angle = random(TWO_PI);
velocity = new PVector(cos(angle), sin(angle));
location = new PVector(x, y);
r = 2.0;
maxspeed = 2;
maxforce = 0.03;
}
void run(ArrayList<Boid> boids)
{
flock(boids);
update();
borders();
render();
}
void applyForce(PVector force)
{
acceleration.add(force);
}
void flock(ArrayList<Boid> boids)
{
PVector sep = separate(boids); // Separation
PVector ali = align(boids); // Alignment
PVector coh = cohesion(boids); // Cohesion
sep.mult(1.5);
ali.mult(1.0);
coh.mult(1.0);
applyForce(sep);
applyForce(ali);
applyForce(coh);
}
// Method to update location
void update()
{
// Update velocity
velocity.add(acceleration);
// Limit speed
velocity.limit(maxspeed);
location.add(velocity);
// Reset accelertion to 0 each cycle
acceleration.mult(0);
}
PVector seek(PVector target)
{
PVector desired = PVector.sub(target, location);
desired.normalize();
desired.mult(maxspeed);
PVector steer = PVector.sub(desired, velocity);
steer.limit(maxforce); // Limit to maximum steering force
return steer;
}
void render()
{
float theta = velocity.heading2D() + radians(90);
fill(255, 255,255);
stroke(255);
pushMatrix();
translate(location.x, location.y);
rotate(theta);
beginShape();
vertex(r*15, r*17); //Left wing tip
vertex(r*25, r*10); //Left wing top point
vertex(r*30, r*13); //middle divit
vertex(r*35, r*10); //right wing top point
vertex(r*45, r*17); //right wing tip
vertex(r*30, r*13); //underswoop
endShape(CLOSE);
popMatrix();
}
void borders()
{
if (location.x < -r) location.x = width+r;
if (location.y < -r) location.y = height+r;
if (location.x > width+r) location.x = -r;
if (location.y > height+r) location.y = -r;
}
PVector separate (ArrayList<Boid> boids) {
float desiredseparation = 25.0f;
PVector steer = new PVector(0, 0, 0);
int count = 0;
for (Boid other : boids)
{
float d = PVector.dist(location, other.location);
if ((d > 0) && (d < desiredseparation))
{
PVector diff = PVector.sub(location, other.location);
diff.normalize();
diff.div(d); // Weight by distance
steer.add(diff);
count++; // Keep track of how many
}
}
if (count > 0)
{
steer.div((float)count);
}
if (steer.mag() > 0)
{
steer.normalize();
steer.mult(maxspeed);
steer.sub(velocity);
steer.limit(maxforce);
}
return steer;
}
// Alignment
PVector align (ArrayList<Boid> boids)
{
float neighbordist = 50;
PVector sum = new PVector(0, 0);
int count = 0;
for (Boid other : boids)
{
float d = PVector.dist(location, other.location);
if ((d > 0) && (d < neighbordist))
{
sum.add(other.velocity);
count++;
}
}
if (count > 0)
{
sum.div((float)count);
sum.normalize();
sum.mult(maxspeed);
PVector steer = PVector.sub(sum, velocity);
steer.limit(maxforce);
return steer;
}
else
{
return new PVector(0, 0);
}
}
PVector cohesion (ArrayList<Boid> boids) {
float neighbordist = 50;
PVector sum = new PVector(0, 0);
int count = 0;
for (Boid other : boids)
{
float d = PVector.dist(location, other.location);
if ((d > 0) && (d < neighbordist))
{
sum.add(other.location); // Add location
count++;
}
}
if (count > 0)
{
sum.div(count);
return seek(sum); // Steer towards the location
}
else
{
return new PVector(0, 0);
}
}
}
class Flock
{
ArrayList<Boid> boids; // An ArrayList for all the boids
Flock()
{
boids = new ArrayList<Boid>(); // Initialize the ArrayList
}
void run()
{
for (Boid b : boids)
{
b.run(boids);
}
}
void AdjSize()
{
if(test == 0)
{
flock.addBoid(new Boid(width/2,height/2));
test = 1;
}
else
{
flock.subBoid(flock.size() - 1);
}
}
void addBoid(Boid b)
{
boids.add(b);
}
void subBoid(Boid b)
{
boids.remove(b);
}
}
最佳答案
您是在 Flock 对象上调用 size()
,而不是在 ArrayList 上。如果你想让 Flock 有一个可调用的 size()
方法,你必须先给它一个。例如,
class Flock {
// ....
public int size() {
return boids.size();
}
}
关于java - 在 ArrayList 中获取 "size() does not exist"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21177382/
ValueError Traceback (most recent call last) in 23 out
在 CSS 中,我从来没有真正理解为什么会发生这种情况,但每当我为某物分配 margin-top:50% 时,该元素就会被推到页面底部,几乎完全消失这一页。我假设 50% 时,该元素将位于页面的中间位
我正在尝试在 pyTorch 中训练我的第一个神经网络(我不是程序员,只是一个困惑的化学家)。 网络本身应该采用 1064 个元素向量并用 float 对它们进行评级。 到目前为止,我遇到了各种各样的
我有一个简单的问题。如何在 3 个维度上移动线性阵列?这似乎太有效了,但在 X 和 Y 轴上我遇到了索引问题。我想这样做的原因很简单。我想创建一个带有 block 缓冲区的体积地形,所以我只需要在视口
我正在尝试运行我购买的一本关于 Pytorch 强化学习的书中的代码。 代码应该按照本书工作,但对我来说,模型没有收敛,奖励仍然为负。它还会收到以下用户警告: /home/user/.local/li
我目前正在使用 this repo使用我自己的数据集执行 NLP 并了解有关 CNN 的更多信息,但我一直遇到有关形状不匹配的错误: ValueError: Target size (torch.Si
UIScrollView 以编程方式设置,请不要使用 .xib 文件发布答案。 我的 UIScrollView 位于我的模型类中,所以我希望代码能够轻松导入到另一个项目中,例如。适用于 iPad 或旋
我在我的 Ruby on Rails 应用程序(版本 4.3.1)中使用 Bootstrap gem。我最近发现了响应式字体大小功能 (rfs)。根据 Bootstrap 文档,它刚刚在 4.3 版中
这个问题不太可能帮助任何 future 的访客;它仅与一个小地理区域、一个特定时刻或一个非常狭窄的情况相关,而这些情况通常不适用于互联网的全局受众。如需帮助使这个问题更广泛地适用,visit the
size 之间的语义区别是什么?和 sizeIs ?例如, List(1,2,3).sizeIs > 1 // true List(1,2,3).size > 1 // true Luis 在 c
我想从 div 中删除一些元素属性。我的 div 是自动生成的。我想遍历每个 div 和子 div,并想删除所有 font-size (font-size: Xpx)和 size里面font tag
super ,对 Python 和一般编程 super 新手。我有一个问题应该很简单。我正在使用一本使用 Python 3.1 版的 python 初学者编程书。 我目前正在写书中的一个程序,我正在学
我无法从 NativeBase 更改缩略图的默认大小。我可以显示默认圆圈,即小圆圈和大圆圈,但我想显示比默认大小更大的圆圈。这是我的缩略图代码: Prop 大小不起作用,缩略图仍然很小。 我的 Na
我是pytorch的新手。在玩张量时,我观察到了两种类型的张量- tensor(58) tensor([57.3895]) 我打印了它们的形状,输出分别是 - torch.Size([]) torch
这是我的 docker images 命令的输出: $ docker images REPOSITORY TAG IMAGE ID CREATED
来自 PriorityQueue 的代码: private E removeAt(int i) { assert i >= 0 && i < size; modCount++;
首先,在我的系统上保留以下内容:sizeof(char) == 1 和 sizeof(char*) == 4。很简单,当我们计算下面类的总大小时: class SampleClass { char c
我正在编写一个游戏来查找 2 个图像之间的差异。我创建了 CCSprite 的子类 Spot。首先我尝试创建小图像并根据其位置添加自身,但后来我发现位置很难确定,因为很难避免 1 或 2 个像素的偏移
我有一个 Tumblr Site每个帖子的宽度由标签决定。 如果一篇文章被标记为 #width200,CSS 类 .width200 被分配。 问题是,虽然帖子的宽度不同,但它们都使用主题运算符加载相
这个问题在这里已经有了答案: What is the ideal growth rate for a dynamically allocated array? (12 个答案) 关闭 8 年前。 我
我是一名优秀的程序员,十分优秀!