Question

This is C++ programming.

Create the following files: point.h – contains the definition for the point struct. A point consists of an x and y coordinate

0 0
Add a comment Improve this question Transcribed image text
Answer #1

program code to copy

main.cpp

#include "line.h";
#include <iostream>
using namespace std;

int main()
{
        //test slope 1
        point p1;
        p1.x = 0;
        p1.y = 1;
        point p2;
        p2.x = 1;
        p2.y = 3;
        line l1(p1,p2);
        cout << "Slope should be 2:" << endl;
        cout <<"Slope: "<< l1.slope()<<endl;

        //test slope 2
        point p3;
        p3.x = 0;
        p3.y = 1;
        point p4;
        p4.x = 0;
        p4.y = 4;
        line l2(p3, p4);
        cout << "Slope should be NaN:" << endl;
        cout << "Slope: " << l2.slope() << endl;

        //test slope 3
        point p5;
        p5.x = 2;
        p5.y = 2;
        point p6;
        p6.x = 4;
        p6.y = 2;
        line l3(p5, p6);
        cout << "Slope should be 0:" << endl;
        cout << "Slope: " << l3.slope() << endl;

        //test slope 4
        point p7;
        p7.x = 2;
        p7.y = 3;
        point p8;
        p8.x = 4;
        p8.y = 1;
        line l4(p7, p8);
        cout << "Slope should be -1:" << endl;
        cout << "Slope: " << l4.slope() << endl;

        //test for intersect_with() 1
        p1.x = 0;
        p1.y = 1;
        p2.x = 1;
        p2.y = 3;
        line l5(p1, p2);

        p3.x = 0;
        p3.y = 4;
        p4.x = 2;
        p4.y = 0;
        line l6(p3, p4);
        cout << "Intersection of l5( (" << p1.x << " , " << p1.y << " )"  << " , (" << p2.x << " , " << p2.y << ") ) and " << "l6( (" << p3.x << ", " << p3.y << ")" << ", (" << p4.x << ", " << p4.y << ") )" << endl;
        cout << "( "<<l5.intersect_with(l5, l6).x << " , " << l5.intersect_with(l5, l6).y << " )" << endl;


        //test for intersect_with() 2
        p1.x = 0;
        p1.y = 1;
        p2.x = 3;
        p2.y = 3;
        line l7(p1, p2);

        p3.x = 0;
        p3.y = 4;
        p4.x = 0;
        p4.y = 0;
        line l8(p3, p4);
        cout << "Intersection of l7( (" << p1.x << " , " << p1.y << ")" << " , (" << p2.x << " , " << p2.y << ") ) and " << "l8( (" << p3.x << ", " << p3.y << ")" << ", (" << p4.x << ", " << p4.y << ") )" << endl;
        cout <<"Intersection point: "<< "(" << l7.intersect_with(l7, l8).x << " , " << l7.intersect_with(l7, l8).y << ")" << endl;


        //test for intersect_with() 2
        p1.x = 0;
        p1.y = 1;
        p2.x = 2;
        p2.y = 5;
        line l9(p1, p2);

        p3.x = 1;
        p3.y = 0;
        p4.x = 2;
        p4.y = 2;
        line l10(p3, p4);
        cout << "Intersection of l9( (" << p1.x << " , " << p1.y << ")" << " , (" << p2.x << " , " << p2.y << ") ) and " << "l10( (" << p3.x << " , " << p3.y << ")" << " , (" << p4.x << " , " << p4.y << ") )" << endl;
        cout <<"Intersection point: "<< "(" << l9.intersect_with(l9, l10).x << " , " << l9.intersect_with(l9, l10).y << ")" << endl;

}

===============================================================================================

line.h

#include "point.h";
class line 
{
        private:
                point p1;
                point p2;
        public:
                line(struct point p1,struct point p2);
                //parameters: none
                //return: slope of the two points
                const double slope();
                //parameters: line l1, line l2
                //return: point of intersection between these two lines
                const point intersect_with(line l1,line l2);
};

=======================================================================

line.cpp

#include <iostream>
#include <cmath>
#include "line.h";
using namespace std;

line::line(const struct point p1,const struct point p2) 
{
        this->p1 = p1;
        this->p2 = p2; 
}
const double line::slope() 
{
        if (this->p1.x == this->p2.x) return std::nan("");
        return ((this->p2.y - this->p1.y) / (this->p2.x - this->p1.x));
}


const point line::intersect_with(line l1, line l2) 
{
        if (isnan(l1.slope()) && isnan(l2.slope()))
        {
                point pq;
                pq.x = std::numeric_limits<double>::quiet_NaN();
                pq.y = std::numeric_limits<double>::quiet_NaN();
                return pq;
        }
        else if (isnan(l1.slope()) || isnan(l2.slope())) 
        {
                        if (isnan(l1.slope())) 
                        {
                                cout << "First line is vertical!" << endl;
                                double b2 = l2.p1.y - (l2.slope() * l2.p1.x);
                                point pi;
                                pi.x = l1.p1.x;
                                pi.y = l2.slope() * pi.x + b2;
                                return pi;
                        }
                        else 
                        {
                                cout << "Secont line is vertical!" << endl;
                                double b1 = l1.p1.y - (l1.slope() * l1.p1.x);
                                point pi;
                                pi.x = l2.p1.x;
                                pi.y = l1.slope() * pi.x + b1;
                                return pi;
                        }
        }
        else if (l1.slope() == l2.slope()) 
        {
                point pi;
                pi.x = std::nan("");
                pi.y = std::nan("");
                return pi;
        }
        else 
        {
                double b1 = l1.p1.y - (l1.slope() * l1.p1.x);
                double b2 = l2.p1.y - (l2.slope() * l2.p1.x);
                point pi;
                pi.x = (b1 - b2) / (l2.slope() - l1.slope());
                pi.y = l2.slope() * pi.x + b2;
                return pi;
        }

}


============================================================================

point.h

struct point 
{
        double x;
        double y; 
};

sample output

slope should be 2: slope: 2 slope should be NaN: slope: nan slope should be 0: slope: 0 slope should be -1: slope: -1 Interse

Add a comment
Know the answer?
Add Answer to:
This is C++ programming. Create the following files: point.h – contains the definition for the point...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that asks the user the slope and y-intersect of one line y =...

    Write a program that asks the user the slope and y-intersect of one line y = a_1x + b_1, the slope and y-intersect of a second line y = a_2x + b_2. The program returns the point of intersection of the two lines, if it exists. Otherwise, the program must print a message to indicate that the two lines do not have a point of intersection. Recall that, if it exists, the point of intersection (x_o, y_o) of two lines...

  • Write a class called Point that contains two doubles that represent its x- and y-coordinates. It...

    Write a class called Point that contains two doubles that represent its x- and y-coordinates. It should have get and set methods for both fields. It should have a constructor that takes two double parameters and initializes its coordinates with those values. It should have a default constructor that initializes both coordinates to zero. It should also contain a method called distanceTo that takes as a parameter another Point and returns the distance from the Point that was passed as...

  • In Java programming language. For this assignment, you must write a class Rectangle and a tester...

    In Java programming language. For this assignment, you must write a class Rectangle and a tester RectangleTest. The Rectangle class should have only the following public methods (you can add other non-public methods): Write a constructor that creates a rectangle using the x, y coordinates of its lower left corner, its width and its height in that order. Creating a rectangle with non-positive width or height should not be allowed, although x and y are allowed to be negative. Write...

  • calculate the molar compositions of the feed, distillate and residue calculate the minimum reflux (use the...

    calculate the molar compositions of the feed, distillate and residue calculate the minimum reflux (use the equation 21.47 of the textbook) A benzene and toluene solution containing 45% benzene, at atmospheric pressure, at a flow of 25000 kg/h, is continuously fractional. The distilled product will contain 95% by weight of benzene and residue 3% benzene. The feed enters the tower as saturated liquid; the reflux is going to come back at its bubble point. Equation 21.47 CHAPTER 21: Distillation 689...

  • Each of these problems (Problems 1-4) is worth four points Definition: Two lines or curves are said to be normal to each other at their point of intersection if they intersect there at right ang...

    Each of these problems (Problems 1-4) is worth four points Definition: Two lines or curves are said to be normal to each other at their point of intersection if they intersect there at right angles or, equivalently, if their tangent lines at the point of intersection are 1. A well-known theorem in geometry states that a line which is tangent to a circle is perpendicular to the radius of the circle at the point of tangency. Use implicit differentiation to...

  • On a single set of axes, sketch a picture of the graphs of the following four...

    On a single set of axes, sketch a picture of the graphs of the following four equations: y = -x + V2. y = -x - v2, y = x + V2, and y = x-V2. These equations determine lines, which in turn bound a diemand Sheped region in the plane. (a) At how many points does the line y = -x + 2 intersect the unit circle? At how many points does the line y = -x-vī intersect the...

  • If a company decreases both the variable expense per unit and total fixed expenses, while keeping...

    If a company decreases both the variable expense per unit and total fixed expenses, while keeping selling price the same, which of the following is NOT true? A. The point at which the total expenses and total sales lines intersect will shift rightward B. The loss area will become smaller C. The total expense line will have a smaller y-intercept D. The total expense line will have a flatter slope

  • EXERCISES X- axis through the middle of a 10 x 10 grid. The point where the...

    EXERCISES X- axis through the middle of a 10 x 10 grid. The point where the 1. Draw an X-Y Y lines intersect is known as the "origin" and is defined as the X an both X and Y are equal to zero. Draw each of the following lines alues of X from -5 to 5 and write the corresponding regression equation: (a) y-intercept 2, slope 2 (b) y-intercept =-2, slope-2; (c) y-intercept 0, slope1; (d) y-intercept 2, slope-2 2....

  • 11. We will prove the following statement by mathematical induction: Let 1,2tn be n2 2 distinct l...

    11. We will prove the following statement by mathematical induction: Let 1,2tn be n2 2 distinct lines in the plane, no two of which are parallel Then all these lines have a point in common 1. For2 the statement is true, since any 2 nonparallel lines intersect 2. Let the statement hold forno, and let us have nno 1 inesn as in the statement. By the inductive hypothesis, all these lines but the last one (i.e. the nes 1,2.n-1) have...

  • Using separate files, write the definition for a class called Point and a class called Circle. Th...

    Using separate files, write the definition for a class called Point and a class called Circle. The class point has the data members x (double) and y (double) for the x and y coordinates. The class has the following member functions: a) void set_x(double) to set the x data member b) void set_y(double) to set the y data member c) double get_x() to return the the x data member d) double get_y() to return the the y data member e)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT