Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

879868 Posts in 33010 Topics- by 24383 Members - Latest Member: celloe

May 25, 2013, 06:32:11 AM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)Points on a given line
Pages: [1]
Print
Author Topic: Points on a given line  (Read 927 times)
Sigma
Level 1
*


View Profile
« on: January 27, 2011, 08:33:26 PM »

Hi all,
      what's the formula to find points(x,y) on a line connecting two points?


All helps are highly appreciated.
Logged
deemen
Level 0
***


View Profile WWW Email
« Reply #1 on: January 27, 2011, 08:43:39 PM »

Simply interpolate the values.

Say your line goes from (x1, y1) to (x2, y2).

x = (1-t) * x1 + t * x2
y = (1-t) * y1 + t * y2

You can find any point on the line by using a t parameter which goes from 0 to 1. 0 is the starting point of the line, 1 will be the end point of the line, and t = 0.5 would be the half-way point along the line.

You can even extrapolate by giving a parameter t > 1.
Logged

Project Lead - Programmer @ Crankshaft Games
Current Project: Party of Sin
BlueSweatshirt
Level 10
*****


the void


View Profile WWW
« Reply #2 on: January 27, 2011, 09:18:15 PM »

f(x) = a(x-h) + k


Yay.

a is the slope of your line,
and (h, k) is your starting point.


Have fun!
Logged

Sigma
Level 1
*


View Profile
« Reply #3 on: January 27, 2011, 09:24:06 PM »

Quote

x = (1-t) * x1 + t * x2
y = (1-t) * y1 + t * y2

f(x) = a(x-h) + k


these formulas can be applied for interpolating rotation values as well?
Logged
BlueSweatshirt
Level 10
*****


the void


View Profile WWW
« Reply #4 on: January 27, 2011, 09:27:48 PM »

Quote

x = (1-t) * x1 + t * x2
y = (1-t) * y1 + t * y2

f(x) = a(x-h) + k


these formulas can be applied for interpolating rotation values as well?

Technically you could use that formula for any concept, so long as there is a start and end point to work with.
Logged

eigenbom
Level 10
*****



View Profile WWW
« Reply #5 on: January 27, 2011, 09:52:52 PM »

f(x) = a(x-h) + k
a is the slope of your line,
and (h, k) is your starting point.

... assuming your line isn't vertical.  My Word!

Logged

Evan Balster
Level 10
*****


dreaming close to metal


View Profile WWW Email
« Reply #6 on: January 27, 2011, 10:29:47 PM »

Interpolating rotation values is a bit more complicated since they cycle.

A simple mid_angle function in Game Maker would look like this:

Code:
//mid_value(ang1, ang2, mid)  [mid ranges from 0 to 1]
var delt;
delt = argument1-argument0;
if (delt > 180) delt -= 360;
if (delt < -180) delt += 360;
return argument0 + delt * argument2;
Logged

Infinite Blank, SoundSelf, Cave Story+, Wreath
voice, accordion, mandolin, (oboe, soon)
Game audio programming consultant.
<plaid/audio>: opensource audio framework
Sigma
Level 1
*


View Profile
« Reply #7 on: January 28, 2011, 01:01:39 AM »

Code:
[quote]
return argument0 + delt * argument2;
[/quote]

why returning (argument0 + delt * argument2) ?
Logged
BlueSweatshirt
Level 10
*****


the void


View Profile WWW
« Reply #8 on: January 28, 2011, 11:34:52 AM »

Interpolating rotation values is a bit more complicated since they cycle.

A simple mid_angle function in Game Maker would look like this:

Code:
//mid_value(ang1, ang2, mid)  [mid ranges from 0 to 1]
var delt;
delt = argument1-argument0;
while (delt > 180) delt -= 360;
while(delt < -180) delt += 360;
return argument0 + delt * argument2;

I made a small adjustment, just so the equation doesn't break in case the the directional value is extremely high or extremely low.
Logged

salade
Level 4
****



View Profile
« Reply #9 on: January 28, 2011, 12:45:07 PM »

If you keep your bounds 0-360 instead of +-180, you can just use modulo.

Code:
//mid_value(ang1, ang2, mid)  [mid ranges from 0 to 1]
var delt;
delt = argument1-argument0;
delt = delt%360;
return argument0 + delt * argument2;


You may have to do something the bounded delta if you want to retain polarity, the next poster can figure it out...
It makes more sense to me to bound it 0-360 since that's how most unit circles are given.
Logged
st33d
Guest
« Reply #10 on: January 29, 2011, 04:59:37 AM »

No, no, no:

Code:
package com.nitrome.util.lerp {

/* Interpolate between two values using the value "t" as a multiplier (t is between 0 and 1) */
public function lerp(a:Number, b:Number, t:Number):Number{
return a + (b-a) * t;
}

}

Code:
package com.nitrome.util.lerp {

/* interpolate between two angles using the value "t" as a multiplier (t is between 0 and 1) */
public function thetaLerp(a:Number, b:Number, t:Number):Number{
a += (Math.abs(b-a) > Math.PI) ? ((a < b) ? (Math.PI*2) : -(Math.PI*2)) : 0;
return a + (b-a) * t;
}

}

Code:
package com.nitrome.util.lerp {

/* interpolate between two angles in degrees using the value "t" as a multiplier (t is between 0 and 1) */
public function degreeLerp(a:Number, b:Number, t:Number):Number{
a += (Math.abs(b-a) > 180) ? ((a < b) ? (360) : -(360)) : 0;
return a + (b-a) * t;
}

}
Logged
Sigma
Level 1
*


View Profile
« Reply #11 on: January 31, 2011, 10:45:34 PM »

thanks for the reply...
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic