How can I find the general form equation of a line from two points?

前端 未结 5 1953
野的像风
野的像风 2021-02-02 09:48

Given the input:

double x1,y1,x2,y2;

How can I find the general form equation (double a,b,c where ax + b

相关标签:
5条回答
  • 2021-02-02 09:58

    Get the tangent by subtracting the two points (x2-x1, y2-y1). Normalize it and rotate by 90 degrees to get the normal vector (a,b). Take the dot product with one of the points to get the constant, c.

    0 讨论(0)
  • 2021-02-02 10:09

    If you start from the equation y-y1 = (y2-y1)/(x2-x1) * (x-x1) (which is the equation of the line defined by two points), through some manipulation you can get (y1-y2) * x + (x2-x1) * y + (x1-x2)*y1 + (y2-y1)*x1 = 0, and you can recognize that:

    • a = y1-y2,
    • b = x2-x1,
    • c = (x1-x2)*y1 + (y2-y1)*x1.
    0 讨论(0)
  • 2021-02-02 10:16

    If you start from the equation of defining line from 2 points

    (x - x1)/(x2 - x1) = (y - y1)/(y2 - y1)
    

    you can end up with the next equation

    x(y2 - y1) - y(x2 - x1) - x1*y2 + y1*x2 = 0
    

    so the coefficients will be:

    • a = y2 - y1
    • b = -(x2 - x1) = x1 - x2
    • c = y1*x2 - x1*y2

    My implementation of the algorithm in C

    inline v3 LineEquationFrom2Points(v2 P1, v2 P2) {
        v3 Result;
    
        Result.A = P2.y - P1.y;
        Result.B = -(P2.x - P1.x);
        Result.C = P1.y * P2.x - P1.x * P2.y;
    
        return(Result);
    }
    
    0 讨论(0)
  • 2021-02-02 10:17

    Shortcut steps: "Problem : (4,5) (3,-7)" Solve: m=-12/1 then 12x-y= 48 "NOTE:m is a slope" COPY THE NUMERATOR, AFFIX "X" Positive fraction Negative sign on between. (tip: simmilar sign = add + copy the sign) 1.Change the second set into opposite signs, 2.ADD y1 to y2 (means add or subtract them depending of the sign), 3.ADD x1 to x2 (also means add or subtract them depending of the sign), 4.Then Multiply 12 and 1 to any of the problem set. After that "BOOM" Tada!, you have your answer

    0 讨论(0)
  • 2021-02-02 10:21
    #include <stdio.h>
    main()
    {
        int a,b,c;
        char x,y;
        a=5;
        b=10;
        c=15;
        x=2;
        y=3;
        printf("the equation of line is %dx+%dy=%d" ,a,b,c);
    }
    
    0 讨论(0)
提交回复
热议问题