FACE TCS Ninja - Coding_6
TEST CODE : TCS Ninja -
Coding_6
Total number of question : 1
Test duration (min) : 20 min
Correct attempt (mark) : NA
Wrong attempt (mark) : NA
CODING
Given two lines, write Program for finding its intersection point
#include <stdio.h>
int main()
{
float x1, y1, x2, y2;
scanf("%f,%f\n%f,%f", &x1, &y1, &x2, &y2);
float x3,y3,x4,y4;
scanf("%f,%f\n%f,%f", &x3, &y3, &x4, &y4);
float m1 = (y2-y1)/(x2-x1);
float m2 = (y4-y3)/(x4-x3);
// Line 1
// y = y1 + m * (x - x1);
// y = y1 + m * (x - x1);
// y = m1*x + y1 - m1 * x1
// Line 2
// y = y1 + m * (x - x1);
// y = y3 + m2 * (x - x3);
// y = m2*x + y3 - m2 * x3
// Solving line 1 and line 2 eqn
float x_intersection = (y1 - m1 * x1 - y3 + m2 * x3)/(m2-m1);
float y_intersection = y1 + m1 * (x_intersection - x1);
printf("%0.2f,%0.2f", x_intersection, y_intersection);
return 0;
}
______________________________________________________________________________________________________
Focus Academy for Career Enhancement Page 1 of 1