xxxxxxxxxx
class Point{
constructor(x,y){
this.x=x;this.y=y;
}
interp(other,factor){
return this.mult(1-factor).add(other.mult(factor));
}
mult(f){
return new Point(this.x*f,this.y*f);
}
add(other){
return new Point(this.x+other.x,this.y+other.y);
}
mag(){
return Math.sqrt(this.x*this.x+this.y*this.y);
}
}
class Ship{
constructor(pos){
this.pos=pos;
this.move={
to:pos,
cur:pos,
start:0,
end:1
};
}
draw(time,other){
let pos=this.pos.interp(this.move.to,(this.clip(time,this.move.start,this.move.end)-this.move.start)/(this.move.end-this.move.start));
this.move.cur=pos;
stroke(0,50);
line(pos.x,pos.y,this.pos.x,this.pos.y);
stroke(255,0,0);
line(pos.x,pos.y,this.move.to.x,this.move.to.y);
noFill();stroke(0);
circle(pos.x,pos.y,7);
stroke(0,255,0);
if(pos.add(other.move.cur.mult(-1)).mag()<50)stroke(255,0,0);
circle(pos.x,pos.y,100);
}
moveTo(pos,start,end){
this.move.to=pos;
this.move.start=start;
this.move.end=end;
}
clip(x,min,max){
return Math.min(max,Math.max(min,x));
}
}
let s1,s2,s3,s4;
function setup(){
createCanvas(800,400);
s1=new Ship(new Point(Math.random()*200+100,Math.random()*200+100));
s2=new Ship(new Point(Math.random()*200+100,Math.random()*200+100));
s3=new Ship(new Point(600,200));
s4=new Ship(s2.pos.add(s1.pos.mult(-1)).add(new Point(600,200)));
s1.moveTo(new Point(300,300),120,720);
s2.moveTo(new Point(100,300),120,720);
s4.moveTo(s2.move.to.add(s1.move.to.mult(-1)).add(new Point(600,200)),120,720);
}
function draw(){
background(255);
s1.draw(frameCount,s2);
s2.draw(frameCount,s1);
s3.draw(frameCount,s4);
s4.draw(frameCount,s3);
}