Author Topic: AcGeVecto2d::mirror  (Read 1575 times)

0 Members and 1 Guest are viewing this topic.

pkohut

  • Guest
AcGeVecto2d::mirror
« on: June 08, 2010, 02:40:30 AM »
Ok, this one has me stumped and getting nowhere.  Can someone explain how this is implemented? Thanks
Quote
AcGeVector2d & mirror(const AcGeVector2d & line);

line - Input line
Returns the 2D vector which is the result of mirroring of this vector with respect to 2D line line in the same plane.

Quote
2d vectors v1, v2, v3
v1.set(2.0, 2.0); v2.set(0.0, 0.0)
result of v1.mirror(v2) = {0.0, 0.0}

v1.set(2.0, 2.0); v2.set(1.0, 1.0)
result of v1.mirror(v2) = {4.0, 4.0}

v1.set(2.0, 2.0); v2.set(2.0, 2.0)
result of v1.mirror(v2) = {16.0, 16.0}

v1.set(2.0, 2.0); v2.set(3.0, 3.0)
result of v1.mirror(v2) = {36.0, 36.0}

v1.set(2.0, 3.0); v2.set(4.0, 5.0)
result of v1.mirror(v2) = {102.0, 107.0}

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: AcGeVecto2d::mirror
« Reply #1 on: June 08, 2010, 08:45:18 AM »
Hi,

As far as can read C++ (and it's not far at all) and from what I can see with the examples you provide, it seems to me that the result coordinates are:
v3.X = (scalar * v2.X) - (area * v2.Y);
v3.Y = (scalar * v2.Y) + (area * v2.X);
where:
scalar is the scalar product (or dot product) of v1 v2:
(v1.X * v2.X) + (v1.Y * v2.Y)
area is the algebraic (signed) area of the triangle descibed by the 2 vectors:
(v1.X * v2.Y) - (v1.Y * v2.X)

As we're in a LISP forum, her're two ways to implement this 'mirror' function

Code: [Select]
(defun mirror (v1 v2)
  ((lambda (s a)
     (list (- (* s (car v2)) (* a (cadr v2)))
   (+ (* s (cadr v2)) (* a (car v2))))
   )
    (+ (* (car v1) (car v2)) (* (cadr v1) (cadr v2))) ; scalar product
    (- (* (car v1) (cadr v2)) (* (cadr v1) (car v2))) ; algebric area
  )
)

(defun mirror (v1 v2)
  ((lambda (x1 x2 y1 y2)
     (list
       (+ (* x1 (- (* x2 x2) (* y2 y2))) (* 2 x2 y1 y2))
       (+ (* y1 (- (* y2 y2) (* x2 x2))) (* 2 x1 x2 y2))
     )
   )
    (car v1)
    (car v2)
    (cadr v1)
    (cadr v2)
  )
)
Speaking English as a French Frog

pkohut

  • Guest
Re: AcGeVecto2d::mirror
« Reply #2 on: June 08, 2010, 11:56:58 AM »
As we're in a LISP forum, her're two ways to implement this 'mirror' function

Again, thank you Gile.  I posted these in the lisp forum cause that's were most of the math brain power is  :-)  Here's C++ versions for those interested.

Code: [Select]
double DotPerp(const AcGeVector2d & v1, const AcGeVector2d & v2)
{
    return v1.x * v2.y - v1.y * v2.x;
}

AcGeVector2d Mirror(const AcGeVector2d & v1, const AcGeVector2d & v2)
{
    double dotProd = v1.dotProduct(v2);
    double dotPerp = DotPerp(v1, v2);

    return AcGeVector2d(dotProd * v2.x - dotPerp * v2.y, dotProd * v2.y + dotPerp * v2.x);

}