Home > OS >  How can I Modify this code to take user input?
How can I Modify this code to take user input?

Time:12-07

How to modify this code so the program can take user input instead of predefined input: the code is to check if two line are intersecting or not in this code the values of the points are given but i want the program to ask the values at the run time.

struct point
{
    lli x, y;
};

int orientation(point p1, point p2, point p3)
{
    int val = (p2.y - p1.y) * (p3.x - p2.x) -
              (p2.x -p1.x) * (p3.y -p2.y);

    if (val == 0) return 0;
    
    return (val > 0)? 1: 2;
    
}

bool validprojection(int a, int b, int c, int d)
{
    if (a > b)
       swap(a,b);
    if (c > d)
       swap(c, d);
    return max(a, c) <= min(b, d);
}

bool doIntersect(point a, point b, point c, point d)
{
    int o1 = orientation(a, b, c);
    int o2 = orientation(a, b, d);
    int o3 = orientation(c, d, a);
    int o4 = orientation(c, d, b);

    if(o1 != o2 && o3 != o4)
    return true;

    if (o1 == 0 && o4 == 0)
    {
        if(validprojection(a.x, b.x, c.x, d.x) && validprojection(a.y, b.y, c.y, d.y))
        return true;
    }
    
    return false;
}

predefined input:

how can i modify this part so it will ask user to input the values of point

int main()
{
    cout<<"To find the intersection point of two line segment";
    point p1 = {1, 1}, p2 = {10, 1}, p3 = {1, 2}, p4 = {10, 2};
    doIntersect(p1, p2, p3, p4)? cout << "yes\n": cout<< "No\n";

    p1 = {10, 0}, p2 = {0, 10}, p3 = {0, 0}, p4 = {10, 10};
    doIntersect(p1, p2, p3, p4)? cout << "yes\n": cout<< "No\n";

    return 0;

CodePudding user response:

how can i modify this part so it will ask user to input the values of point

You can do this by using operator overloading. In particular you can overload operator>> as shown below. The below shown program asks for input from user and use those point entered by the user to check if they intersect or not.

#include <iostream>

using namespace std;
struct point
{
    int x, y;
    
    //default constructor 
    point(): x(0), y(0)
    {
        
    }
    //overload operator>> 
    friend std::istream &operator>>(std::istream &is, point& inputPoint);
};

int orientation(point p1, point p2, point p3)
{
    int val = (p2.y - p1.y) * (p3.x - p2.x) -
              (p2.x -p1.x) * (p3.y -p2.y);

    if (val == 0) return 0;
    
    return (val > 0)? 1: 2;
    
}

bool validprojection(int a, int b, int c, int d)
{
    if (a > b)
       swap(a,b);
    if (c > d)
       swap(c, d);
    return max(a, c) <= min(b, d);
}

bool doIntersect(point a, point b, point c, point d)
{
    int o1 = orientation(a, b, c);
    int o2 = orientation(a, b, d);
    int o3 = orientation(c, d, a);
    int o4 = orientation(c, d, b);

    if(o1 != o2 && o3 != o4)
    return true;

    if (o1 == 0 && o4 == 0)
    {
        if(validprojection(a.x, b.x, c.x, d.x) && validprojection(a.y, b.y, c.y, d.y))
        return true;
    }
    
    return false;
}
//define overloaded operator>>
std::istream &operator>>(std::istream &is, point& inputPoint)
{
    std::cout<<"Enter x value: ";
    std::cin >> inputPoint.x ;
    std::cout<<"Enter y value: ";
    std::cin >> inputPoint.y;
    //check if input succeeded 
    if(cin)
    {
       //do something if needed 
       ;
    }
    else 
    {
        inputPoint = point();//otherwise leave the object in DEFAULT STATE
    }
    return is;
    
}
int main()
{
    cout<<"To find the intersection point of two line segment"<<std::endl;
    
    
    //TAKE INPUT(x and y values) FROM USER
    point p1; 
    std::cin >> p1;
    
    point p2; 
    std::cin >> p2;
    
    point p3; 
    std::cin >> p3;
    point p4; 
    std::cin >> p4;
    
    //check if intersect
    doIntersect(p1, p2, p3, p4)? cout << "yes\n": cout<< "No\n";

    
    //AGAIN TAKE INPUT FROM USER
    std::cin >> p1;
    std::cin >> p2;
    std::cin >> p3;
    std::cin >> p4;
    doIntersect(p1, p2, p3, p4)? cout << "yes\n": cout<< "No\n";

    return 0;
}

The output of the above program can be seen here.

CodePudding user response:

Code

#include <iostream>
#include <string>
#include <sstream> 

std::ostream &operator<<(std::ostream &os, Point &p) {
    return os << p.x << ',' << p.y;
}

int main() {
    Point points[2][2];
    std::string line;

    while (true) {
        for (int i = 0; i < 2; i  ) {
            for (int j = 0; j < 2; j  ) {
                std::cout << "Line " << i   1<< ", Point " << j   1 << " (x,y): "; // Prints: "Line i, Point j (x,y): "
                std::getline(std::cin, line);
                if (line.size() == 0) {
                    goto end;
                }

                std::stringstream ss(line);
                ss >> points[i][j].x;
                ss.ignore(1, ',');
                ss >> points[i][j].y;

                line.clear();
            }
        }

        bool intersect = doIntersect(points[0][0], points[0][1], points[1][0], points[1][1]);
        std::cout << "The lines 1 [(" << points[0][0] << "), (" << points[0][1] << ")] and 2 " << "[(" << points[1][0] << "), (" << points[1][1] << ")] " << (intersect ? "do" : "don't") << "intersect\n" << std::endl; // Double new line intended, Prints: "The lines 1 [(x,y), (x,y)] and 2 [(x,y), (x,y)] do/don't intersect"
    }

    end: return 0;
}

Explaination

  • std::ostream& operator<<.....: Make Point ostreamable (usable with cout).

  • Point points[2][2]: a 2 x 2 point array (2 lines, 2 points each)

  • string line: a string for user input

  • while (true): repeat the cycle of "Get input, Check if lines intersect" (remove it if you want the program to exit after completing the cycle once)

    The cycle:
    A. Get input:

    • 2 nested loops, one for lines, the other for points
    • Print Line i, Point j (x,y):
    • std::getline: Read till the user presses enter
    • If the input is empty, terminate the program
    • Read a number into point.x, ignore the ',' then read another number into point.y

    B. Check if lines intersect

    • Use the doIntersect and save the result into a variable
    • Print: The lines 1 [(x,y), (x,y)] and 2 [(x,y), (x,y)] do/don't intersect where (do/don't) depends on whether the result is true (lines intersect) or not
  •  Tags:  
  • c
  • Related