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 8438993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:52:51+00:00 2026-06-10T07:52:51+00:00

I have to compute the distance from a point to a line (check if

  • 0

I have to compute the distance from a point to a line (check if it is line or a line segment). I am not sure that the bool function IsSegment is working properly. Can i have some suggestions? Thank you.

double Distance_From_Line_to_Point(int *a, int *b, int *c, bool IsSegment) {
    double distance;
    int dot1;
    int dot2;
    distance = Cross_Product(a, b, c) / Distance(a, b);
    if (IsSegment(a,b,c) == true) {
        dot1 = Dot_Product(a, b, c);
        if (dot1 > 0) {
            return Distance(b, c);
        }
        dot2 = Dot_Product(b, a, c);
        if (dot2 > 0) {
            return Distance(a, c);
        }
    }
    return fabs(distance);
}

bool IsSegment(int *a, int *b, int *c) {
    double angle1;
    double angle2;
    angle1 = atan(double(b[1] - a[1]) / (b[0] - a[0]));
    angle2 = atan(double(c[1] - b[1]) / (c[0] - b[0]));
    if ((angle2 - angle1) * (180 / PI) > 90) {
        return false;
    }
    return true;
}
  • 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-06-10T07:52:52+00:00Added an answer on June 10, 2026 at 7:52 am

    Can’t you just use the formula to get the distance?

    So to find the line:

    void getLine(double x1, double y1, double x2, double y2, double &a, double &b, double &c)
    {
           // (x- p1X) / (p2X - p1X) = (y - p1Y) / (p2Y - p1Y) 
           a = y1 - y2; // Note: this was incorrectly "y2 - y1" in the original answer
           b = x2 - x1;
           c = x1 * y2 - x2 * y1;
    }
    

    http://formule-matematica.tripod.com/distanta-de-dreapta.htm

    double dist(double pct1X, double pct1Y, double pct2X, double pct2Y, double pct3X, double pct3Y)
    {
         double a, b, c;
         getLine(pct2X, pct2Y, pct3X, pct3Y, a, b, c);
         return abs(a * pct1X + b * pct1Y + c) / sqrt(a * a + b * b);
    }
    

    Example on how to use the code:

    #include <CMATH>
    
    void getLine(double x1, double y1, double x2, double y2, double &a, double &b, double &c)
    {
        // (x- p1X) / (p2X - p1X) = (y - p1Y) / (p2Y - p1Y) 
        a = y1 - y2; // Note: this was incorrectly "y2 - y1" in the original answer
        b = x2 - x1;
        c = x1 * y2 - x2 * y1;
    }
    
    double dist(double pct1X, double pct1Y, double pct2X, double pct2Y, double pct3X, double pct3Y)
    {
        double a, b, c;
        getLine(pct2X, pct2Y, pct3X, pct3Y, a, b, c);
        return abs(a * pct1X + b * pct1Y + c) / sqrt(a * a + b * b);
    }
    
    
    int main(int argc, char* argv[])
    {
        double d = dist(1,2,3,4,5,6);
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to have JavaScript compute a timestamp returned from PHP's time() function
I have to compute a value involving data from several tables. I was wondering
I have a huge data set and I have to compute for every point
i have been working on query which uses compute by clause for avg and
I have data that I would like to compute some statistics with. The data
I have a object with properties that are expensive to compute, so they are
I have a class that spawns an arbitrary number of worker object that compute
I have been working on a web project that stores locations for users. The
I've written a simple little helper method whoch calculates the distance from a point
I have a homework assignment in which I need to compute the edit distance

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.