What do the x and y values mean here? Can you show in a figure?

  • JavaScript
  • Thread starter shivajikobardan
  • Start date
  • Tags
    Figure Mean
In summary, the code checks to see if the ball is within the boundaries of the canvas. If it is not, it moves the ball according to its speed and gravity.
  • #1
shivajikobardan
674
54
TL;DR Summary
HTML5 canvas javascript
JavaScript:
function checkBoundaryCollision() {

  if (x - ballRadius <= 0 || x + ballRadius >= canvasWidth) {

    dx = -dx;

  }

  if (y - ballRadius <= 0 || y + ballRadius >= canvasHeight) {

    dy = -dy;

  }

}

A5gv62pzDoiMQslngM79oB_Hgmk3H9r2Sk5yoMTIcnPHPvE1Xg.png


Full code here:
 
Technology news on Phys.org
  • #2
x and y are the current positions of the ball.
x ranges from 0 (far left) to canvasWidth.
y ranges from 0 to canvasHeight.
dx and dy and the x and y increments - so on each cycle you would execute x=x+dx; y=y+dy;

I also recommend the use of parenthesis as follows:
Code:
function checkBoundaryCollision() {
  if ( ((x - ballRadius) <= 0) || ((x + ballRadius) >= canvasWidth)) {
    dx = -dx;
  }
  if ( ((y - ballRadius) <= 0) || ((y + ballRadius) >= canvasHeight)) {
    dy = -dy;
  }
}
 
  • #3
.Scott said:
x and y are the current positions of the ball.
x ranges from 0 (far left) to canvasWidth.
y ranges from 0 to canvasHeight.
dx and dy and the x and y increments - so on each cycle you would execute x=x+dx; y=y+dy;

Those are reasonable guesses, but it may have been better to tell the OP to look elsewhere in the code for the definitions of these variables, since they aren't defined in the extract they have posted.

Give a man a fish, etc.
 
  • #4
.Scott said:
I also recommend the use of parenthesis as follows:
Code:
function checkBoundaryCollision() {
  if ( ((x - ballRadius) <= 0) || ((x + ballRadius) >= canvasWidth)) {
    dx = -dx;
  }
  if ( ((y - ballRadius) <= 0) || ((y + ballRadius) >= canvasHeight)) {
    dy = -dy;
  }
}
Nothing wrong with your recommendation, but IMO the following is just as clear. In my version I've written each of the four comparisons such as (x - ballRadius <= 0) without parentheses around these expressions. Since addition and subtraction are of higher precedence in Javascript (and other languages derived from C) than any of the comparison operators, all four comparisons in the code above will be evaluated after the subtractions or additions are performed. See http://www.devdoc.net/web/developer...t=Table Precedence ,( … ) 46 more rows

One could also remove the parentheses around the comparison expressions, since logical AND and logical OR are at considerably lower comparisons, but I've left the parentheses in. My reasoning is that programmers with at least a little experience are aware of the precedence levels of the arithmetic operators vs. the comparison operators, but might not be as aware of how the logical operators fit into the precedence relationships.
JavaScript:
function checkBoundaryCollision() {
  if ( (x - ballRadius <= 0) || (x + ballRadius >= canvasWidth) ) {
    dx = -dx;
  }
  if ( (y - ballRadius <= 0) || (y + ballRadius >= canvasHeight) ) {
    dy = -dy;
  }
}
 
  • #5
My rule with parentheses is to only put them in where they are needed, otherwise you have to spend time working out what they are doing, only to find that they aren't doing anything. In practice I leave decisions like this up to https://prettier.io/, which takes the same view. In a decent text editor this makes it pretty easy to see what is going on.

1674148854096.png
 
  • #6
pasmith said:
Those are reasonable guesses, but it may have been better to tell the OP to look elsewhere in the code for the definitions of these variables, since they aren't defined in the extract they have posted.

Give a man a fish, etc.
.. and you'll end up wanting to give him a bigger fish.
Code:
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");
const ballRadius = 10;

var ballX = Array();
ballX.speed = 2.4;
ballX.vel   = ballX.speed;
ballX.rad   = ballRadius;
ballX.limit = canvas.width;
ballX.pos   = 20;

var ballY = Array();
ballY.speed = 1.7;
ballY.vel   = ballY.speed;
ballY.rad   = ballRadius;
ballY.limit = canvas.height;
ballY.pos   = 20;

function CheckWalls(ball) {
  if(ball.pos<0) {
    ball.pos = -ball.pos;
    ball.vel = ball.speed;
  }
  if(ball.pos>ball.limit) {
    ball.pos = (2*ball.limit)-ball.pos;
    ball.vel = -ball.speed;
  }
  if(ball.pos<ball.rad) {
    ball.erad = (ball.pos+ball.rad)/2;
    ball.epos = ball.erad;
  } else if((ball.limit-ball.pos)<ball.rad) {
    ball.erad = (ball.limit-ball.pos+ball.rad)/2;
    ball.epos = ball.limit-ball.erad;
  } else {
    ball.erad = ball.rad;
    ball.epos = ball.pos;
  };
  return ball;
}

function draw() {
  // Draw the puck
  ctx.beginPath();
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  CheckWalls(ballX);
  CheckWalls(ballY);
  ctx.ellipse(ballX.epos, ballY.epos, ballX.erad, ballY.erad, 0, 0, Math.PI * 2);
  ctx.fillStyle = "red";
  ctx.fill();
  ctx.closePath();
  ballX.pos = ballX.pos + ballX.vel;
  ballY.pos = ballY.pos + ballY.vel;
}
setInterval(draw, 12);
 
  • #7
.Scott said:
.. and you'll end up wanting to give him a bigger fish.
Yes, particularly as the OP has taken a step backwards with setInterval this time round; his last question used requestAnimationFrame, which is the right way to do animations in JavaScript.
 

1. What do the x and y values represent in this data set?

The x and y values represent the independent and dependent variables, respectively. The independent variable is typically plotted on the horizontal axis and is the variable that is controlled or manipulated by the researcher. The dependent variable is typically plotted on the vertical axis and is the variable that is measured or affected by changes in the independent variable.

2. How are the x and y values related to each other in this figure?

The x and y values are related through a mathematical or functional relationship. This means that changes in the x value will result in corresponding changes in the y value, and vice versa. The type of relationship between the two variables can be determined by examining the shape and trend of the data points on the figure.

3. Can you explain the significance of the x and y values in this graph?

The significance of the x and y values in a graph depends on the specific context and purpose of the research. In general, the x and y values represent the variables that are being studied and can provide insight into the relationship between them. The specific meaning and significance of these values can be further understood by interpreting the results and conclusions of the study.

4. How do the x and y values contribute to the overall understanding of the data?

The x and y values play a crucial role in understanding the data as they represent the variables that are being studied. By analyzing the relationship between these variables, patterns and trends can be identified, and conclusions can be drawn about the data. Additionally, the x and y values can also help in making predictions and drawing inferences about the data set.

5. Can you demonstrate the relationship between the x and y values using a visual aid?

Yes, a figure or graph can be used to visually demonstrate the relationship between the x and y values. By plotting the data points on a graph and examining the pattern and trend of the points, the relationship between the two variables can be easily understood. This visual aid can also help in identifying any outliers or anomalies in the data set.

Similar threads

  • Programming and Computer Science
Replies
1
Views
945
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
1
Views
754
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Calculus and Beyond Homework Help
Replies
19
Views
783
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
1
Views
743
Back
Top