Design and write class definition for class Circle,Square,Triangle which are derived from a class Shape.Define a function intersect () that takes two shape* arguments and calls a suitable member function to determine if two shape of same class overlap. Make sure to add suitable declaration of member functions to the classes to achieve this
#include<iostream>
#include<string>
#include<math.h>
using namespace std;
class Shape
{
private:
//arguments and calls a suitable member function to
//determine if two shape of same class overlap.
int x;
int y;
public:
//Constructor
Shape(){}
//Constructor
Shape(int x,int y){
this->x=x;
this->y=y;
}
//a function intersect () that takes two shape*
virtual bool intersect(Shape shape1,Shape shape2){
return true;
}
int getX(){
return x;
}
int getY(){
return y;
}
};
class Circle: public Shape
{
private:
int radius;
public:
//Constructor
Circle(){}
//Constructor
Circle(int x,int y,int radius):Shape(x,y){
this->radius=radius;
}
bool intersect(Circle circle1,Circle circle2){
int distSq = (circle1.getX() - circle2.getX()) * (circle1.getX() -circle2.getX()) +
(circle1.getY() - circle2.getY()) * (circle1.getY() - circle2.getY());
int radiusSumSq = (circle1.getRadius() + circle2.getRadius()) * (circle1.getRadius() + circle2.getRadius());
if (distSq > radiusSumSq){
return false;
}
return true;
}
int getRadius(){
return radius;
}
};
class Square: public Shape
{
private:
int side;
public:
//Constructor
Square(){}
//Constructor
Square(int x,int y,int side):Shape(x,y){
this->side=side;
}
bool intersect(Square square1,Square square2){
if (square1.getX() > (square2.getX()+square2.getSide()) ||
square2.getX() > (square1.getX()+square1.getSide())) {
return false;
}
if (square1.getY() > (square2.getY()+square2.getSide()) ||
square2.getY() > (square1.getY()+square1.getSide())) {
return false;
}
return true;
}
int getSide(){
return side;
}
};
class Triangle: public Shape
{
private:
int x2;
int y2;
int x3;
int y3;
public:
//Constructor
Triangle(){}
//Constructor
Triangle(int x1,int y1,int x2,int y2,int x3,int y3):Shape(x1,y1){
this->x2=x2;
this->y2=y2;
this->x3=x3;
this->y3=y3;
}
double calculateArea(int x1, int y1, int x2, int y2,int x3, int y3)
{
return abs((x1*(y2-y3) + x2*(y3-y1)+x3*(y1-y2))/2.0);
}
bool pointLiesInsideTriangle(int x1, int y1, int x2,int y2, int x3, int y3, int x, int y)
{
double ABC = calculateArea (x1, y1, x2, y2, x3, y3);
double PBC = calculateArea (x, y, x2, y2, x3, y3);
double PAC = calculateArea (x1, y1, x, y, x3, y3);
double PAB = calculateArea (x1, y1, x2, y2, x, y);
return (ABC == PBC + PAC + PAB);
}
bool intersect(Triangle triangle1,Triangle triangle2){
//check the first point of the triangle
if(pointLiesInsideTriangle(triangle1.getX(),triangle1.getY(),triangle1.getX2(),triangle1.getY2(),
triangle1.getX3(),triangle1.getY3(),triangle2.getX(),triangle2.getY())){
return true;
}
//check the second point of the triangle
if(pointLiesInsideTriangle(triangle1.getX(),triangle1.getY(),triangle1.getX2(),triangle1.getY2(),
triangle1.getX3(),triangle1.getY3(),triangle2.getX2(),triangle2.getY2())){
return true;
}
//check the third point of the triangle
if(pointLiesInsideTriangle(triangle1.getX(),triangle1.getY(),triangle1.getX2(),triangle1.getY2(),
triangle1.getX3(),triangle1.getY3(),triangle2.getX3(),triangle2.getY3())){
return true;
}
return true;
}
int getX2(){
return x2;
}
int getY2(){
return y2;
}
int getX3(){
return x3;
}
int getY3(){
return y3;
}
};
int main(){
Circle circle1(5,5,10);
Circle circle2(8,6,10);
Square square1(10,10,30);
Square square2(0,4,7);
Triangle triangle1(3,3,5,6,8,9);
Triangle triangle2(7,6,5,6,8,10);
if(circle1.intersect(circle1,circle2)){
cout<<"Two circles overlap\n";
}else{
cout<<"Two circles do not overlap\n";
}
if(square1.intersect(square1,square2)){
cout<<"Two squares overlap\n";
}else{
cout<<"Two squares do not overlap\n";
}
if(triangle1.intersect(triangle1,triangle2)){
cout<<"Two triangles overlap\n";
}else{
cout<<"Two triangles do not overlap\n";
}
system("pause");
return 0;
}
Comments
Leave a comment