处理:为什么这个Walker对象不画?
Processing: why does this Walker object not draw?
我正在完成代码本质中的一个练习,该练习涉及将现有程序转换为包含向量。我正在使用的原始代码是 Walker 对象的代码,它在 "random walk" 期间向右倾斜:
根据我目前所写的内容,当我尝试 运行 代码时,Sketch 选项卡打开但没有绘制任何东西。
程序为 Walker 对象声明 x 和 y 组件,添加它们,渲染。然后是以下内容:
void step()
{
int rx = int(random(1));
int ry = int(random(1));
if (rx < 0.4)
{
x++;
} else
if (rx < 0.5)
{
x--;
}
else
if (ry < 0.9)
{
y++;
}
else
{
y--;
}
}
}
Walker w;
Walker location;
Walker velocity;
void setup()
{
size(200, 200);
background(255);
Walker w = new Walker(5, 5);
Walker location = new Walker(100, 100);
Walker velocity = new Walker(2.5, 3);
}
void draw()
{
location.add(velocity);
w.step();
w.render();
}
我假设您正在 Chapter 1 of the Nature of Code book:
进行此练习
Exercise 1.2
Take one of the walker examples from the introduction and convert it to use PVectors.
所以您想从 WalkerTendsToDownRight example 开始并使用向量,对吗?保存步行者位置的 x 和 y 整数字段可以用向量替换。我认为主要的草图代码可以保持不变。 walker 代码可能如下所示:
class Walker {
PVector location;
Walker() {
location = new PVector(width / 2, height / 2);
}
void render() {
stroke(0);
strokeWeight(2);
point(location.x, location.y);
}
// Randomly move right (40% chance), left (10% chance),
// down (40% chance), or up (10% chance).
void step() {
float randomDirection = random(1);
if (randomDirection < 0.4) {
location.x++;
} else if (randomDirection < 0.5) {
location.x--;
} else if (randomDirection < 0.9) {
location.y++;
} else {
location.y--;
}
location.x = constrain(location.x, 0, width - 1);
location.y = constrain(location.y, 0, height - 1);
}
}
在 GitHub 上的 Walker with vector example 中,他们还使用了一个向量来存储助行器将要进行的移动。使用这种方法,阶跃函数可能会短很多(同时保持向右 and/or 向下移动的偏好):
void step() {
PVector move = new PVector(random(-1, 4), random(-1, 4));
location.add(move);
location.x = constrain(location.x, 0, width - 1);
location.y = constrain(location.y, 0, height - 1);
}
如果您希望学步车继续小步走,您甚至可以添加对限制功能的调用:
move.limit(1);
我正在完成代码本质中的一个练习,该练习涉及将现有程序转换为包含向量。我正在使用的原始代码是 Walker 对象的代码,它在 "random walk" 期间向右倾斜:
根据我目前所写的内容,当我尝试 运行 代码时,Sketch 选项卡打开但没有绘制任何东西。
程序为 Walker 对象声明 x 和 y 组件,添加它们,渲染。然后是以下内容:
void step()
{
int rx = int(random(1));
int ry = int(random(1));
if (rx < 0.4)
{
x++;
} else
if (rx < 0.5)
{
x--;
}
else
if (ry < 0.9)
{
y++;
}
else
{
y--;
}
}
}
Walker w;
Walker location;
Walker velocity;
void setup()
{
size(200, 200);
background(255);
Walker w = new Walker(5, 5);
Walker location = new Walker(100, 100);
Walker velocity = new Walker(2.5, 3);
}
void draw()
{
location.add(velocity);
w.step();
w.render();
}
我假设您正在 Chapter 1 of the Nature of Code book:
进行此练习Exercise 1.2
Take one of the walker examples from the introduction and convert it to use PVectors.
所以您想从 WalkerTendsToDownRight example 开始并使用向量,对吗?保存步行者位置的 x 和 y 整数字段可以用向量替换。我认为主要的草图代码可以保持不变。 walker 代码可能如下所示:
class Walker {
PVector location;
Walker() {
location = new PVector(width / 2, height / 2);
}
void render() {
stroke(0);
strokeWeight(2);
point(location.x, location.y);
}
// Randomly move right (40% chance), left (10% chance),
// down (40% chance), or up (10% chance).
void step() {
float randomDirection = random(1);
if (randomDirection < 0.4) {
location.x++;
} else if (randomDirection < 0.5) {
location.x--;
} else if (randomDirection < 0.9) {
location.y++;
} else {
location.y--;
}
location.x = constrain(location.x, 0, width - 1);
location.y = constrain(location.y, 0, height - 1);
}
}
在 GitHub 上的 Walker with vector example 中,他们还使用了一个向量来存储助行器将要进行的移动。使用这种方法,阶跃函数可能会短很多(同时保持向右 and/or 向下移动的偏好):
void step() {
PVector move = new PVector(random(-1, 4), random(-1, 4));
location.add(move);
location.x = constrain(location.x, 0, width - 1);
location.y = constrain(location.y, 0, height - 1);
}
如果您希望学步车继续小步走,您甚至可以添加对限制功能的调用:
move.limit(1);