Public Group

#### Archived

This topic is now archived and is closed to further replies.

# Getting a Point That's 200 Pixels Up a Line

This topic is 5934 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites
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]

##### Share on other sites
I mean the point where the distance between that point and A is 200.

##### Share on other sites
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.

##### Share on other sites
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);

##### Share on other sites
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]

##### Share on other sites
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.

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633734
• Total Posts
3013587
×