Notice
Recent Posts
Recent Comments
Link
빙수달 게임 개발 노트
[C++] Circle2D 클래스 정의 본문
//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;
}

'Programming > C++' 카테고리의 다른 글
| [C++] 클래스 멤버 함수 이용하여 점 이동 (0) | 2024.12.19 |
|---|---|
| [C++] 예금주 설정, 예금 입.출금 함수, 잔액표시 함수 (1) | 2024.12.19 |
| [C++] 학교 사물함 열기 (0) | 2024.12.19 |
| [C++] 두 선의 교차점을 구하는 프로그램 (0) | 2024.12.19 |
| [C++] 점이 원 안에 있는지 판별하는 프로그램 (0) | 2024.12.19 |