# Distance Between Points (Range Question)

This topic is 1199 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello,

I'm wondering if someone could teach me how to find the farthest distance an object could be in a range of points. I know the formula for distance between points, but not sure how to find the two farhest away points from this range. The numbers are from the functions below.

Y = random(enemyUnit.Y - 80 , enemyUnit.Y + 80)
X = random(enemyUnit.X  - 75, enemyUnit.X + 75)

These are two random points being set to X, Y.

After this is calculated I lerp towards X, Y point from current point. The last thing I do is test distance away from the new point and see if it's so many px away yet. This is what trips me up. The distance event doesn't fire every time, because I might be short. I need to know how to figure out ranges like this so being short can't happen.

How do you tell the farthest away these two points can be. This is really dumb,  I know, lol.

To be honest I'm not even sure if this can be calculated, or if I have enough numbers. For a more visual look I'm messing with Construct. The events are below.

##### Share on other sites

Not entirely sure what you are talking about here, but I'll try.

X and Y will be somewhere in the rectangle (-80, -75) to (80, 75), with enemyPosition being the center. So, the farthest point is in one of the corners. All corners have the same distance from the center of a rectangle, so just pick one and calculate the distance.

sqrt(80 * 80 + 75 * 75) ~= 110

Does that help?

##### Share on other sites

Well I'm horrible with math. I'm doing this wrong anyway. What happens with the current set up is the enemy just moves towards a certain point. Then the next turn he hardly moves at all. It's like the current calculation just picks a point moves to it, then all other points picked on next turns are only pixels away from that first move.

I'm using a behavior called line of site. It sets up a line of site so many pixels away from the center of the image (image point to be exact). So really I want to pick any random location within that line of sight. That's what's tripping me up..

##### Share on other sites

If you have a line from (x1, y1) to (x2, y2), and you want a random point between (x1, y1) and (x2, y2), use

r = random (0 to 1, inclusive upper and lower border)
x = x1 + (x2 - x1) * r
y = y1 + (y2 - y1) * r

To understand how this works, see (x1, y1) as the starting point. Think only along the horizontal x axis. The value (x2 - x1) is the number of pixels you need to add to x1, to arrive at x2. (Verification: x1 + (x2-x1) == x1 + x2 - x1 == x1 - x1 + x2 == x2). "r" is a factor between 0 and 1, telling the fraction of points you want to add. For r=0 you add nothing, and you stay in x1, for r=1, you add (x2-x1), and arrive in x2.

For r=0.5 you end up in the middle, since you add (x2-x1) /2, ie you add half the points.

For y, the same applies. Since you use the same r, and the line is straight, you stay on the line for each 'r' value.

##### Share on other sites

"If you have a line from (x1, y1) to (x2, y2), and you want a random point between (x1, y1) and (x2, y2), use"

I'm not trying to find a point on a line. I'm trying to pick any point from the sprite so long as it's about 100px away. Say the sprite can move in 8 directions.How do I pick a  single point from that sprite, within 100 px? Check the image below. The srpite is in the center. The outter square is every where it could pick to move. postive x and y is following the axes arrows. So positive y is heading down the screen.

##### Share on other sites

Ah, I think I get it. So you want to have a point at roughly 100 px distance, but random direction.

To get this, first get a random direction. A good way to do that is to calculate a random point on the unit circle, like this:

angle = random(0, 2*PI); // assuming radians, if your programming language uses degrees then it's obviously random(0, 360)

dirX = sin(angle);

dirY = cos(angle);

Then, just extend the direction to the desired length by multiplying:

length = random(90, 110); // roughly 100 px, you can also have exactly 100 px

dirX = dirX * length;

dirY = dirY * length;

Now you have a point at the desired distance in a random direction!

I hope that helps!

##### Share on other sites

Yes, that's amazing. So anywhere within the 100px block just add random(1, 100).

This is great. Can you tell me exactly what kind of math this falls under? Linear algebra? I really need to brush up. A few books wouldn't hurt.

##### Share on other sites

Wait, see if I made a mistake. I did this.

X = sin(random(0, 360)) * random(1, 100);
y = cos(random(0,360))) * random(1, 100);

but the spirte always shoots off up into the top left corner of the screen instead of moving within it's small 100px movable space.

Edited by JeremyB

##### Share on other sites

You want a pixel from the sprite (sx, sy), such that sx <= 100 and sy <= 100 ?

sprite top_left = (tx, ty)

obviously, tx <= 100 and ty <= 100, or else you cannot pick a pixel.

len_x = 100 - tx;  // Number of pixels you can pick right of the sprite left edge
if len_x > sprite_width then len_x = sprite_width;  // But no more than the width of the sprite.

len_y = min(100 - ty, sprite_height); // Same as above, but vertical, and more compact.


so from the top-left of the sprite, you have len_x pixels horizontally and len_y pixels vertically that are within the 100, 100 range.

r1 = random(); r2 = random(); // pick 2 random numbers 0..1;

pos_x = tx + int(len_x * r1); // horizontal position is left of the sprite + fraction * number of pixels you can pick from.
pos_y = ty + int(len_y * r2);

##### Share on other sites

I have no clue guys.

X = tan(enemyUnit.X )+ 32 * random(0, 1)

Y = tan(enemyUnit.Y)+ 32 * random(0, 1)

This does the same thing as the other guys explanation. The srite just shoots off to the top left... same point every time.

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633704
• Total Posts
3013460
×