C++ Code for finding Parametric Equation and Vector form for Line L through two points p1 and p2

Description:

Given two points P_1 (x_1, y_1, z_1) and P_2 (x_2, y_2, z_2) we are to find the parametric equation and vector form of line through these points.

The parallel vector to the line is,

\overrightarrow{P_1 P_2} = < x_2 - x_1, y_2 - y_1, z_2 - z_1 >

So the parallel vector of P_1 (2, 4, -1) and P_2 (5, 0, 7) is,

\overrightarrow{P_1 P_2} = < 5 - 2, 0 - 4, 7 - (-1) > = < 3, -4, 8 >

Note that \vec{v} is the parallel vector. We know,

\vec{r} = \vec{r_0} + t \vec{v}

where,
\vec{r} = < x, y, z >
\vec{r_0} = < x_0, y_0, z_0 >
\vec{v} = < a, b, c >

So the vector equation form is,

\vec{r} = \vec{r_0} + t \vec{v}
< x, y, z > = < x_0, y_0, z_0 > + t < a, b, c >

Also the parametric form is,

x = x_0 + at
y = y_0 + bt
z = z_0 + ct

Now we can use either vector from origin to P_1 or origin to P_2 . Here the vector from origin to  origin to P_1 which is (0, 0, 0) to (2, 4, -1) is,

\vec{r_0} = < 2 - 0, 4 - 0, -1 - 0 > = < 2, 4, -1 >

So the vector equation of line through P_1 and P_2 is,

< x, y, z > = < 2, 4, -1 > + t < 3, -4, 8 >

calculating further,
< x, y, z > = < 2, 4, -1 > + < 3t, -4t, 8t >
< x, y, z > = < 2 + 3t, 4 - 4t, -1 + 8t >

Finally,

L: x = 2 + 3t, y = 4 - 4t, z = -1 + 8t

Similarly the equation of the line can found out using origin to P_2 .


Input:

input for parametric equation
input for parametric equation

 

Code:

/**
 * @author  Quickgrid ( Asif Ahmed )
 * @link    https://quickgrid.wordpress.com
 * Problem: Find parametric equations for the line through p1 and p2.
 *          Also for the segment joining these points.
 */

#include<stdio.h>

struct myVector{
    int x, y, z;
} v1, v2;

void input2dPoints(){
    printf("Enter p1:");
    scanf("%d %d", &v1.x, &v1.y);
    printf("Enter p2:");
    scanf("%d %d", &v2.x, &v2.y);
}

void input3dPoints(){
    printf("Enter p1:");
    scanf("%d %d %d", &v1.x, &v1.y, &v1.z);
    printf("Enter p2:");
    scanf("%d %d %d", &v2.x, &v2.y, &v2.z);
}

struct myVector calculateParallelVector(int choice){
    if(choice == 1)
        return {v2.x - v1.x, v2.y - v1.y};
    else{
        return {v2.x - v1.x, v2.y - v1.y, v2.z - v1.z};
    }
};

void printParametricAndVectorForm(int point, struct myVector mv, struct myVector parallelVector){
        printf("Taking point p%d(%d, %d) on the line L we get parametric equation:\n", point, mv.x, mv.y);
        printf("L: x = %d + %dt, y = %d + %dt\n",  mv.x, parallelVector.x, mv.y, parallelVector.y);
        printf("\n");
        printf("Vector form:\n");
        printf("<x, y> = <%d, %d> + t <%d, %d>\n",  mv.x, mv.y, parallelVector.x, parallelVector.y);
        printf("\n\n");
}

//@Override
void printParametricAndVectorForm(int point, struct myVector mv, struct myVector parallelVector, int dimension){
        printf("Taking point p%d(%d, %d, %d) on the line L we get parametric equation:\n", point, mv.x, mv.y, mv.z);
        printf("L: x = %d + %dt, y = %d + %dt, z = %d + %dt\n",  mv.x, parallelVector.x, mv.y, parallelVector.y, mv.z, parallelVector.z);
        printf("\n");
        printf("Vector form:\n");
        printf("<x, y, z> = <%d, %d, %d> + t <%d, %d, %d>\n",  mv.x, mv.y, mv.z, parallelVector.x, parallelVector.y, parallelVector.z);
        printf("\n\n");
}

int main(){
    int choice;
    struct myVector parallelVector;

    printf("1. Two Dimensional.\n");
    printf("2. Three Dimensional.\n");
    printf("\nChoice: ");
    scanf("%d", &choice);
    printf("\n");

    switch(choice){
    case 1:
        input2dPoints();

        printf("\n");
        parallelVector = calculateParallelVector(choice);
        printf("Parallel vector -> p1p2 to the line L is: <%d, %d>\n", parallelVector.x, parallelVector.y);
        printf("\n\n");

        printParametricAndVectorForm(1, v1, parallelVector);
        printParametricAndVectorForm(2, v2, parallelVector);

        break;

    case 2:
        input3dPoints();

        printf("\n");
        parallelVector = calculateParallelVector(choice);
        printf("Parallel vector -> p1p2 to the line L is: <%d, %d, %d>\n", parallelVector.x, parallelVector.y, parallelVector.z);
        printf("\n\n");

        printParametricAndVectorForm(1, v1, parallelVector, 3);
        printParametricAndVectorForm(2, v2, parallelVector, 3);

        break;

    default:
        printf("Wrong input.\n");
    }

    return 0;
}
Advertisements

One thought on “C++ Code for finding Parametric Equation and Vector form for Line L through two points p1 and p2

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s