Question: Design a Triangle class which models the fact that 3 points determine a triangle. (Consider reusing your Point and Line class from lab 10a.) Write
Design a Triangle class which models the fact that 3 points determine a triangle. (Consider reusing your Point and Line class from lab 10a.)
Write a main program that instantiates a vector of 20 triangles with randomly selected integer vertices in the range of [-10, 10]. Display the triangles along with their corresponding perimeter and area. (No user input)
This is the code that its refering to use
#include
class Point {//requires cmath
private:
double x, y;
public:
Point(double argx = 0, double argy = 0);
void setX(double argx);
void setY(double argy);
double getX(void);
double getY(void);
void displayPoint(void);
double distanceFromOrgin(void);
}; class Line {
private:
Point a, b;
public:
Line(Point argA = { 0, 0 }, Point argB = { 0, 0 });
void setA(Point arg);
void setB(Point arg);
Point getA(void);
Point getB(void);
void displayLine(void);
void displayEquation(void);
}; int main(void) {
Point myPoint, yourPoint;
//set myPoint
myPoint.setX(2);
myPoint.setY(3);
yourPoint.setX(6);
yourPoint.setY(4);
myPoint.displayPoint();
yourPoint.distanceFromOrgin();
yourPoint.displayPoint();
Line myLine(myPoint, yourPoint);
myLine.displayEquation();
myLine.displayLine();
//now test one more set of points(1,6) and (2,3)
myPoint.setX(1);
myPoint.setY(6);
yourPoint.setX(3);
yourPoint.setY(2);
myPoint.displayPoint();
yourPoint.distanceFromOrgin();
yourPoint.displayPoint();
Line myLine1(myPoint, yourPoint);
myLine1.displayEquation();
myLine1.displayLine(); system("pause"); return(0);
} Point::Point(double argx, double argy) {
x = argx;
y = argy;
}
void Point::displayPoint(void) {
cout << "(" << getX() << ")" << "(" << getY() << ")" << endl;
}
void Point::setX(double argx) {
x = argx;
}
void Point::setY(double argy) {
y = argy;
}
double Point::getX(void) {
return (x);
}
double Point::getY(void) {
return (y);
}
double Point::distanceFromOrgin(void) {
return(sqrt(getX()*getX() + getY() + getY()));
}
Line::Line(Point argA, Point argB) {
setA(argA);
setB(argB);
}
void Line::displayLine(void) {
cout << "The points are";
getA().displayPoint();
getB().displayPoint();
}
void Line::setA(Point arg) {
a = arg;
}
void Line::setB(Point arg) {
b = arg;
}
Point Line::getA(void) {
return(a);
}
Point Line::getB(void) {
return(b);
}
void Line::displayEquation()
{
//first find slope of the line using formula m= (y2-y1)/(x2-x1)
float constant;
string Y;
float y, x;
y = (b.getY() - a.getY());
x = (b.getX() - a.getX());
float m = y / x;
//after finding slope now find equation by y-y1 = m ( x-x1)
constant = -m*a.getX() + a.getY();
//first push y = into string Y
Y.push_back('y');
Y.push_back('=');
std::ostringstream buff;
buff << m;
//now function slope value m which is float value
for (int i = 0; i < buff.str().length(); i++)
Y.push_back(buff.str()[i]);
Y.push_back('x');
if (constant > 0)
Y.push_back('+');
else
Y.push_back('-');
//now push constant
std::ostringstream buf;
buf << constant;
//now function string value for constant
for (int i = 0; i < buf.str().length(); i++)
Y.push_back(buf.str()[i]);
cout << Y << endl;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
