Question

In: Computer Science

This is the header file: #ifndef __HW2_H__ #define __HW2_H__ // Previous two lines are the start...

This is the header file:

#ifndef __HW2_H__
#define __HW2_H__
// Previous two lines are the start of the marco guard

// Try not to change this file

#include <iostream>
#include <cmath>

using std::cout;
using std::endl;
using std::istream;
using std::ostream;

class Point {
private:
   double x, y, z;
public:
   // Constructors
   Point();
   Point(double inX, double inY, double inZ = 0);
   Point(const Point& inPt);

   // Get Functions
   double getX() const;
   double getY() const;
   double getZ() const;

   // Set Functions
   void setX(double inX);
   void setY(double inY);
   void setZ(double inZ);
   void setXY(double inX, double inY);
   void setXYZ(double inX, double inY, double inZ);

   // Member Functions
   void print();
   double distance();
   double distance(const Point& pt2) const;
   bool origin();
   bool line(Point pt2);
   Point cross(Point pt2);

   // Friend functions
   friend istream& operator>>(istream& ins, Point& target);
   friend ostream& operator<<(ostream& outs, const Point& source);
};

// Overloading Functions
Point operator+(const Point& pt1, const Point& pt2);
Point operator-(const Point& pt1, const Point& pt2);

// Non-Member Functions
bool plane(const Point pts[], const Point& target);
bool square(const Point pts[], const size_t size);
Point centroid(const Point pts[], const size_t size);

// Following line is the end of the marco guard
#endif

This is the implementation file I wrote so far:

#include "hw2.h"
#include<iostream>
#include<cassert>
#include<cmath>
#include<cstdio>
#include<cstdlib>
// Put your functions/prototype here

using namespace std;

Point::Point(){
   x=0; y=0; z=0;
}
Point::Point(double inX, double inY, double inZ=0){
   x=inX;
   y=inY;
   z=inZ;
}
Point::Point(const Point& inPt){
   x=inPt.x;
   y=inPt.y;
   z=inPt.z;
}
void Point::setX(double inX){ x=inX}
void Point::setY(double inY){y=inY}
void Point::setZ(double inZ){y=inZ}
void Point::setXY(double inX, double inY){
   x=inX;
   y=inY;
}
void Point::setXYZ(double inX, double inY, double inZ){
   x=inX;
   y=inY;
   z=inZ;
}
double Point::getX(){return x;}
double Point::getY(){return y;}
double Point::getZ(){return z;}

void Point::print(){
   cout<<"("<<getX()<<","<<getY()<<","<<getZ()<<")"<<endl;
}
double Point::distance(){
   return sqrt((x*x)+(y*y)+(z*z));
}
double Point::distance(const Point& pt2) const{
   return sqrt((x-pt2.x)*(x-pt2.x)+(y-pt2.y)*(y-pt2.y)+(z-pt2.z)*(z-pt2.z));
}
bool Point::origin(){
   if(x==0&&y==0&&z==0)
       return true;
   else
       return false;
}
bool Point::line(Point pt2){

I need help with question 11,12,13, and 14 from the following:

Name your implementation file as LastName(3 to 5 letters)_FirstNameInitial_HW1.cpp
Note: You can only use iostream, cassert, cmath, cstdio, and cstdlib.
Create a class called Point with the following:
1. Private members: x, y and z as double.
2. Write a default constructor and set the point to (0, 0, 0).
3. Write a constructor and set the point to user input, if z is not inputted, set it to 0.
4. Write a copy constructor.
5. Write the set functions for each individual member, x and y together, and x, y, and z
together.
6. Write the get functions for each individual member.
7. Write a member function called print() that print out the point as (x, y, z)\n.
8. Write a member function called distance() that return the distance between the origin and
the point.
9. Write a member function called distance(param) that return the distance between a pt2
and the point.
10. Write a member function called origin() that return true if the point is the origin.
11. Write a member function called line(param) that return true if pt2 is on the same line as
the origin and the point. Otherwise, return false. Also return false if the origin and the
point do NOT make a line.
12. Write a member function called cross(param) that return a point that is the cross product
of a pt2 and the point.
13. Overload the addition (+) and the subtraction (-) operators.
14. Overload the output (<<) and input (>>) operators. The output should be (x, y, z)\n.

15. Write a non-member function called plane(param) that takes an array of exactly three
points and a target point. Return true if the target point is on the plane created by the
static array of three points. Otherwise, return false.
To find the plane
i. Find u and v where u is pt2-pt1 and v is pt3-pt1.
ii. Find the normal vector to the plane by using the cross product of u and v.
iii. Using the Point-Normal Form, the normal vector, and any of the three
points, the equation of the plane is a(x-x 0 ) + b(y-y 0 ) + (cz-z 0 ) = 0, where
<a, b, c> is the normal vector and P(x 0 , y 0 , z 0 ) is one of the three points.
Thus, any points P(x, y, z) that satisfy this equation is on the plane.
16. Write a non-member function called square(param) that takes a static array of unique
points and the size of the array. Return true if the array of points can create at least one
square. Otherwise return false.
17. Write a non-member function called centroid(param) that takes a static array of points
and the size of the array and return the centroid of the array of points. If there is no
center, return the origins.

Solutions

Expert Solution

Q11 Answer

bool Point::line(Point pt2){
if(sqrt((x*x)+(y*y)+(z*z))+sqrt((x-pt2.x)*(x-pt2.x)+(y-pt2.y)*(y-pt2.y)+(z-pt2.z)*(z-pt2.z))==sqrt((pt2.x*pt2.x)+(pt2.y*pt2.y)+(pt2.z*pt2.z)))
{
return true;
}
else if(sqrt((pt2.x*pt2.x)+(pt2.y*pt2.y)+(pt2.z*pt2.z))+sqrt((x-pt2.x)*(x-pt2.x)+(y-pt2.y)*(y-pt2.y)+(z-pt2.z)*(z-pt2.z))==sqrt((x*x)+(y*y)+(z*z)))
{
return true;
}
else if(sqrt((x*x)+(y*y)+(z*z))+sqrt((pt2.x*pt2.x)+(pt2.y*pt2.y)+(pt2.z*pt2.z))+(z-pt2.z)*(z-pt2.z))==sqrt((x-pt2.x)*(x-pt2.x)+(y-pt2.y)*(y-pt2.y)+(z-pt2.z)*(z-pt2.z)))
{
return true;
}
else
{
return false;
}
}

Q12 Explanation

Cross product of two points can be defined as determinant value of the 2X2 matrix by hiding one variable at a time from both the points

p1=x y z

pt2=pt2.x pt2.y pt2.z

so p3.x=y*pt2.z-z*pt2.y;(determinant value after hiding x from both points)

p3.y=x*pt2.z-z*pt2.x;(determinant value after hiding y from both points)

p3.z=x*pt2.y-y*pt2.x;(determinant value after hiding z from both points)

Q12 Answer

Point cross(Point pt2)

{Point p3;

p3.x=y*pt2.z-z*pt2.y;

p3.y=x*pt2.z-z*pt2.x;

p3.z=x*pt2.y-y*pt2.x;

return p3;

}

Q13 Answer

Point operator +(const Point &pt1,const Point &pt2)

{

Point p3;

p3.x=pt1.x+pt2.x;

p3.y=pt1.y+pt2.y;

p3.z=pt1.z+pt2.z;

return p3;

}

Point operator -(const Point &pt1,const Point &pt2)

{

Point p3;

p3.x=pt1.x-pt2.x;

p3.y=pt1.y-pt2.y;

p3.z=pt1.z-pt2.z;

return p3;

}

Q14 Answer

friend istream& operator >>(istream& ins,Point &target)

{

cout<<"Enter x,y,z";

ins>>target.x;

ins>>target.y;

ins>>target.z;

return ins;

}

friend ostream& operator <<(ostream& outs, const Point& source)

{

outs<<"("<<source.x<<","<<source.y<<","<<source.z<<")"<<endl;

return outs;

}


Related Solutions

Use the functions.h header file with your program (please write in C code): #ifndef FUNCTIONS_H #define...
Use the functions.h header file with your program (please write in C code): #ifndef FUNCTIONS_H #define FUNCTIONS_H typedef struct MyStruct { int value; char name[ 100 ]; } MyStruct; void sortArray( MyStruct*, int ); void printArray( MyStruct*, int ); #endif Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like...
I create a h file, and I wrote #ifndef MYSTACK_H #define MYSTACK_H #include <cstdlib> #include <cstddef>...
I create a h file, and I wrote #ifndef MYSTACK_H #define MYSTACK_H #include <cstdlib> #include <cstddef> #include <iostream> struct node { int value; node* next; node(int value, node* next = nullptr) { this->value = value; this->next = next; } }; class mystack { private: node* stack_top; size_t stack_size; public: mystack(); mystack(const mystack& x); ~mystack(); mystack& operator=(const mystack& x); size_t size() const; bool empty() const; void clear(); const int& top() const; void push(int value); void pop(); void clone(const mystack& x); };...
list.h file #ifndef LIST_H_ #define LIST_H_ struct ListNode { long value; struct ListNode *next; }; struct...
list.h file #ifndef LIST_H_ #define LIST_H_ struct ListNode { long value; struct ListNode *next; }; struct ListNode *list_prepend(struct ListNode *list, long value); int list_length(struct ListNode *list); struct ListNode *list_remove(struct ListNode *list, long value); #endif /* LIST_H_ */ given.c file #include #include "list.h" struct ListNode *list_prepend(struct ListNode *list, long value) { struct ListNode *node = malloc(sizeof(struct ListNode)); node->value = value; node->next = list; return node; } list.c file #include #include "list.h" /* Counts and returns the number of nodes in the...
using the header: #include <pthread.h> // This is a header file for a Read/Right Lock Library....
using the header: #include <pthread.h> // This is a header file for a Read/Right Lock Library. Your C code //SHOULD access your routines using these exact function // prototypes typedef struct RW_lock_s { } RW_lock_t; void RW_lock_init(RW_lock_t *lock); /* This routine should be called on a pointer to a struct variable of RW_lock_t to initialize it and ready it for use. */ void RW_read_lock(RW_lock_t *lock); /* This routine should be called at the beginning of a READER critical section */...
Use the provided BingoBall.h and Set.h files to implement the Set.cpp file. //File: BingoBall.h #ifndef BINGOBALL_H...
Use the provided BingoBall.h and Set.h files to implement the Set.cpp file. //File: BingoBall.h #ifndef BINGOBALL_H #define   BINGOBALL_H #include <iostream> class BingoBall { public:    BingoBall():letter{'a'}, number{0} {}    BingoBall(char let, int num) :        letter{ let }, number{ num } {}    char getChar() const { return letter; }    int getNumber() const { return number; }    //overload == operator    bool operator==(BingoBall &b) const { return (number == b.getNumber() && letter == b.getChar()); } private:   ...
Study the file polygon.h. It contains the header file for a class of regular polygons. Implement...
Study the file polygon.h. It contains the header file for a class of regular polygons. Implement the methods, and provide a driver to test it. It should be in C++ polygon.h file- #ifndef POLY_RVC_H #define POLY_RVC_H #include <iostream> using namespace std; class Polygon { public:    Polygon();    Polygon(int n, double l);    //accessors - all accessors should be declared "const"    // usually, accessors are also inline functions    int getSides() const { return sides; }    double getLength()...
Study the file polygon.h. It contains the header file for a class of regular polygons. Implement...
Study the file polygon.h. It contains the header file for a class of regular polygons. Implement the methods, and provide a driver to test it. It should be in C++ Write a polygon.h file with given instructions for the polygon.cpp file #include <iostream> #include <math.h> using namespace std; #ifndef M_PI # define M_PI 3.14159265358979323846 #endif int main() {    float areaP, length, sides;    cout << "\n\n Polygon area program.\n";    cout << "---------------------------------\n";    cout << " Enter the...
A header file contains a class template, and in that class there is a C++ string...
A header file contains a class template, and in that class there is a C++ string object. Group of answer choices(Pick one) 1)There should be a #include for the string library AND a using namespace std; in the header file. 2)There should be a #include for the string library. 3)There should be a #include for the string library AND a using namespace std; in the main program's CPP file, written before the H file's include.
Write a C++ program that design a class definition to be put in a header file...
Write a C++ program that design a class definition to be put in a header file called fizzjazz.h A store sells online FizzJazz which are sound tones made by famous bands. For each FizzJazz, the store wants to keep track of the following attributes: * title - the name of the sound tone * band - Famous band name that created the tone * duration - this is in seconds and may be fractional: examples: 20.0, 34.5 Each attribute will...
Write a C++ program that design a class definition to be put in a header file...
Write a C++ program that design a class definition to be put in a header file called fizzjazz.h A store sells online FizzJazz which are sound tones made by famous bands. For each FizzJazz, the store wants to keep track of the following attributes: * title - the name of the sound tone * band - Famous band name that created the tone * duration - this is in seconds and may be fractional: examples: 20.0, 34.5 Each attribute will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT