Find coordinates given original point and slope

Stephen

New member
Joined
Apr 7, 2011
Messages
2
Here's my problem: Given a point (x,y) and a slope m what is the formula to find the point (a,b) which lies d units away from point (x,y) at slope m when point (a,b) is on the left side of (x,y). When (a,b) is on the right side?

I'm not asking someone to just give me the answer, I need to know how to find the answer. This is not for homework, it's for a program i'm writing that requires collision detection and something to position items to a touching position of they happen to collide / intersect. Googling around, I've seen some crazy stuff involving theta and arctangent, which I haven't been over because I'm in pre-calculus.

I also saw the formulas:
x = D/sqrt(1+m^2), and y = mD/sqrt(1+m^2) for right side of (x,y)?
or
x = -D/sqrt(1+m^2), and y = -mD/sqrt(1+m^2) for left side of (x,y)?

however I don't understand how to use these formulas given a point.
If these formulas are the answer could someone please explain to me how to use them?
 
Sometimes, one comes to a point where it is time to upgrade one's math skills. Maybe the program is over your head?

1) I suspect you will have difficulty defining "left" and "right". What do you do with a horizontal line?

2) The angle of inclination is given by tan(θ)=byax=m\displaystyle \tan(\theta) = \frac{b-y}{a-x} = m. Therefore, a line containing your point would be (yb)=m(xa)\displaystyle (y-b) = m\cdot (x-a)

3) All points a given distance, r, from a point (q,s) can be described by (xq)2+(ys)2=r2\displaystyle (x-q)^{2} + (y-s)^2 = r^2

4) Unfortunately this much information narrows the choices down to two possible points. You'll need more to get it to only 1.

5) Actually, I'd examine a rotational mapping. Let us know when you are ready.
 
graph.png


In the example above, given the point a, slope of the line, and distance away from point a, there are 2 points, b and c, that satisfy the condition, 5 units away from point a gives a circle of possibilities. Constraining them to a slope (the point must lie on the line of given slope which runs through point a) brings it down to 2 possible points. I have to find out the formula to find the coordinates of point b and c.

I have already accounted for special cases when the slope is undefined or flat. A different process easily corrects collisions on the same axis.

I hope you understand now, there are 2 points, I need to know how to find both of their coordinates.

tkhunny said:
4) Unfortunately this much information narrows the choices down to two possible points. You'll need more to get it to only 1.
I need to find both points.

Edit:
So I experimented with the formulas and got it working perfectly with this code.
Code:
if (zombiesx[x] < zombiesx[y]) {
		//Zombie 1 is left of zombie 2
		zombiesx[y] = zombiesx[y] + ((fixdist)/(sqrt(1+m*m)));
		zombiesy[y] = zombiesy[y] + ((m*fixdist)/(sqrt(1+m*m)));
		zombiesx[x] = zombiesx[x] + (-1*(fixdist/(sqrt(1+m*m))));
		zombiesy[x] = zombiesy[x] + (-1*((m*fixdist)/(sqrt(1+m*m))));
	}
	else if (zombiesx[x] > zombiesx[y]) {
		//Zombie 2 is left of zombie 1
		zombiesx[y] = zombiesx[y] + (-1*(fixdist)/(sqrt(1+m*m)));
		zombiesy[y] = zombiesy[y] + (-1*(m*fixdist)/(sqrt(1+m*m)));
		zombiesx[x] = zombiesx[x] + ((fixdist/(sqrt(1+m*m))));
		zombiesy[x] = zombiesy[x] + (((m*fixdist)/(sqrt(1+m*m))));
	}
	else if (zombiesy[x] > zombiesy[y]) {
		//Zombie 2 is directly above zombie 1
		zombiesx[y] = zombiesx[y] - fixdist;
		zombiesx[x] = zombiesx[x] + fixdist;
	}
	else {
		//Zombie 1 is directly above zombie 2
		zombiesx[y] = zombiesx[y] + fixdist;
		zombiesx[x] = zombiesx[x] - fixdist;
	}
This translates into this mathematically:
Givens:
(X,Y) - starting coordinates
M - slope
D - distance away from (x,y)
Results:
(A,B) - Resulting coordinates that lies distance d from point (x,y) constrained to slope m
If the destination is on the right side:
A = X + D/(sqrt(1+m^2))
B = Y + M*D/(sqrt(1+m*m))
If the destination is on the left side:
A = X + -1*D/(sqrt(1+m*m))
B = Y + -1*M*D/(sqrt(1+m*m))

I believe my math translation is correct, but I'm 100% sure the code snippet works. Tested it with my wii and zombies collide properly and when they intersect, it's immediately corrected.
 
Top