Question: Add a new function to this programme which checks if a given point is on (contained in) a given line segment. #include #include using namespace

Add a new function to this programme which checks if a given point is on (contained in) a given line segment.

#include  #include  using namespace std;
struct Point { double x; double y; }; struct LineSeg { Point s; Point e; };
double segLength(LineSeg l) { return sqrt( pow(l.s.x - l.e.x, 2) + pow(l.s.y - l.e.y, 2) ); }
int main() { Point p1 = { 1, 1 }; Point p2 = { 2, 2 }; LineSeg line = { p1, p2 }; cout << segLength(line) << endl; } 

Add a new function to this programme which checks if a given point is on (contained in) a given line segment. Your function should have the following form:

bool contains(LineSeg l, Point p) { ... }

It should return true if p is contained in l and return false otherwise. Your code should function correctly when called using the following test code:

int main() { Point p1 = { 1, 1 }; Point p2 = { 2, 2 }; LineSeg line = { p1, p2 }; Point p3 = { 1.5, 1.5 }; cout << std::boolalpha << contains(line, p3) << endl; Point p4 = { 1.6, 1.6 }; cout << std::boolalpha << contains(line, p4); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!