How to calculate a mirror point along a line?

In a 2D plane, I have a point and a line. How to get a mirror point along this line?

+7
source share
8 answers

Suppose the equation is a line ax + by + c = 0. Now imagine a straight line, perpendicular to it, that can be represented -bx + ay + d = 0(the product of the slopes of two perpendicular lines is -1). Now the problem is to find d. Put the coordinate of the point on the second line and you will easily get the value d.

The second part is to find a point on the second line that is equidistant as the first point from the first line. To do this, you can find the intersection of two lines. Calculate the differences at xboth the ygiven point and the intersection point. Now add them to the value xand ythe intersection points. This gives you the point you need (you may need to negate the differences - up to the subtraction order that you use).

+6
source

, , , (, ), . - , .

- "" , , , . , . .

(1) L

A * x + B * y + C = 0

. , (A, B) .

, X1(x1, y1) X2(x2, y2),

A = y2 - y1
B = -(x2 - x1)
C = -A * x1 - B * y1

(2) , (A, B).

M = sqrt(A * A + B * B)

A' = A / M
B' = B / M
C' = C / M

A' * x + B' * y + C' = 0

- L, , (A', B') .

(3) P(px, py)

D = A' * px + B' * py + C'

D P L. , P L ( , ).

, L P. P , (A', B') ( "" ), . P ( ), .

(4) P'(px', py'), P |2 * D| L .

" " , P "" L, (A', B') "" . , P "" L, (A', B') "" .

-2 * D ( ) (A', B').

,

px' = px - 2 * A' * D
py' = py - 2 * B' * D

P'(px', py').


, N L P N. , , .

(1)

A*x + B*y + C = 0

L , 1 . .

(2) , P. , ​​

D*x + E*y + F = 0

D E

D = B
E = -A

F , P

F = -D*px - E*py

(3) ,

A*x + B*y = -C
D*x + E*y = -F

. , , , .

N(nx, ny), .

(4)

px' = nx + (nx - px)
py' = ny + (ny - py)

P'(px', py').

, . , , Cramer 3. , , , " " , - . C F , .

+16

, . P n , Q Q :

Q '= Q + 2 (I - nn T) (P - Q)

( - 2x2, n T - n ( n 2x1), nn T - 2x2, n n > T.) , Q ' , P .

/ .

+7

. . Voilà, .

+2

, , ..

P=(x1,y1) and y = ax + b

-, , a = 0 ( , x),

P'(x2,y2), with x2 = x1, y2 = y1 + 2(b-y1)

  • , : y = cx + d, ac = -1

    == > c = -1/a

  • b , P : y1 = -x1/a + d

    == > d = y1 + x1/a

  • : y = -x/a + y1 + x1/a = ax + b

    == > x = (y1 + x1/a -b)/(a ​​+ 1/a), y = a (y1 + x1/a -b)/(a ​​+ 1/a) + b

  • P, , P '.

    == > P '(x2, y2), x2 = x1 + 2 ((y1 + x1/a -b)/(a ​​+ 1/a) - x1) y2 = y1 + 2 (a (y1 + x1/a -b)/(a ​​+ 1/a) + b - y1)

, . , . , .

+1

, . , ; , ... Line.ClosestPoint(Point pt) - , ...

, , , . , m, -1/m. , , , , -1/m, .

public class Line
{
    protected const double epsilon = 1.0e-8;

    public Point Anchor { get; set; }

    public double Slope { get; set; }

    public virtual Point ClosestPoint(Point pt)
    { return Intersection(Make(pt, -1 / Slope)); }

    protected Line(Point anchor, double slope)
    {
        Anchor = anchor;
        Slope = slope;
    }

    public static Line Make(Point anchor, double slope)
    { return new Line(anchor, slope); }

    public virtual Point Intersection(Line line)
    {
        if (lib.Within(line.Slope, Slope, epsilon))              
            if( lib.Within(line.YIntcpt, YIntcpt, epsilon))
                // code for NoInterceptException not included
                throw new NoInterceptException(
                    "The two lines overlap.");
            else return Point.NullPoint;
        // ----------------------------------------
        double tm = Slope, om = line.Slope,
            tx = Anchor.X, ty = Anchor.Y,
            ox = line.Anchor.X, oy = line.Anchor.Y;

        var x = double.IsInfinity(tm) ? tx :
                double.IsInfinity(om) ? ox :
                   (tm * tx - om * ox + oy - ty) /
                          (tm - om);

        var y = double.IsInfinity(tm) ?
               om * (x - ox) + oy :
               tm * (x - tx) + ty;

        return Point.Make(x, y);
    }
}

public struct Point
{
    const double epsilon = 1.0e-12;
    private readonly bool isDef;
    private readonly double y;
    private readonly double x;
    public bool HasValue { get { return isDef; } }
    public bool IsNull { get { return !isDef; } }

    private Point(double xValue, double yValue)
    { x = xValue; y = yValue; isDef = true; }
    public static Point Make(double x, double y)
    { return new Point(x, y); }

    public double X 
    {
        get
        {
                  // code for AlgebraicException not included
            if (IsNull) throw new AlgebraicException("Null Point Object"); 
            return x;
        }
    }
    public double Y
    {
        get
        {
                  // code for AlgebraicException not included
            if (IsNull) throw new AlgebraicException("Null Point Object");
            return y;
        }
    }

    public static Point NullPoint { get { return new Point();}}

    //  Other functionality

} 
+1

, . ,

ax+by+c=0

, = . , , (3,2). , (2,3), , y = x , :

y-x=0

.

    (ax+by+c)/a^2+b^2
    (a^2 means a square.)

, K. , (3,2) yx = 0, K :

(-3+2)/(1+1)= -1/2

, , , , :

 x-2ak
 y-2bk

x y - , a b - , (yx = 0 a = -1, b = 1, c = 0), :

3 - 2 x -1 x -1/2 = 3-1 = 2
2 - 2 x +1 x -1/2 = 2+1 = 3
0

JS, : Q '= Q + 2 (I - nn T) (P - Q),

Q '= Q + 2 (I - d 2vv T) (P - Q)

v= R - P , (, n), P R - ; vv T ; d=1/sqrt(vx*vx + vy*vy) - v. d 2 (= r ), , sqrt

// 2D Points P=[x,y] and R are points on line, 
// Q is point for which we want to find reflection
function mirror(Q,[P,R]) {
  let [vx,vy]= [ R[0]-P[0], R[1]-P[1] ];
  let [x,y]  = [ P[0]-Q[0], P[1]-Q[1] ];
  let r= 1/(vx*vx+vy*vy);
  return [ Q[0] +2*(x -x*vx*vx*r -y*vx*vy*r), 
           Q[1] +2*(y -y*vy*vy*r -x*vx*vy*r)  ];
}

console.log( mirror([3,2], [[2,4],[4,5]]) )
Hide result
0

All Articles