Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 473631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:12:23+00:00 2026-05-13T00:12:23+00:00

Looking for the quickest way to calculate a point that lies on a line

  • 0

Looking for the quickest way to calculate a point that lies on a line
a given distance away from the end point of the line:

void calculate_line_point(int x1, int y1, int x2, int y2, int distance, int *px, int *py) 
{
    //calculate a point on the line x1-y1 to x2-y2 that is distance from x2-y2
    *px = ???
    *py = ???
}  

Thanks for the responses, no this is not homework, just some hacking out of
my normal area of expertise.

This is the function suggested below. It’s not close to working. If I
calculate points every 5 degrees on the upper right 90 degree portion of
a circle as starting points and call the function below with the center of the circle as x2,y2 with a distance of 4 the end points are totally wrong. They lie below and to the right of the center and the length is as long as the center point. Anyone have any suggestions?

void calculate_line_point(int x1, int y1, int x2, int y2, int distance)
{

//calculate a point on the line x1-y1 to x2-y2 that is distance from x2-y2

  double vx = x2 - x1; // x vector
  double vy = y2 - y1; // y vector

  double mag = sqrt(vx*vx + vy*vy); // length

  vx /= mag;
  vy /= mag;

// calculate the new vector, which is x2y2 + vxvy * (mag + distance).

  px = (int) ( (double) x2 + vx * (mag + (double)distance) );
  py = (int) ( (double) y2 + vy * (mag + (double)distance) );

}

I’ve found this solution on stackoverflow but don’t understand it completely, can anyone clarify?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T00:12:23+00:00Added an answer on May 13, 2026 at 12:12 am

    I think this belongs on MathOverflow, but I’ll answer since this is your first post.
    First you calculate the vector from x1y1 to x2y2:

    float vx = x2 - x1;
    float vy = y2 - y1;
    

    Then calculate the length:

    float mag = sqrt(vx*vx + vy*vy);
    

    Normalize the vector to unit length:

    vx /= mag;
    vy /= mag;
    

    Finally calculate the new vector, which is x2y2 + vxvy * (mag + distance).

    *px = (int)((float)x1 + vx * (mag + distance));
    *py = (int)((float)y1 + vy * (mag + distance));
    

    You can omit some of the calculations multiplying with distance / mag instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for the quickest/shortest way to get the first value from comma separated
I'm looking for the quickest way to return a set of accounts that have
I am looking for the quickest and easiest way to combine two silverlight projects.
whats the quickest way to generate an XmlDocument object from a file. Im working
Looking at this array, one can see that only the foo6 value differs from
Looking for a way, how to get the values from a hash, in an
I am looking for the quickest and simplest way to determine whether or not
Given a timestamp and a directory path, what would be the quickest way to
Looking for a control that allows to select one text value at a time
Looking for a way to get path of file (and then process it in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.