Benutzer-Werkzeuge

Webseiten-Werkzeuge


Seitenleiste

skript:bewegungen

Dies ist eine alte Version des Dokuments!




Simulation von Bewegungen

Programm, welches den Roboter vorwärts bewegen und drehen lässt. Realisierung mit globalen Variablen ohne Klassen:

// current x coordinate of the robot center:
float myPosX = 100;
// current y coordinate of the robot center:
float myPosY = 100;
// robot radius:
float myRadius = 25;
// current angle between the robot's pointer and the x axis, in radian:
float myAngle = 0;
 
void setup() {
  size(600, 400);
  drawRobot(25, 100, 100, 0);
}
 
void draw() {
  background(255);
  turn(0.1);
  run(10);
  drawRobot(myRadius, myPosX, myPosY, myAngle);
}
 
/**
* Moves the robot forward
* float distance: distance the robot should travel
*/
void run(float distance) {
  myPosX = myPosX + distance*cos(myAngle);
  myPosY = myPosY + distance*sin(myAngle);
}
 
/**
* Changes the robot's direction 
* float angleChange: angle by which the robot's direction should be changed in radians
*/
void turn(float angleChange) {
  myAngle = myAngle + angleChange;
}
 
/**
 * Draws a robot on the canvas. 
 * float radius: radius of the robot
 * float posX: x coordinate of the robot's center
 * float posY: y coordinate of the robot's center
 * float direction: angle between the robot's pointer and canvas x axis in radians
 */
void drawRobot(float radius, float posX, float posY, float direction) {
 
  // draw the circle:
  ellipse(posX, posY, 2*radius, 2*radius);
 
  // calculate the position of the pointer line (the line is +20% longer than the radius):
  float endOfLineX = posX + radius * 1.2 * cos(direction);
  float endOfLineY = posY + radius * 1.2 * sin(direction);
 
  // draw the pointer line:
  line(posX, posY, endOfLineX, endOfLineY);
}
skript/bewegungen.1494494345.txt.gz · Zuletzt geändert: 2017/05/11 11:19 von d.golovko