Answer to Question #20998 in C++ for maram

Question #20998
Write a program to find geometry intersection. Suppose two lines segments intersect. The two endpoints for the first line are (x1, y1) and (x2 , y2) and for the second line segment are ( x3, y3) and (x4, y4). Write a program that prompts the user to enter four endpoints and display the intersecting points.
Expert's answer
#include <iostream>
#include <utility>

using namespace std;

float A1(float x1, float x2)
return float(1/(x2-x1));

float B1(float y2, float y1)
return float(-(1/(y2-y1)));

float C1(float x1, float x2,float y2, float y1)
return float(-(x1*A1(x1, x2))+ y1*(-B1(y2, y1)));

float A2(float x3, float x4)
return float(1/(x4-x3));

float B2(float y4, float y3)
return float(-(1/(y4-y3)));

float C2(float x3, float x4,float y4, float y3)
return float(-(x3*A2(x3, x4))+ y3*(-B2(y4, y3)));

float Intersection(float x1,float x2,float x3,float y1,float y2,float y3,float x4,float y4)
pair<float , float > intersec;
intersec.first = float(-(C1(x1,x2,y2,y1)*B2(y4, y3)-C2(x3, x4, y4, y3)*B1(y2, y1))/(A1(x1, x2)*B2(y4, y3)-(A2(x3, x4)*B1(y2, y1))));
intersec.second = float(-(A1(x1, x2)-A2(x3, x4)*C1(x1, x2, y2, y1))/(A1(x1, x2)*B2(y4, y3)-A2(x3, x4)*B1(y2, y1)));

cout << intersec.first<<" quot;<<intersec.second<<endl;

int main(int argc, const char * argv[])
float x1, x2, x3, y1, y2, y3, x4, y4;


cout << C2(x3, x4, y4, y3);
return 0;

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!


No comments. Be the first!

Leave a comment

Ask Your question

New on Blog