|
Title: Trig - Position along a line. Post by: kinglake on June 22, 2012, 02:48:04 PM Hi guys.. Just having a bit of a mental block at the moment. I'm currently working on a platform game in Actionscript 3 and am trying to get ramps in.
I think i've roughly worked out how to do this, but i'm a little stuck. What i'm trying to work out is what the y position of the line if i have the x position. (http://www.freeimagehosting.net/t/g668t.jpg) (http://www.freeimagehosting.net/g668t) So if i have the position of 'x', a', 'b' and 'c', how can i work out the position of 'y'? My first thoughts were to work out the length of the side x/y and add this length to the position of 'x', however i'm a little confused as to how i would do this. If anyone could point me in the right direction i'd be most grateful. Title: Re: Trig - Position along a line. Post by: zacaj on June 22, 2012, 02:57:16 PM ((c.x-a.x) /(x.x-a.x)) * ((c.y-a.y))+a.y
I think that should so do it... Title: Re: Trig - Position along a line. Post by: Mittens on June 22, 2012, 02:58:57 PM havent done this in a while but i think y = x * tan(angle between a and b in degrees)
Title: Re: Trig - Position along a line. Post by: Polly on June 22, 2012, 03:06:08 PM @zacaj - The division should be the other way around ::)
Code: float y = a.y+(c.y-a.y)*(x-a.x)/(c.x-a.x); Title: Re: Trig - Position along a line. Post by: zacaj on June 22, 2012, 03:16:52 PM @zacaj - The division should be the other way around ::) Code: float y = a.y+(c.y-a.y)*(x-a.x)/(c.x-a.x); Yeah I noticed that as soon as I posted and then my Internet decided it wanted to take a break... Title: Re: Trig - Position along a line. Post by: syn9 on June 22, 2012, 03:18:55 PM Assuming that the origin is at b and your starting point is x1,y1 = a.x,a.y
The slope of the line = m = (c.y - a.y) / (c.x - a.x) The general point slope line formula is (y - y1) = m(x - x1) Solving for y becomes y = m(x - x1) + y1 In your case this is y = (c.y - a.y) / (c.x - a.x) * (x - a.x) + a.y Hope that helps! Edit: --- looks like zacaj's post was updated while I was typing and is equivalent. Title: Re: Trig - Position along a line. Post by: kinglake on June 23, 2012, 03:17:50 AM Wow, cheers for the quick replies guys. I'm now about to try implementing.
Edit: Works perfectly, thanks a lot guys! |