Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*****MUST BE IN PYTHON***** Implement a class Point that represents a point by s

ID: 3569991 • Letter: #

Question

*****MUST BE IN PYTHON*****

Implement a class Point that represents a point by storing a name and the x and y coordinates of the point. Include a method distance, so that if p1 and p2 are points, then p1.dist(p2) returns the distance between the points. Implement a base class Shape that stores just a name and has methods

1. pt_is_in (a point)
2. area()
3. translate(vx,vy)

4. show_data()

In the Shape class, these methods should just return a message that the methods should be implemented by derived classes. The show data() method should just return the name of the object.

Implement a subclass of Shape called Circle that specifies a circle by accept- ing the center (a Point) and the radius. The class Circle should implement the methods above for a circle. The translate method should translate the whole circle by the vector (vx, vy) (i.e., a point (x, y) is translated to (x + vx, y + vy)). The show data() method should give the name and coordinates of the center point and the radius.

Implement similar classes for line segments and rectangles. The rectangles have their sides parallel to the coordinate axes and are specified by specifying two corners.

Create your own shape and design a class for it.

Test that all arguments make geometric sense and print error messages if they do not. Implement _ _str_ _ and

_ _repr_ _ methods for all classes.

Explanation / Answer


#include "stdafx.h"
#include<iostream>
#include<math.h>
#include<string>
using namespace std;

class Point
{
public:

   string name;
   float x;
   float y;

   Point(){}

   Point(float x1 , float y1)
   {
       x=x1;
       y=y1;
   }

   float dist(Point p2)
   {
       return (float)sqrt(((x - p2.x)*(x - p2.x))+((y - p2.y)*(y - p2.y)));
   }
};

class Shape
{
public:
  
   string name;

   void pt_is_in(Point a);
   virtual float area() = 0;
   virtual void translate(float vx,float vy) = 0;

   public:

   void virtual show_data()
   {
       std::cout<<name;
   }
};

class Circle : public Shape
{
public:

   Point center;
   float radius;

   Circle(Point c, float r)
   {
       center.x = c.x;
       center.y = c.y;

       radius = r;

       name = "circle";
   }

   float area()
   {
       return (22/7)*radius*radius;
   }

   void translate(float vx, float vy)
   {
       center.x += vx;
       center.y += vy;
   }

   void show_data()
   {
       std::cout<<" "<<name;

       cout<<" Center: ("<<center.x<<","<<center.y<<")";
       cout<<" Radius: "<<radius<<" ";
   }

};

class Rectangle : Shape
{
public:

   Point corner1;
   Point corner2;

   Rectangle(Point c1, Point c2)
   {
       if((c1.x == c2.x) || (c1.y == c2.y))
           cout<<" Not Rectangle ";

       corner1.x = c1.x;
       corner1.y = c1.y;

       corner2.x = c2.x;
       corner2.y = c2.y;

       name = "rectangle";
   }

   float area()
   {
       float a = Point(corner1.x, corner2.y).dist(corner2);

       float b = Point(corner2.x, corner1.y).dist(corner1);

       return a*b;
   }

   void translate(float vx, float vy)
   {
       corner1.x += vx;
       corner1.y += vy;

       corner2.x += vx;
       corner2.y += vy;
   }

   void show_data()
   {
       std::cout<<" "<<name;

       cout<<" Corner1: ("<<corner1.x<<","<<corner1.y<<")";
       cout<<" Corner2: ("<<corner1.x<<","<<corner2.y<<")";
       cout<<" Corner3: ("<<corner2.x<<","<<corner1.y<<")";
       cout<<" Corner4: ("<<corner2.x<<","<<corner2.y<<")";
   }
};

class Line : Shape
{
public:

   Point p1;
   Point p2;
   float slope;
   float intercept;

   Line(Point c1, Point c2)
   {
       if((c1.x == c2.x) && (c1.y == c2.y))
           cout<<" Not a line ";

       p1.x = c1.x;
       p1.y = c1.y;

       p2.x = c2.x;
       p2.y = c2.y;

       name = "Line";

       slope = (p1.y - p2.y)/(p1.x - p2.x);

       intercept = p1.y - (slope)*(p1.x);
   }

   void show_data()
   {
       std::cout<<" "<<name;

       cout<<" Slope: "<<slope<<" and intercept: "<<intercept;
       cout<<" Line formula: y="<<slope<<"*x + "<<intercept;
      
   }

   float area()
   {
       cout<<" cannot be determined ";

       return -1;
   }

   void translate(float vx, float vy)
   {
       p1.x += vx;
       p1.y += vy;

       p2.x += vx;
       p2.y += vy;
   }

};

void main()
{
   Circle obj = Circle(Point(2,3), 7);

   obj.show_data();
   cout<<" Area: "<<obj.area();


   Rectangle ob = Rectangle(Point(1, 1), Point(3, 3));
   ob.show_data();
   cout<<" Area: "<<ob.area();

   Line l2 = Line(Point(1, 1), Point(3, 3));
   l2.show_data();
   cout

}