Advertisement

Getting a Point That's 200 Pixels Up a Line

Started by September 15, 2002 05:10 PM
5 comments, last by rjahrman 22 years, 2 months ago
I have two coordinate pairs (A and B, for example). How can I find the point that is 1) colinear to A and B, and 2) 200 pixels away from A?
What do you mean by "200 pixels". I assume that you're not primarily interested in straight horizontal nor straight vertical lines. Do you mean like: What is the Y-coordinate of the point that intersects the line at X-coordinate 200? For this you can use the standard line equation:

y = k * x + m

where slope: k = (By - Ay) / (Bx - Ax)
displacement: m = Ay - k * Ax

The terminology I use is probably lousy, but hope you get the picture. It should be in any math book.

[edited by - CWizard on September 15, 2002 6:24:39 PM]
Advertisement
I mean the point where the distance between that point and A is 200.
But in what unit? What would you say the distance is between (0,0)-(14,6)? If your answer is about 15.23, we can use something like this: (pseudo-code)

// Ax,Ay = First point
// Bx,By = Second point
// d = Distance from first point

// Angle of line
V = atan2((By - Ay), (Bx - Ax));

// Point d units from A
Px = Ax + cos(V);
Py = Ay + sin(V);

Something like that. Anyone can corret me if I''m wrong.
Just for fun I coded a function that do this:
  #include <math.h>	void PointOnLine(int Ax, int Ay, int Bx, int By, int *Cx, int *Cy, int d){	float fAngle = atan2f((float)(By - Ay), (float)(Bx - Ax));		*Cx = Ax + (int)cosf(fAngle);	*Cy = Ay + (int)sinf(fAngle);}	int	cxPoint,	cyPoint;	PointOnLine(100, 50, 800, 450, &cxPoint, &cyPoint, 200);  
Damn, made an error in the above code. Here's a fixed one, also including an alternative method:
    #include <math.h>	void PointOnLine(int Ax, int Ay, int Bx, int By, int *Cx, int *Cy, int d){	float fAngle = atan2f((float)(By - Ay), (float)(Bx - Ax));		*Cx = Ax + (int)(cosf(fAngle) * (float)d);	*Cy = Ay + (int)(sinf(fAngle) * (float)d);}	void PointOnLine2(int Ax, int Ay, int Bx, int By, int *Cx, int *Cy, int d){	int Vx = Bx - Ax;	int Vy = By - Ay;		float fLength = sqrtf((float)(Vx * Vx + Vy * Vy));		*Cx = Ax + ((float)Vx / fLength * (float)d);	*Cy = Ay + ((float)Vy / fLength * (float)d);}	int	cxPoint,	cyPoint;	PointOnLine(100, 50, 800, 450, &cxPoint, &cyPoint, 200);    
Not tested, though.

[edited by - CWizard on September 15, 2002 8:15:39 PM]
Advertisement
quote: Original post by rjahrman
I mean the point where the distance between that point and A is 200.


That is a circle around A with a radius of 200.
A point cannot be colinear.

This topic is closed to new replies.

Advertisement