Home / Expert Answers / Economics / nbsp-nbsp-nbsp-nbsp-nbsp-nbsp-void-lab-reflect-double-a-4-int-n-double-pt1-3-dou-pa423

(Solved):             void Lab_Reflect(double a[][4], int n, double pt1[3], dou ...



 

 

 

 

Question
Modify the Lab_Reflect() function so that the function performs Scaling of a
polygon with respect to the reference p

 

 

void Lab_Reflect(double a[][4], int n, double pt1[3], double pt2[3],
double b[][4])
/*
    function:     to generate a mirror image of a set of vertices 
                with respect to a mirroring line defined by its
                end points.

    input: a[][4] - a set of vertices
            a[i][0] - the x-coordinate of the (i+1)th vertex
            a[i][1] - the y-coordinate of the (i+1)th vertex
            a[i][2] - the z-coordinate of the (i+1)th vertex
            a[i][3] - the h-coordinate of the (i+1)th vertex

        n - no. of vertices specified in a[][]

        pt1[3] - the first end point of the mirroring line
                pt1[0] - x coordinate
                pt1[1] - y coordinate
                pt1[2] - z coordinate

        pt2[3] - the second end point of the mirroring line
                pt2[0] - x coordinate
                pt2[1] - y coordinate
                pt2[2] - z coordinate
        
    output: b[][4] - the set of transformed vertices
    b[i][0] - the x-coord of the (i+1)th mirrored vertex
    b[i][1] - the y-coord of the (i+1)th mirrored vertex
    b[i][2] - the z-coord of the (i+1)th mirrored vertex
    b[i][3] - the h-coord of the (i+1)th mirrored vertex
*/
{
    /* determine angle of rotation the brings the mirror line to
       become horizontal */
    double dx, dy;
    dx = pt2[0] - pt1[0];
    dy = pt2[1] - pt1[1];

    double ang;
    if (fabs(dx) < 0.0001) ang = 1.57079; // mirror line vertical
    else ang = atan2(dy, dx);
    ang = -1.0*ang; // inverse the sign

    /* store the sine and cosine of the angle, to be used in the
   rotation matrices */
    double cosine = cos(ang); 
    double sine = sin(ang);

    // setup the translation matrix
    double Mtrans1[4][4];
    matrix_identity(Mtrans1);
    for (int i=0; i<3; i++)
        Mtrans1[3][i] = -1.0*pt1[i];

    // setup the inverse translation matrix
    double Mtrans2[4][4];
    matrix_identity(Mtrans2);
    for (i=0; i<3; i++)
        Mtrans2[3][i] = pt1[i];

    // setup the rotation matrix
    double Mrot1[4][4];
    matrix_identity(Mrot1);
    Mrot1[0][0] = cosine;
    Mrot1[1][1] = cosine;
    Mrot1[0][1] = sine;
    Mrot1[1][0] = -1.0*sine;

    double Mrot2[4][4];
    matrix_identity(Mrot2);
    Mrot2[0][0] = cosine;
    Mrot2[1][1] = cosine;
    Mrot2[0][1] = -1.0*sine;
    Mrot2[1][0] = sine;

    // setup the reflection matrix
    double Mref[4][4];
    matrix_identity(Mref);
    Mref[1][1]=-1.0;

    // setup the concatenated matrix
    double Mcon[4][4], temp1[4][4], temp2[4][4], temp3[4][4];
    matrix_matrix(Mtrans1, Mrot1, temp1);
    matrix_matrix(temp1, Mref, temp2);
    matrix_matrix(temp2, Mrot2, temp3);
    matrix_matrix(temp3, Mtrans2, Mcon);


    // Apply multiplication to all vertices
    for (int k=0; k<n; k++) // loop for all vertices
            vector_matrix(a[k], Mcon, b[k]);
}

Question Modify the Lab_Reflect() function so that the function performs Scaling of a polygon with respect to the reference point pt1. The scale factor equals to 1/10 of the distance between pti and pt2. Note 1: The C function sqrt() can be used. The function sqrto provided in the C library computes the square root of a value. For example: double x, y; x= 4.0; y = sqrt(x); y will be given the value 2.0. Note 2: The Scaling transformation is similar to the operation of rotation about an arbitrary point. It consists of three steps: 1. Translate the polygon such that pti is moved to the origin. 2. Perform scaling using the following matrix. 3. Perform inverse translation. s 0 0 0 0 S 0 0 Matrix for scaling: o o 0 0 S 0 O 0 0 1 1 CS Scanned with CamScanner


We have an Answer from Expert

View Expert Answer

Expert Answer


We have an Answer from Expert

Buy This Answer $5

Place Order