[C++] Dimensionクラス
Posted by yama3 2008年 11月 11日. プログラミング 
C++による2次元座標(平面座標)を表現するクラスです。
追加的な関数としては、点と点の距離を求める関数くらいかな?
あんまり関数が思いつかなかったです。
それにしても、C++でのクラスファイルの作成って文法的なところが思ったよりも難しい。やっぱりPHPが簡単に思えてしまう。
また、いつものように変更したほうがいい点などは教えてください。
dimension.h
- #ifndef _MATH_H_INCLUDED
- #include <math.h>
- #endif
- class dimension {
- public:
- double x;
- double y;
- public:
- // コンストラクタ
- dimension() {
- this->x = 0;
- this->y = 0;
- }
- // Setter
- void Set( double px, double py ) {
- this->x = px;
- this->y = py;
- }
- void SetX( double px ) {
- this->x = px;
- }
- void SetY( double py ) {
- this->y = py;
- }
- // Getter
- void Get( double &x, double &y ) {
- x = this->x;
- y = this->y;
- }
- double GetX() {
- return this->x;
- }
- double GetY() {
- return this->y;
- }
- // 点と点の距離を求める。(指定された点との距離)
- double distance( dimension target_point ) {
- double a = this->x – target_point.x;
- double b = this->y – target_point.y;
- double c = sqrt( a*a + b*b );
- return c;
- }
- // 点と点の距離を求める。(原点との距離)
- double distance() {
- double a = sqrt( this->x * this->x + this->y * this->y );
- return a;
- }
- // デストラクタ
- ~dimension() {
- }
- };
- /*
- #include <stdio.h>
- int main() {
- dimension p1;
- dimension p2;
- double a, b;
- p1.Set( 4, 5 );
- p1.Get( a, b );
- p2.Set( a, b );
- printf( “Distance From Origine: %f\n”, p1.distance(p2) );
- return 0;
- }
- */
関連記事
[PR]
トラックバック
http://yamablo.com/2008/11/11-003903.php/trackback

