Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411472 Posts in 69369 Topics- by 58423 Members - Latest Member: antkind

April 23, 2024, 12:20:14 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Project point onto a line segment? (Solved+Interactive)
Pages: [1]
Print
Author Topic: Project point onto a line segment? (Solved+Interactive)  (Read 7397 times)
Aquin
Level 10
*****


Aquin is over here.


View Profile WWW
« on: December 18, 2010, 06:52:35 PM »



Interactive App:
http://aquinhasa.com/scratch/projection.swf
You can move T1, T2, and P around with the mouse.  Give it a try!

Source code for the app provided:
http://aquinhasa.com/scratch/Main.as
« Last Edit: December 19, 2010, 03:56:00 PM by Aquin » Logged

I'd write a devlog about my current game, but I'm too busy making it.
Aquin
Level 10
*****


Aquin is over here.


View Profile WWW
« Reply #1 on: December 18, 2010, 07:03:32 PM »

Never mind, I have it figured out.  My brain decided to unstupid for a second, thankfully.

If anyone wants the answer, just let me know.  Otherwise, ignore this thread I guess.
Logged

I'd write a devlog about my current game, but I'm too busy making it.
BorisTheBrave
Level 10
*****


View Profile WWW
« Reply #2 on: December 19, 2010, 03:21:29 AM »

It would be better to write the answer anyway - how many times have you got to an archived thread via Google with the question you want, and then a statement that the answer exists.

In that spirit, here it is.

Code:
e = p2 - p1;
normalize(e);
return p1 + e * dot(e, p3 - p1);

Where p1, p2, and p3 are 2d input vectors (i.e. pairs of co-ordinates) and
Code:
function normalize(v)
{
  d = sqrt(v.x*v.x + v.y*v.y)
  v.x /= d;
  v.y /= d;
}
function dot(a,b):float
{
  return a.x*b.x+a.y*b.y;
}
Logged
Aquin
Level 10
*****


Aquin is over here.


View Profile WWW
« Reply #3 on: December 19, 2010, 10:03:38 AM »

Yep, that's pretty much it!  I have another solution (one that doesn't involve the computationally expensive sqrt and relies on a certain dot product being zero), but your write-up is spot-on and much simpler to understand  Beer!
Logged

I'd write a devlog about my current game, but I'm too busy making it.
BorisTheBrave
Level 10
*****


View Profile WWW
« Reply #4 on: December 19, 2010, 01:02:27 PM »

Hm, you've got a point there. How about:

Code:
e = p2 - p1
return p1 + e * dot(e, p3 - p1) / len2(e)

where
Quote
function len2(v):float
{
  return v.x*v.x + v.y*v.y;
}
Logged
Aquin
Level 10
*****


Aquin is over here.


View Profile WWW
« Reply #5 on: December 19, 2010, 01:51:28 PM »

Yeah, your second method is more accurate than the first.  A quick note:

P' = T1 + xT1T2

The dot product should be zero (the lines being perpendicular to each other in the image)

T1T2 • PP' = 0
T1T2 • <P' - P> = 0
T1T2 • <T1 + xT1T2 - P> = 0
T1T2 • <T1 - P> + x || T1T2 ||^2 = 0
x || T1T2 ||^2 = -T1T2 • <T1 - P>
x || T1T2 ||^2 = T1T2 • <P - T1> = T1T2 • T1P

x = (T1T2 • T1P) / || T1T2 ||^2

P' = T1 + ((T1T2 • T1P) / || T1T2 ||^2) T1T2

Which is exactly what you said.  So anyone who cares about the pure math, there she is.

Also, this finds a projected point on the line that runs through T1T2; not a line segment.  So you have to clamp P' to the endpoints if it falls off the line segment.  Simple stuff.

I'll post up the flash app in an hour or so.
« Last Edit: December 19, 2010, 03:48:58 PM by Aquin » Logged

I'd write a devlog about my current game, but I'm too busy making it.
Aquin
Level 10
*****


Aquin is over here.


View Profile WWW
« Reply #6 on: December 19, 2010, 03:54:04 PM »

Source code and test app are now provided.  The source code runs through the why and how of the maths. 

Thanks Boris!  Grin
Logged

I'd write a devlog about my current game, but I'm too busy making it.
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic