Question: Processing 3.3.6 - Java (Processing) ---- or Arduino I need to make a function that will draw a circle that moves back and forth along

Processing 3.3.6 - Java (Processing) ---- or Arduino

I need to make a function that will draw a circle that moves back and forth along a horizontal line. It will go left once it touches the right most end of the line (X_RIGHT), and go right when it touches the left most end of the line (X_LEFT).

Basically, a loop of a ball going back and forth across a line. I just need to know how to make it work with if and else statements within the moveBall function.

Here is my code so far, with my incomplete code within the moveBall function.

----------------------------------------------------------------------------------------------------------------------------

//****1) The state variables needed go here: final int X_LEFT = 100; //The left end of the horizontal line. final int X_RIGHT = 400; //The right end of the horizontal line. final int Y = 100; //The y position of the line and circle. final int BALL_DIAM = 50; //The diameter of the circle final float BALL_SPEED = 2.5; //Its speed (pixels/second)

float ballX;

//****2) Add your functions here: void drawAll() { background(155); line(X_LEFT,Y,X_RIGHT,Y); ellipse(ballX,Y,BALL_DIAM,BALL_DIAM); }

void moveBall() { if (ballX == 400) { ballX = max(100,ballX - BALL_SPEED); } else { ballX = min(400,ballX + BALL_SPEED); } if (ballX == 100) { ballX = min(400,ballX + BALL_SPEED); } }

void setup() { size(500,200); ballX = 250; }

void draw(){ drawAll(); moveBall(); }

-----------------------------------------------------------------------------------------------------

Thank you so much to anyone who can help!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!