class Walker{
constructor(element,x,y,r,theta,fill){
this.x = x
this.y = y
this.r = r
this.theta = theta
this.fill = fill
this.active = true
this.circle = element.append("circle")
.attr("r",r)
.attr("cx",x)
.attr("cy",y)
.attr("fill",fill)
.attr("stroke","black")
}
step(stepSize){
const r = d3.randomUniform(0,1)()
const dx = r*stepSize*Math.cos(this.theta)
const dy = r*stepSize*Math.sin(this.theta)
this.x = this.trim(this.x + dx)
this.y = this.trim(this.y + dy)
return this.circle.transition().duration(20*r*stepSize).ease(d3.easeLinear).attr("cx",this.x).attr("cy",this.y)
}
async walk(stepSize){
while(this.active){
await this.step(stepSize).end()
if(this.x == 0 || this.x == 500 || this.y == 0 || this.y == 500){
this.circle.attr("fill","red")
this.fill = "red"
}
const dtheta = d3.randomUniform(-Math.PI/6,Math.PI/6)()
this.theta = this.theta + dtheta
}
}
trim(x) {
if (x <= 0) {
return 0
} else if (x >= 500) {
return 500;
} else {
return x
}
}
}