빙수달 게임 개발 노트

[C++] Circle2D 클래스 정의 본문

Programming/C++

[C++] Circle2D 클래스 정의

빙수달 2024. 12. 19. 00:38
//circle2D.h
#ifndef CIRCLE2D_H_
#define CIRCLE2D_H_
class Circle2D
{
private:
	double x;
	double y;
	double radius;
public:
	double get(double x, double y);	// double 데이터 필드와 get 상수 함수
	double get(double x, double y, double radius);	// double 데이터 필드의 radius와 get 상수 함수
	Circle2D();	// (x,y)가 (0,0), radius = 1인 기본원 생성 생성자
	Circle2D(double x, double y, double radius);
	double getArea(double radius);
	double getPerimeter(double radius);
	bool contains(double X, double Y);
	bool contains(Circle2D& circle);
	bool ooverlaps(const Circle2D& circle);
};

#endif
//circle2D.cpp
#include "circle2D.h"
#include<iostream>
using namespace std;

double Circle2D::get(double x, double y)
{
    cout << "원의 중심은 ("<<x<<","<<y<<")이다.\n";
    return 0;
}

double Circle2D::get(double x, double y, double radius)
{
    cout << "원의 중심은 (" << x << "," << y << ")이고, "<<"원의 반지름은 "<<radius<<"이다.\n";
    return 0;
}

Circle2D::Circle2D()
{
    x = 0;
    y = 0;
    radius = 1;
    cout << "(x,y)가" << "(" << x << "," << y << ")" << "이고 반지름이 " << radius << "인 원이 생성됐다.\n";
}

Circle2D::Circle2D(double x, double y, double radius)
{
    cout << "(x,y)가" << "(" << x << "," << y << ")" << "이고 반지름이 " << radius << "인 원이 생성됐다.\n";
}

double Circle2D::getArea(double radius)
{
    double area;
    area = radius*radius*3.14;
    return area;
}

double Circle2D::getPerimeter(double radius)
{
    double round_length;
    round_length = 2 * 3.14 * radius;
    return round_length;
}

bool Circle2D::contains(double X, double Y)
{
    int distance = sqrt((x-X)*(x-X) + (y-Y)*(y-Y));
    if(radius > distance)
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool Circle2D::contains(Circle2D& circle)
{
    double distance = sqrt((x - circle.x) * (x - circle.x) + (y - circle.y) * (y - circle.y));
    if(radius >= circle.radius + distance)
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool Circle2D::ooverlaps(const Circle2D& circle)
{
    double distance = sqrt((x - circle.x) * (x - circle.x) + (y - circle.y) * (y - circle.y));
    if(radius+circle.radius>=distance)
    {
        return true;
    }
    else
    {
        return false;
    }
}
//circle2D_main.cpp
#include "circle2D.h"
#include<iostream>

using namespace std;

int main()
{
	Circle2D circle1;		// 클래스 객체 circle1 생성, 동시에 생성자도 호출된다.
	circle1.get(4, 6);		// 원의 중심을 나타내는 (x,y)
	circle1.get(3, 5, 2);	// 원의 중심과 반지름을 나타내는 (x,y,radius) 
	Circle2D circle2(5, 7, 1);		// 클래스 객체2 생성, 동시에 인수를 가지고 있는 생성자도 호출된다. (x,y,radius)
	cout << circle1.getArea(3) << endl;		// 원의 반지름이 radius인 원의 넓이
	cout << circle1.getPerimeter(3) << endl;	// 원의 반지름이 radius인 원의 둘렉
	cout << circle1.contains(0.5, 0) << endl;		// 점contains(x,y)가 circle1 안에 있다면 1을 출력, 밖에 있으면 0 출력
	cout << circle1.contains(circle2) << endl;		// circle2가 circle1 내부에 있는지 판별
	cout << circle1.ooverlaps(circle2) << endl;		// circle2가 circle1과 겹치는지 판별

	return 0;
}