//Would someone please write me a program for this assignment. I need it in a co
ID: 3687270 • Letter: #
Question
//Would someone please write me a program for this assignment. I need it in a couple hours. I really really need help. Thank you!
Program 19 "Line Class"
Implement a Line class that uses two points which represent a line.
Given:
Design and implement a classed called Line that has the following fields and methods.
[edit]Fields
Point p1, p2;
[edit]Methods
Line()
default constructor - initialize points to (0,0) and (1,1)
Line(Point a, Point b)
constructor - set p1 and p2 to Points a and b respectively.
void setFirstPoint(Point a)
set p1 to a
void setSecondPoint(Point a)
set p2 to a
Point getFirstPoint() const
return a copy of p1
Point getSecondPoint() const
return a copy of p2
bool slope(double& m) const
return true is there is a slope, false otherwise
store the value of the slope in m
bool yIntercept(double& b) const
return true is there is a y-intercept, false otherwise
store the value of the y-intercept in b
bool isParallel(Line) const
return true is the lines are parallel, false otherwise
bool isCollinear(Line) const
return true is the lines are collinear, false otherwise
bool isPerpendicular(Line) const
return true is the lines are perpendicular, false otherwise
Point intersect(Line) const
return the Point where the two lines intersect
precondition: the lines intersect
void display(ostream&) const
prints the equation of the line (in slope intercept form) to the output stream
[edit]Deliverables
Line.h
Line.cpp
Explanation / Answer
struct Point{
double x;
double y;
Point(double a = 0, double b = 0){
x = a;
y = b;
}
void display(ostream& out){
out << "(" << x << "," << y << ")";
}
};
void setFirstPoint((a.x - b.x)*(a.x - b.x))
{
}
void setSecondPoint ((a.y - b.y)*(a.y - b.y))
{}
double length(Point a,Point b){
return (a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y);
}
bool points(Point p, Point a, Point b){
return abs(length(a,p) + length(p,b) - length(a,b)) < eps;
}
bool intersect(Point a, Point b, Point p, Point q){
//slopes of the line segments,
Point u(b.x-a.x,b.y-a.y);
Point v(q.x-p.x,q.y-p.y);
// for parallelism
if (abs(u.x*v.y - u.y*v.x) < eps){
if (points(a,p,q) || points(b,p,q) || points(p,a,b) || points(q,a,b))
return true;
else
return false;
}
//for intersection between line
// x = a.x +s*u.x x = p.x+t*v.x
// y = a.y +s*u.y y = p.y+t*v.y
//this below is just a solution for equation of 4 unknown variables x,y would
//be coordinates of the point of an intersection, but we don't need them.
double s,t;
if (abs(u.y) < eps){
s = (a.y - p.y ) / v.y;
t = ( p.x + s * v.x - a.x ) / u.x;
}
else if (abs(u.x) < eps){
s = (a.x - p.x) / v.x;
t = ( p.y + s * v.y - a.y ) / u.y;
}
else {
s = ( p.y * ( u.x / u.y ) - a.y * ( u.x / u.y ) - p.x + a.x ) / ( v.x - (v.y * u.x ) / u.y );
t = ( p.x + s * v.x - a.x ) / u.x;
}
// the value of s and t will be between 0 and 1 by parametric representation
if(s <=1+eps && s+eps >= 0 && t <=1+eps && t +eps >= 0 ) return true;
else return false;
// if point q lies on line segment 'p'
bool onSegment(Point p, Point q)
{
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;
else
return false;
}
}
int main() {
cout << "Example" ;
if (intersect(Point(-11,1),Point(-1,1),Point(-2,4),Point(-4,2))) cout << "True" << endl;
else cout << "False" ;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.