Jump to content
  • Advertisement

Archived

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

rjahrman

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!