Finding the xy's between two points

Get help using Construct 2

Post » Tue Apr 07, 2015 1:22 pm

Just looking for a good formula to get all the points in between two points.. without having to add a bunch of conditions.
Image Image
B
164
S
49
G
113
Posts: 7,648
Reputation: 78,995

Post » Tue Apr 07, 2015 1:43 pm

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
Game design is all about decomposing the core of your game so it becomes simple instructions.
B
43
S
18
G
18
Posts: 2,106
Reputation: 16,426

Post » Tue Apr 07, 2015 2:08 pm

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?
Image Image
B
164
S
49
G
113
Posts: 7,648
Reputation: 78,995

Post » Tue Apr 07, 2015 2:32 pm

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?
You think you can do these things, but you can't, Nemo!
Just keep reading.
Just keep learning.
B
63
S
16
G
9
Posts: 1,429
Reputation: 12,603

Post » Tue Apr 07, 2015 3:00 pm

newt wrote: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)
Game design is all about decomposing the core of your game so it becomes simple instructions.
B
43
S
18
G
18
Posts: 2,106
Reputation: 16,426

Post » Tue Apr 07, 2015 4:32 pm

What is your use case for getting all the points between two lines? You mean for picking objects on the line?
Last edited by R0J0hound on Tue Apr 07, 2015 4:46 pm, edited 1 time in total.
B
84
S
27
G
70
Posts: 4,921
Reputation: 49,534

Post » Tue Apr 07, 2015 4:39 pm

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
B
57
S
19
G
9
Posts: 191
Reputation: 9,091

Post » Tue Apr 07, 2015 4:55 pm

@R0J0hound
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.

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

@DUTOIT
In y=mx+c (c=0) its m*(x+c)?
Image Image
B
164
S
49
G
113
Posts: 7,648
Reputation: 78,995

Post » Tue Apr 07, 2015 8:40 pm

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)
B
84
S
27
G
70
Posts: 4,921
Reputation: 49,534

Post » Tue Apr 07, 2015 11:43 pm

Yeah.
@Ashley
We need a line expression please.
Image Image
B
164
S
49
G
113
Posts: 7,648
Reputation: 78,995

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 3 guests