Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distance from a point to a line/segment

Tags:

c++

c

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;
}
like image 629
laura Avatar asked Aug 26 '12 17:08

laura


People also ask

What is the distance formula for a segment?

A line segment has endpoints at and . What is the distance of this segment? Explanation: To find the distance, we use the distance formula: \displaystyle d^2=\Delta x^2+\Delta y^2.


2 Answers

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;
}
like image 109
Thanatos Avatar answered Sep 30 '22 18:09

Thanatos


Distance from a point to a line

Distance from points to line

You need 2 formulas:

Line formula: source this answer.

    private Vector2 m_point1;
    private Vector2 m_point1;
    private float m_A;
    private float m_B;
    private float m_C;
    public void CalculateLine()
    {
        m_A = m_point1.y - m_point2.y;
        m_B = m_point2.x - m_point1.x;
        m_C = m_point1.x * m_point2.y - m_point2.x * m_point1.y;
        if(m_A == 0 && m_B == 0)
        {
            Debug.LogError("Line error: A & B = 0");
        }
    }

Distance from point to line: source Wikipedia

public float Distance2DPointToLine(Vector2 point)
{
    return Mathf.Abs(m_A * point.x + m_B * point.y + m_C) / 
        Mathf.Sqrt(m_A * m_A + m_B * m_B);
}

Distance from a point to a line segment

It depends what you define "Distance from a point to a line segment"

Maybe distance from a point to a line segment is the distance from a point to the segment's middle point: To middle point?

Maybe the distance is available if the point can be projected on the line segment Distance: To be or not to be

Maybe you didn't imagine what's result when you ask about segment, so I can't answer the segment part for you.

like image 42
123iamking Avatar answered Sep 30 '22 16:09

123iamking