Saturday, January 21, 2012

Finding the reflection point of a particular point assuming a line as the reflection plane.

#include <iostream>


typedef struct
{
    double x, y;
}pp;


pp A, B, C, P;


int main()
{
    while(1)
    {
        printf("Enter the two endpoints of the reflection line:\n");
        scanf("%lf %lf %lf %lf", &A.x, &A.y, &B.x, &B.y);


        printf("Enter a point:\n");
        scanf("%lf %lf", &P.x, &P.y);


        double a1, b1, c1, a2, b2, c2, t;
        //calculating line
        /*A = y2-y1
        B = x1-x2
        C = A*x1+B*y1*/
        a1 = B.y - A.y;
        b1 = A.x - B.x;
        c1 = a1*A.x + b1*A.y;


        t = a1;
        a2 = b1;
        b2 = -1*t;
        c2 = a2*P.x+b2*P.y;


        double det;
        pp intrs;


        det = a1*b2 - a2*b1;


        if(det == 0)
        {
            printf("AB and CD Lines are Parallel\n");
        }
        else
        {
            intrs.x = (b2*c1 - b1*c2)/det;
            intrs.y = (a1*c2 - a2*c1)/det;


            printf("The Intersection Point is (x, y) = (%lf, %lf)\n", intrs.x, intrs.y);
        }


        pp p_p;
        /*X' = Y - (X - Y)*/
        p_p.x = intrs.x - (P.x - intrs.x);
        p_p.y = intrs.y - (P.y - intrs.y);


        printf("Reflection point is (%lf %lf)\n", p_p.x, p_p.y);
    }
    return 0;
}


/*Rotation
Rotation doesn't really fit in with line intersection, but I felt that it
would be good to group it with reflection. In fact, another way to find the
reflected point is to rotate the original point 180 degrees about Y.


Imagine that we want to rotate one point around another, counterclockwise
by θ degrees. For simplicity, lets assume that we are rotating about the origin.
In this case, we can find that x' = x Cos(θ) - y Sin(θ) and y' = x Sin(θ) + y Cos(θ).
If we are rotating about a point other than the origin, we can account for this
by shifting our coordinate system so that the origin is at the point of rotation,
doing the rotation with the above formulas, and then shifting the coordinate
system back to where it started.*/

No comments:

Post a Comment