Please please help me this. I want to update the point class and create a cluste
ID: 669314 • Letter: P
Question
Please please help me this. I want to update the point class and create a cluster class in the following 3D code. And I want to make it more universal.
The requirements for the Point class are as follows:
Points should be able to have an arbitrary number of dimensions of doubles. That is, (1, 2), (1, 2, 4), (2, 3, ..., 5, 6), etc. You need to:
Add a private dim member.
Create at least one constructor which takes an int for the dimension and an array of doubles. Of course,
the array has to be at least as big as the dimension value. You will have to decide if it makes sense to
have a default constructor and what initialization it should do.
Take care of all consequences of this change, including updating the distanceTo() function.
Since the dimension could be potentially very large, you have to allocate it dynamically, and take care of
all cleanup. Memory leaks will be penalized.
Remember what dynamic allocation means: overload the "big three".
Point objects should be comparable. You need to:
Overload the comparison operators: ==, !=, <, >, <=, and >=. Hint: reuse the implementation of == for !=,
and so on. This makes the implementation more consistent, easy to ready, and less error prone.
Assume a lexicographic order, that is, (3, 4, 5) > (1, 2, 3) > (1, 2, 3) > (1, 1, 3) > (1, 1, 2), etc. In other
words, compare the first dimension, then the second, and so on.
Point arithmetic should be allowed. You need to:
Overload the arithmetic operators: +, . Addition and subtraction should be dimensionwise, that is, (1, 2, 3) + (4, 5, 6) gives (5, 7, 9). Similar with subtraction.
Create factorwise multiplication and division: *, /. That is, if p1 is a point (1, 2, 3), then p1*4 should give a point (4, 8, 12). Similar for division. (Remember to disallow division by zero.) Factors should be doubles.
Overload the compound assignment operators according to the rules in (1) and (2) above: +=, =, *=, and /=.
Cluster class
The requirements for the Cluster class are as follows:
The Cluster should be able to have an arbitrary number of Points. You have to:
Use a typedef statement to create an alias for the type Point * (that is, pointer to Point). Call it PointPtr.
Make sure it is global in your namespace.
Create a private nested structure type Node to support a singlylinked list. Hint: It only needs two
members!
Create a private singlylinked list member of the Cluster class to hold the Points. Hint: This is just a
pointer to the head.
Optional: You could, if you want, create an alias to the Node * type (that is, pointer to Node), and use
appropriately. If you do so, make sure it is visible where it is used.
Create a size member to keep track of how many Points you have in the Cluster.
A linked list is dynamically allocated by default: overload the big three.
Implement a default constructor, if it makes sense. Hint: A Cluster can remain empty and/or unused.
The linked list naturally supports an order of the elements. You have to:
1. Keep the Points contained in the Cluster in the lexicographic order mentioned in the requirements for the
Point class. Hint: Use the Point comparison operators when adding a Point.
3. Since Clusters contain PointPtrs, the corresponding points can be treated as a set, even if there are points
that are equal to each other. (Hint: Two points (1, 2, 3) and (1, 2, 3) will have different addresses.) You have to:
1. Overload operator+ to represent the UNION of two Clusters.
2. Overload operator to represent the INTERSECTION of two Clusters. Hint: This may be empty.
Overload operator==. It will return true iff (this means if and only if) two Clusters contain exactly the same points (that is, PointPtrs). Hint: The ordering might come useful here.
Overload the compound assignment operators accordingly.
4. Clusters should be able to to add and remove Points. You should:
Implement the add() and remove() functions to be able to add and remove PointPtr. Hint: See the signatures in the arraybased definition of the Cluster class from the lectures.
Overload operator+ to allow a statement like this: c1 = c1 + p1, where c1 is a Cluster and p1 is a Point. Hint: How similar or different is this to add()?
Overload operator to allow a statement like this: c2 = c2 p2, where c2 is a Cluster and p2 is a Point. Hint: How similar or different is this to remove()?
Overload the compound operators accordingly.
// Point.h
// A 3-dimensional point class!
// Coordinates are double-precision floating point.
#ifndef POINT_H
#define POINT_H
// A 3-dimentional point class
class Point {
private:
// Coordinates
double x_coordinate;
double y_coordinate;
double z_coordinate;
public:
// Constructors
Point(); // default constructor
Point(double x, double y, double z); // three-argument constructor
// Destructor
~Point();
// Mutator methods
void setX(double newX);
void setY(double newY);
void setZ(double newZ);
// Accessor methods
double getX();
double getY();
double getZ();
// distanceTo
double distanceTo(Point &nextPoint);
};
#endif // __point_h
// Point.cpp
#include "Point.h"
#include <cmath>
// Default constructor
// Initializes the point to (0.0, 0.0, 0.0)
Point::Point() {
x_coordinate = 0.0;
y_coordinate = 0.0;
z_coordinate = 0.0;
}
// Constructor
// Initializes the point to (initX, initY, initZ)
Point::Point(double initX, double initY, double initZ) {
x_coordinate = initX;
y_coordinate = initY;
z_coordinate = initZ;
}
// Destructor
// No dynamic allocation, so nothing to do; if omitted, generated automatically
Point::~Point() {
// no-op
}
// Mutator methods
// Change the values of private member variables
void Point::setX(double newX) {
x_coordinate = newX;
}
void Point::setY(double newY) {
y_coordinate = newY;
}
void Point::setZ(double newZ) {
z_coordinate = newZ;
}
// Accessors
// Return the current values of private member variables
double Point::getX() {
return x_coordinate;
}
double Point::getY() {
return y_coordinate;
}
double Point::getZ() {
return z_coordinate;
}
// DistanceTo function
double Point::distanceTo(Point &nextPoint) {
return sqrt ((x_coordinate - nextPoint.x_coordinate)*(x_coordinate - nextPoint.x_coordinate) +
(y_coordinate - nextPoint.y_coordinate)*(y_coordinate - nextPoint.y_coordinate) +
(z_coordinate - nextPoint.z_coordinate)*(z_coordinate - nextPoint.z_coordinate));
}
// main.cpp
#include "Point.h"
#include <cmath>
#include <iostream>
using namespace std;
// Function for calculating the area of a 3D triangle
double computeArea(Point &a, Point &b, Point &c){
// Calculate half perimeter s= (a+b+c)/2
double s = (a.distanceTo(b) + b.distanceTo(c) + c.distanceTo(a))/2.0;
// calculate area
return sqrt (s* (s - a.distanceTo(b)) * (s - b.distanceTo(c)) * (s -c.distanceTo(a)));
}
int main() {
double x, y, z; // Declared three coordinator variables
// Enter three x, y, and z coordinators for first point A.
cout << "Please enter the x, y, and z coordinates of the first point: " << endl;
cin >> x >> y >> z;
// First point
Point first = Point (x, y, z);
// Enter three x, y, and z coordinators for second point B.
cout << "Please enter the x, y, and z coordinates of the second point: " << endl;
cin >> x >> y >> z;
// Second point
Point second = Point (x, y, z);
// Enter three x, y, and z coordinators for third point C.
cout << "Please enter the x, y, and z coordinates of the third point: " << endl;
cin >> x >> y >> z;
// First point
Point third = Point (x, y, z);
// The area of the triangle
double area = computeArea(first, second, third);
// Print the total area
cout << "The area is: " << area << endl;
return 0;
}
Explanation / Answer
#include <bits/stdc++.h>
using namespace std;
class Point{
public:
double[] co_ordinate;
int dimension;
Point(int dim,double[] arr){
co_ordinate = new[dim];
for(int i = 0; i < dim; i++)
co_ordinate[i] = arr[i];
}
double distanceTo(Point* nextPoint){
int dim = min(nextPoint->dimension,dimension);
double distance = 0.0;
for (int i = 0; i < dim; i++)
distance += pow(nextPoint->co_ordinate[i] - co_ordinate[i],2);
int rem = abs(nextPoint->dimension-dimension);
if (nextPoint->dimension > dimension){
for (int i = dim; i < dim+rem; i++)
distance += pow(nextPoint->co_ordinate[dim],2);
}
else{
for (int i = dim; i < dim+rem; i++)
distance += pow(co_ordinate[dim],2);
}
return dis;
}
Point* addition(Point* nextPoint){
Point* p;
int dim = max(nextPoint->dimension,dimension);
double[] temp_arr = new double[dim];
for (int i = 0; i < dim; i++){
if (i < nextPoint->dimension)
temp_arr[i] += nextPoint->co_ordinate[i];
if (i < dimension);
temp_arr[i] += co_ordinate[i];
}
p = new Point(dim,temp_arr);
return p;
}
Point* subtraction(Point* nextPoint){
Point* p;
int dim = max(nextPoint->dimension,dimension);
double[] temp_arr = new double[dim];
for (int i = 0; i < dim; i++){
if (i < nextPoint->dimension)
temp_arr[i] -= nextPoint->co_ordinate[i];
if (i < dimension);
temp_arr[i] += co_ordinate[i];
}
p = new Point(dim,temp_arr);
return p;
}
Point* multiplication(int factor){
Point* p;
double[] temp_arr = new double[dimension];
for (int i = 0; i < dimension; i++){
temp_arr[i] = co_ordinate[i]*factor;
}
p = new Point(dimension,temp_arr);
return p;
}
Point* division(int factor){
Point* p;
double[] temp_arr = new double[dimension];
for (int i = 0; i < dimension; i++){
temp_arr[i] = co_ordinate[i]/factor;
}
p = new Point(dimension,temp_arr);
return p;
}
bool operator==(Point* nextPoint){
if (dimension == nextPoint->dimension){
for (int i = 0; i < dimension; i++){
if (co_ordinate[i] != nextPoint->co_ordinate[i])
return false;
}
return true;
}
return false;
}
bool operator!=(Point* nextPoint){
if (dimension == nextPoint->dimension){
for (int i = 0; i < dimension; i++){
if (co_ordinate[i] == nextPoint->co_ordinate[i])
return false;
}
return true;
}
return true;
}
bool operator<(Point* nextPoint){
if (dimension == nextPoint->dimension){
for (int i = 0; i < dimension; i++){
if (co_ordinate[i] >= nextPoint->co_ordinate[i])
return false;
}
return true;
}
return false;
}
bool operator<(Point* nextPoint){
if (dimension == nextPoint->dimension){
for (int i = 0; i < dimension; i++){
if (co_ordinate[i] <= nextPoint->co_ordinate[i])
return false;
}
return true;
}
return false;
}
bool operator<=(Point* nextPoint){
if (dimension == nextPoint->dimension){
for (int i = 0; i < dimension; i++){
if (co_ordinate[i] > nextPoint->co_ordinate[i])
return false;
}
return true;
}
return false;
}
bool operator<(Point* nextPoint){
if (dimension == nextPoint->dimension){
for (int i = 0; i < dimension; i++){
if (co_ordinate[i] < nextPoint->co_ordinate[i])
return false;
}
return true;
}
return false;
}
};
class cluster{
public:
int no_of_points;
vector<Point*> my_vec;
cluster(){
no_of_points = 0;
}
cluster* operator+(Point* p){
no_of_points++;
my_vec.push_back(p);
cluster *c = new cluster();
c->no_of_points = no_of_points;
c->my_vec = my_vec;
return c;
}
void operator-(Point* p){
no_of_points--;
my_vec.pop_back(p);
cluster *c = new cluster();
c->no_of_points = no_of_points;
c->my_vec = my_vec;
return c;
}
bool operator==(cluster* c){
if (c->no_of_points == no_of_points){
for (int i = 0; i < no_of_points; i++){
if (my_vec[i] != c->my_vec[i])
return false;
}
return true;
}
return false;
}
bool operator!=(cluster* c){
if (c->no_of_points == no_of_points){
for (int i = 0; i < no_of_points; i++){
if (my_vec[i] == c->my_vec[i])
return false;
}
return true;
}
return true;
}
};
int main(){
return 0;
}