In: Computer Science
Could you function key to move the arms and legs to this code? so a key function for each direction of the robot. left right forward and back.
void setup()
{
size(500, 250);
background(255);
draw();
}
void draw()
{
{
fill(100, 100, 97);
rect(45, 0, 50, 32); // Head
rect(45, 32, 50, 50); // Body
line(90, 20, 50, 20);// Mouth
rect(33, 32, 12, 42); // Left arm
rect(95, 32, 12, 42); // Right arm
rect(33, 72, 12, 14); // Left hand
rect(95, 72, 12, 14); // Right hand
rect(45, 82, 14, 40); // Left leg
rect(81, 82, 14, 50); // Right leg
rect(40,120,20,20); // Left foot
rect(80,120,20,20); // Right foot
fill(245, 17, 33);
ellipse(55, 10, 13, 12); // Left eye
ellipse(85, 10, 13, 12); // Right eye
}
}
Answer : Before writing the function we will see what would be our approach for this method:
Approach : Count number of Forward movements (F), Backward movements (B), left movements (L) and right movements (R) as countForward, countBackward, countLeft and countRight respectively. Final x-coordinate will be (countRight – countLeft) and y-coordinate will be (countForward – countBackward).
Below is the function for movement of Robot .
#include <bits/stdc++.h>
using namespace std;
// function to find final position of
// robot after the complete movement
void finalPosition(string move)
{
int l = move.size();
int countForward = 0, countBackward = 0;
int countLeft = 0, countRight = 0;
// traverse the instruction string 'move'
for (int i = 0; i < l; i++) {
// for each movement increment its
// respective counter
if (move[i] == 'F')
countForward++;
else if (move[i] == 'B')
countBackward++;
else if (move[i] == 'L')
countLeft++;
else if (move[i] == 'R')
countRight++;
}
// required final position of robot
cout << "Final Position: ("
<< (countRight - countLeft)
<< ", " << (countForward - countBackward)
<< ")" << endl;
}
// Driver program to test above
int main()
{
string move = "FBBLLRFFFBFBRFBBFFLLBRRRR";
finalPosition(move);
return 0;
}
Note: Provide your Feedback as thumbs up and vice-versa. For any query ask in comment section. Always happy to help you.