Skip to content

Instantly share code, notes, and snippets.

@ivanseidel
Created April 17, 2015 20:22
Show Gist options
  • Save ivanseidel/1694d82f809455d09da6 to your computer and use it in GitHub Desktop.
Save ivanseidel/1694d82f809455d09da6 to your computer and use it in GitHub Desktop.
Simple Robot Simulation with Interface
interface QuandoPrecisaExecutar{
public void executar(Robo eeew);
}
class Robo{
Vetor position;
Vetor direction;
Robo(double x, double y){
position = new Vetor(x, y);
direction = fromTheta(0);
}
public void mover(double passos){
position = position.soma(direction.multiplicar(passos));
}
public void girar(double angulo){
direction = direction.rotate(Math.toRadians(angulo));
}
public void draw(){
fill(0, 255, 0);
ellipse((float)position.x, (float)position.y, 20, 20);
double pX = position.x;
double pY = position.y;
double dX = direction.x;
double dY = direction.y;
line( (float)pX, (float)pY,
(float)(pX + dX*20), (float)(pY + dY*20));
}
public boolean seVaiSair(double distancia){
Vetor pFinal = position.soma(direction.multiplicar(distancia));
if( pFinal.x < 0 || pFinal.y < 0 ||
pFinal.x > width || pFinal.y > height)
return true;
return false;
}
public QuandoPrecisaExecutar quandoExecutar;
public void executar(){
if(quandoExecutar != null)
quandoExecutar.executar(this);
}
}
Robo robos[] = new Robo[20];
void setup(){
size(700, 300);
QuandoPrecisaExecutar andadorFrente = new QuandoPrecisaExecutar(){
public void executar(Robo r){
if(r.seVaiSair(1 + 10)){
r.girar(90);
}else{
r.mover(10);
}
}
};
QuandoPrecisaExecutar giradorAntihorario = new QuandoPrecisaExecutar(){
public void executar(Robo r){
r.mover(1);
r.girar(-10);
}
};
for(int i = 0; i < 10; i++){
robos[i] = new Robo(i*30, 150);
robos[i].quandoExecutar = giradorAntihorario;
}
for(int i = 10; i < 20; i++){
robos[i] = new Robo(i*30, 150);
robos[i].quandoExecutar = andadorFrente;
}
}
void draw(){
background(255);
for(int i = 0; i < 20; i++){
robos[i].executar();
}
for(int i = 0; i < 20; i++){
robos[i].draw();
}
}
public class Vetor {
public double x = 0;
public double y = 0;
Vetor(double x, double y){
this.x = x;
this.y = y;
}
Vetor(){}
public Vetor soma(double x2, double y2){
return new Vetor(x + x2, y + y2);
}
public Vetor soma(Vetor v){
return new Vetor(x + v.x, y + v.y);
}
public Vetor multiplicar(double e){
return new Vetor(x * e, y * e);
}
public Vetor dividir(double e){
if(e == 0)
return new Vetor();
return new Vetor(x / e, y / e);
}
// Creates a new vector from a Theta value (Radians is default)
public Vetor rotate(double theta){
double currentTheta = Math.atan2(this.y, this.x);
double currentNorm = Math.sqrt(x*x + y*y);
// Creates vector with new Angle
Vetor finalVector = fromTheta(currentTheta + theta);
// Un-normalize vector
return finalVector.multiplicar(currentNorm);
}
public void imprimir(){
System.out.printf("[%.2f,%.2f]",x,y);
}
}
Vetor fromTheta(double theta){
double x = Math.cos(theta);
double y = Math.sin(theta);
return new Vetor(x, y);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment