pythagorean theorem to find distance between two points

I am trying to limit the length of an old fashioned Qix video game so that the ball only travels within certain decreased parameters, rather than the full arena, when a specific command is entered in the command line. The full arena is 500, so I was trying to make the decreased arena be 400. I think that I need to use the pythagorean theorem to find the distance between x1 and y1, as well as x2 and y2, and then take that hypotenuse value and decrease it by a particular quantity. I have no idea how to make this happen, and have tried so many things that all result in errors. Please help!

Answers (2)

Read about hypot()

6 Comments

I did, and I tried putting it in like that (C = sqrt(abs(A).^2 + abs(B).^2)) but it gives me an error. I also don't know how to shorten the hypotenuse after finding it, which is not included in the hypot() information.
What error did you encounter?
it just says error in line C = sqrt(abs(A).^2 + abs(B).^2). It doesn't further specify.
so see sir Walter's answer below
At the moment we do not know that you have defined A and B or what you defined them as and how they relate to x1, x2, y1, y2 .
size(x1) size(x2) size(y1) size(y1) size(A) size(B) ?

Sign in to comment.

[theta, r] = pol2cart(x2-x1, y2-y1);
[newrelx, newrely] = cart2pol(theta, r * reduction_factor);
new_x2 = x1 + newrelx; new_y2 = y1 + newrely;

1 Comment

By the way, this can be programmed more directly:
new_x2 = x1 + (x2 - x1) * reduction_factor;
new_y2 = y1 + (y2 - y1) * reduction_factor;

Sign in to comment.

Categories

Find more on Signal Processing in Help Center and File Exchange

Asked:

jat
on 26 Nov 2018

Commented:

on 26 Nov 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!