Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You will create a class to represent a Line. An object of the Line class has two points as its state variables. You can name

You will create a class to represent a Line. An object of the Line class has two points as its state variables. You can name them something like Point a and Point b (or one and two etc.). A line is infinite going in either direction past point a and point b.

Requirements for Line (

1a) a .h header file and .cpp implementation file named Line (Line.h, Line.cpp) with Pre and Post Conditions for each member function clearly written in the .h file. Both the header and .cpp should include Point.h

https://drive.google.com/drive/folders/1Tbt2yO2GRVC4ZYeyrh6arjqRYSQQeHcV

here is the point.h and the main file

1b) All member functions that should not change the calling object should be const. Remember const correctness, you cannot call a function that is not const from a const function (i.e. do not try to call a non-const point function from a const line function). All parameters that dont change should be passed as const. All constant objects passed into member functions should be passed as const&. Remember why we do this (const means it cannot be changed, & means you do not make a copy of it which leads to memory efficiency)

1c) The line object must have at least 2 state variables two points.

2) One value constructor that takes care of the case when no points are passed in, 1 point is passed in, and 2 points are passed in. You can use the following form in the function prototype (in Line.h) to create default values for a point (it invokes the Point constructor.

Line(Point userPoint1= Point(0,0), Point userPoint2=Point(0,0));

This will, by default, set both Points of the Line to 0,0. You do not want this. Instead the following behavior is required

-CASE 1: If no points are passed into the line constructor, the line is on the x-axis with points (0,0) and (1,0).

-CASE 2: If one point is passed into the line constructor, lets call it point A, the line goes through (0,0) and point A. For example if point A is (3,3) then the line will have one point at (0,0) and one point at (3,3).

-CASE 3: If two points are passed into the line constructor, then the line goes through those two points.

-Case 4: NOTE if the SAME point is passed into the line constructor, the line should instead default to CASE 2, as if one valid point had been passed in.

3) Copy Constructor: Initializes a Line Object to be a copy of the line passed in (remember, should be passed in as a const&). Remember to use Points operator equals.

4a) Getter for the first and second point (this is just to make the testing script easier). get_first and get_second. This should return a POINT.

4) Slope: returns the slope of the line as a double. Remember, slope is rise over run, or change in y between the two points over the change in x between the two points. For example a Line with points (0,0) (1,2) has a slope of 2. A line with points (0,0) (1,-1) has a slope of -1. Dont worry if it divides by zero, inf will automatically be returned (inf is a valid double value in C++)

5) Translate: add a fixed pair of numbers (x,y) to the points of a given line. For example, if Line A goes from (0,0) to (1,1) translating it by (1.5,1.7) will make A go from (1.5,1.7) to (2.5,2.7). Remember to use Points translate to accomplish this.

6,7) Reflect: reflect_x and reflect_y reflect the points over the x axis and y axis respectively. So if we reflect a Line (0,0) (1,1) over the x_axis the line will now be at (0,0) (1,-1) and over the y axis it will be (0,0)(-1,1). Remember to use Points reflect_x and reflect_y functions to do this.

8)Rotation: You can rotate a line, about the origin counter clockwise, by rotating both of its points a given amount in radians r. Imagine you have a Line with points (0,0) (1,0). Rotating it 90 degrees ccw (or pi/2 radians) will make the line have points (0,0) (0,1). Basically the horizontal line is now vertical. If you have the line (6,3) and (1,-9) and you rotate it 90 degrees your new line will be (-3,6) (9,1). You can see a demonstration of what that means here. Remember you can just rotate your two points.

9) Operator = : Assignment operator Allows you to set two Lines equal to each other. Note the assignment operator is like the copy constructor but is invoked at a different time. The copy constructor is invoked when creating a Line or a Point COPY CONSTRUCTOR: Line x=y; Or Line x(y); OPERATOR EQUALS: Lets say Line x and c have already been created and we say x=c. this will invoke the assignment operator (operator =)

10a) Member function, you will put this in the private section of your class. Y intercept, y_intecept, returns the y-intercept of the Line. This can be calculated by using the formula derived on this webpage. All you need to calculate the y-intercept is the y-value when x is 0. So first you need to have an equation for the line thats easy because the equation is at the top of the page. x1 and y1 are the x and y value of ANY point on the line. The slope is m, which you can get by calling slope() (you can call it directly by just saying slope() inside of the class, it assumes you are calling it on the object). Solving for y with your point substituted for (x1,y1) and 0 substituted for x and the slope substituted for m in that top equation of the the above webpage will tell you the y-value where the line will intercept the y-axis.

10b) Operator == : Checks if two lines are equal. Two lines are equal if their Slope and Y Intercept are equal (i.e. the points of the two lines may not be equal but as long as the slope and y-intercept are equal, the lines are equal.

11) Friend function to Overload cin this is the same as cinning two points. You can invoke your overloaded cin operator that you already programmed for point on each point in line (remember, you can do this directly because cin is a friend). This will allow the user to enter 4 values x1,y1 and x2,y2> or 2 points. However, if the user types in the same point you should make one of those points (0,0) like you did in the value constructor above!

12) Friend function to Overload cout the format for coutting a line is as follows

If the line has a normal (i.e. non-zero, non-infinite) slope y = mx + b i.e. if its the line (0,0) (0,1) coutting the line would yield y = 1*x + 0. if its the line (0,-1) (1,0) the line would yield y = 1*x + -1 (looks wierd but you are adding a negative 1, so the math works out). If its the line (0,1) (1,0) coutting the line would yield y = -1*x +1. etc.

If the line has an infinite slope (vertical line, all ys have the same x) x = i.e. For the line represented by (5,0) (5,10) the equation would be x = 5. For the line represented by (0,0) (0,20) the equation would be x = 0; For the line represented by (-5,0) (-5, 10) the equation would be x = -5.

If the line has a slope of 0 (horizontal line, all xs have the same y) y = i.e. for the line represented by (0,5) (10,5) the equation would be y = 5. For the line represented by (0,0) (0,10) the equation would be y = 0. For the line represented by (0,-5) (10,-5) the equation would be y = -5.

Remember you can invoke the private function yIntercept from cout because its a friend and so it can see the private variables and the private functions!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle 10g Database Administrator Implementation And Administration

Authors: Gavin Powell, Carol McCullough Dieter

2nd Edition

1418836656, 9781418836658

More Books

Students also viewed these Databases questions