Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how to write this program

+2 votes
asked Feb 2, 2019 by anonymous 1 flag
  • In this specific case (2D space), all vectors exist on the same plane. Hence, a vector v is defined by its x- and y-coordinates and cross-product can mathematically computed as follows:
  • v1 x v2 = v1.x * v2.y – v1.y * v2.x

Where V1.x represents the x coordinate of vector V1 and V1.y represents the y coordinate of vector V1.  Similar meaning for V2.x and V2.y.

  • Vector multiplication is defined as follows:
  • v1 * v2 = v1.x * v2.x + v1.y * v2.y
  • The values of t and u in the parametric equations of the line segments can be computed as follows:
  • t = q x s / r x s
  • u = q x r / r x s

where the operator x indicates the cross-product operation as defined above. q, s, and r are auxiliary vectors whose x- and y-coordinates are computed as follows:

  • r = (x2 – x1, y2 – y1)    represents the vector P1P2
  • s = (x4 – x3, y4 – y3)    represents the vector P3P4
  • q = (x3 – x1, y3 – y1)   represents the vector P3P1

Algorithm

  • Read x1, y1, x2, y2
  • Read x3, y3, x4, y4
  • Compute vectors r, s, and q
  • Compute cross-products qxs, qxr, and rxs
  • Compute auxiliary values: q*r, r*r, q*s, and s*s
  • If rxs is zero and qxr is zero, then
    • The two lines are collinear
      • If 0 <= q*r <= r*r or 0 <= q*s <= s*s
        • The two lines are overlapping
      • else
        • The two lines are disjoint
  • If rxs is zero and qxr is not zero
    • The two lines are parallel and not intersecting
  • If rxs is not zero , compute t and u
    • If   0 <= t <= 1 and 0 <= u <= 1
      • The two lines intersect at P1+t*r
    • else
      • The two line segments do not intersect

1 Answer

0 votes
answered Feb 13, 2019 by Shiro
Pretty simple implementation, hope it helps!
#include <iostream>

using namespace std;

int crossProduct(int* v1, int* v2){
    int product = v1[1]*v2[2] - v1[2]*v2[1];
    return product;
}

int pointProduct(int* v1, int* v2){
    int product = v1[1]*v2[1] + v1[2]*v2[2];
    return product;
}

int main()
{
    //Variables inputs
    int x1,x2,x3,x4,y1,y2,y3,y4,t,u;
    cout << "Insert x1 ";
    cin >> x1;
    cout << "Insert y1 ";
    cin >> y1;
    cout << "Insert x2 ";
    cin >> x2;
    cout << "Insert y2 ";
    cin >> y2;
    
    cout << "Insert x3 ";
    cin >> x3;
    cout << "Insert y3 ";
    cin >> y3;
    cout << "Insert x4 ";
    cin >> x4;
    cout << "Insert y4 ";
    cin >> y4;
    
    
    //Array assignments
    int r[2],s[2],q[2];
    r[1] = x2 - x1;
    r[2] = y2 - y1;
    cout << "r: " << r[1] << ","<< r[2] << endl;
    s[1] = x4 - x3;
    s[2] = y4 - y3;
    cout << "s: " << s[1] << "," << s[2] << endl;
    q[1] = x3 - x1;
    q[2] = y3 - y1;
    cout << "q: " << q[1] << "," << q[2] << endl;
    cout << "Assignments done\n";
    
    //CrossProduct
    int qsProd = crossProduct(q,s);
    cout << "q x s: " << qsProd << endl;
    int qrProd = crossProduct(q,r);
    cout << "q x s " << qrProd << endl;
    int rsProd = crossProduct(r,s);
    cout << "r x s " << rsProd << endl;
    
    if(rsProd != 0){
        t = qsProd/rsProd;
        u = qrProd/rsProd;
    }
    else{
        cerr << "Cannot divide by zero!";
        exit(1);
    }
    
    //PointProduct
    int qrPoint = pointProduct(q,r);
    int rrPoint = pointProduct(r,r);
    int qsPoint = pointProduct(q,s);
    int ssPoint = pointProduct(s,s);
    
    //Conditions PrintOut
    if(rsProd == 0 && qrProd == 0)
        cout << "Lines are Collinear!\n";
    if((rrPoint >= qrPoint && qrPoint >= 0) || (ssPoint >= qsPoint >= 0))
        cout << "Lines are Overlapping!\n";
    else
        cout << "lines are Disjoint!\n";

    if(rsProd == 0 && qrProd != 0)
        cout << "The two lines are parallel and not intersecting\n";
    if(rsProd != 0){
        if((1 >= t && t >= 0) && (1 >= u && u >= 0))
            cout << "The two lines intersect at P1+t*r\n";
        else
            cout << "The two line segments do not intersect\n";
    }
        
}   

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...