The goal of this project is to 1 Understand the principle of
The goal of this project is to:
1. Understand the principle of abstraction and encapsulation and use them to implement classes.
2. Overload various operators and become aware of their restrictions.
3. Highlights the peculiarities of classes with pointer data members and how to avoid them.
4. Create and work with dynamic two-dimensional arrays.
Problem:
The equation of a line in standard form is ax + by = c, wherein both a and b cannot be zero, and a, b, and c are real numbers. If b 0, then –a / b is the slope of the line. If a = 0, then it is a horizontal line, and if b = 0, then it is a vertical line. The slope of a vertical line is undefined. Two lines are parallel if they have the same slope or both are vertical lines. Two lines are perpendicular if either one of the lines is horizontal and the other is vertical or the product of their slopes is -1. Design the class lineType to store a line. To store a line, you need to store the values of a (coefficient of x), b (coefficient of y), and c. Your class must contain the following operations:
a. If a line is nonvertical, then determine its slope.
b. If two lines are not parallel, then find the point of intersection.
c. Overloads the stream insertion operator, <<, for easy output.
d. Overloads the stream extraction operator, >>, for easy intput. (The line ax + by = c is input as (a, b, c).)
e. Overloads the assignment operator to copy a line into another line.
f. Overloads the unary operator +, as a member function, so that it returns true if a line is vertical; false otherwise.
g. Overloads the unary operator -, as a member function, so that it returns true if a line is horizontal; false otherwise.
h. Overloads the operator ==, as a member function, so that it returns true if two lines are equal; false otherwise.
i. Overloads the operator ||, as a member function, so that it returns true if two lines are parallel; false otherwise.
j. Overloads the operator &&, as a member function, so that it returns true if two lines are perpendicular; false otherwise.
Add appropriate constructors to initialize variables of lineType. Also write a program to test your class
Note: Please do not post the code here. I would really apreciate it if you could email me the code at: jemo_nemo4@yahoo.com
Thank You.
Solution
#include <iostream>
using namespace std;
struct Point
{
int x;
int y;
};
bool onSegment(Point p, Point q, Point r)
{
if (q.x <= max(p.x, r.x) && q.x >= min(p.x, r.x) &&
q.y <= max(p.y, r.y) && q.y >= min(p.y, r.y))
return true;
return false;
}
int orientation(Point p, Point q, Point r)
{
int val = (q.y - p.y) * (r.x - q.x) -
(q.x - p.x) * (r.y - q.y);
if (val == 0) return 0; // colinear
return (val > 0)? 1: 2; // clock or counterclock wise
}
bool doIntrsct(Point pt1, Point q1, Point pt2, Point q2)
{
int o1 = orientation(pt1, q1, pt2);
int o2 = orientation(pt1, q1, q2);
int o3 = orientation(pt2, q2, pt1);
int o4 = orientation(pt2, q2, q1);
if (o1 != o2 && o3 != o4)
return true;
if (o1 == 0 && onSegment(pt1, pt2, q1)) return true;
if (o2 == 0 && onSegment(pt1, q2, q1)) return true;
if (o3 == 0 && onSegment(pt2, pt1, q2)) return true;
if (o4 == 0 && onSegment(pt2, q1, q2)) return true;
return false;
}
int main()
{
struct Point pt1 = {1, 1}, q1 = {10, 1};
struct Point pt2 = {1, 2}, q2 = {10, 2};
doIntrsct(pt1, q1, pt2, q2)? cout << \"Yes\ \": cout << \"No\ \";
pt1 = {10, 0}, q1 = {0, 10};
pt2 = {0, 0}, q2 = {10, 10};
doIntrsct(pt1, q1, pt2, q2)? cout << \"Yes\ \": cout << \"No\ \";
pt1 = {-5, -5}, q1 = {0, 0};
pt2 = {1, 1}, q2 = {10, 10};
doIntrsct(pt1, q1, pt2, q2)? cout << \"Yes\ \": cout << \"No\ \";
return 0;
}

