# Finding the xy's between two points

0 favourites
• 11 posts
From the Asset Store
Footsteps SFX Two contains 450 sounds of steps and jumps on different surfaces.
• Just looking for a good formula to get all the points in between two points.. without having to add a bunch of conditions.

• Well, if A.x and A.y are the coordinates for point A, and same logic for B

with let's say course being a variable between 0 and 1

you have lerp(A.X, B.X, course) returning an X value

lerp(A.Y, B.Y, course) will return the Y value on the same line corresponding to that X, as long as course is between 0 and 1, the point will be between them.

EDIT: if you prefer the actual equation of the line (AB), you can have it:

Y=(A.Y-B.Y)/(A.X-B.X)*X+ init

Init being found with

A.Y=(A.Y-B.Y)/(A.X-B.X)*A.X+ init

init= A.Y-A.X*(A.Y-B.Y)/(A.X-B.X)

In the end, If I am correct,

Y=(A.Y-B.Y)/(A.X-B.X)*X+A.Y-A.X*(A.Y-B.Y)/(A.X-B.X)

or Y= (X-A.X)*(A.Y-B.Y)/(A.X-B.X) + A.Y

Depends on what you want to do exactly after that...if it is verifying if a point C is between A and B, you just verify that A.X<C.X<B.X

and that C.Y= (C.X-A.X)*(A.Y-B.Y)/(A.X-B.X) + A.Y

• I've used lerp before with distance(). It seemed a little overly complicated, but then again so does this.

Like I don't know what init means, and I'm not sure how to get the x component out of that.

Or should I say overly simplified?

• ## Try Construct 3

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• Would this help?

Just copied this, but you can find a lot of various methods. But this looked pretty good.

Find the slope m=(x1-x2)/(y1-y1) and make a loop of x where y=mx+c (c=0). now get

(x,y) for a range of x.

Your two points: (x1, y1) (x2, y2):

m = (y1 - y2) / (x1-x2);

c = y1 - x1 * m;

Then, for any given x:

y = mx + c;

This looks interesting(a little complicated)

If it is on a straight line, can't you find midpoint then midpoint between point a and midpoint, and midpoint and point b, and repeat etc?

• I've used lerp before with distance(). It seemed a little overly complicated, but then again so does this.

Like I don't know what init means, and I'm not sure how to get the x component out of that.

Or should I say overly simplified?

I may have over complicated it, it is the same as dutoit ( init was a value, which dutoit calls c)

• What is your use case for getting all the points between two lines? You mean for picking objects on the line?

• linear BÃ©zier curve is what you need

dx = (1-t) * x1 + t * x2;

dy = (1-t) * y1 + t * y2;

t - is a coef. from 0 to 1.

http://commons.wikimedia.org/wiki/File: ... _1_big.gif

• Say for example I wanted to place tiles between the two points. Snapped accordingly.

I'm not terribly worried about picking objects between the points, theres a system command to do that. I think that would work.

Thanks, thats pretty much the same as the lerp way.

In y=mx+c (c=0) its m*(x+c)?

• For that you could use Bresenham's line algorithm for a nice result. But it isn't simple.

You could use lerp to do it like this:

https://dl.dropboxusercontent.com/u/542 ... _step.capx

but you'll have to handle the case when deltay is more than deltax and do the loop in that direction.

Edit: actually this would do it:

variable delta= max(abs(x1-x0), abs(y1-y0))/32

repeat delta times

--- create sprite at (round(lerp(x0, x1, loopindex/delta)/32)*32), round(lerp(y0, y1, loopindex/delta)/32)*32)

• Yeah.

We need a line expression please.

• Here's what I came up with using sin/cos as alternative.

https://dl.dropboxusercontent.com/u/666 ... oline.capx

It ain't pretty.

• 11 posts