2017-04-17 42 views
0

我也碰到這個棘手的問題來到這裏,所以基本上我要求寫應該返回一個指向一個數字如果點我點擊就在於人物和空值的函數沒有更好的替代方案如果這一點不在任何數字。有沒有這種情況[向下轉換]

CFigure *ApplicationManager::GetFigure(int x, int y) const 
{ 
    //If a figure is found return a pointer to it. 
    //if this point (x,y) does not belong to any figure return NULL 
    int c = 0; 
    for (size_t i = 0; i < FigCount; i++) 
    { 
     if (dynamic_cast<CRectangle*> (FigList[i])) 
     { 
      CFigure* basepointer = FigList[i]; 
      Point A = static_cast<CRectangle*>(basepointer)->GetCorner1(); 
      Point B = static_cast<CRectangle*>(basepointer)->GetCorner2(); 

      if ((x>=A.x && x<=B.x) || (x<=A.x && x>=B.x)) 
      { 
       if ((y >= A.y && x <= B.y) || (y <= A.y && x >= B.y)) 
       { 
        c++; 
       } 
      } 
     } 
     else if (dynamic_cast<CCircle*> (FigList[i])) 
     { 
      CFigure* basepointer = FigList[i]; 
      Point A = static_cast<CCircle*>(basepointer)->getCntr(); 
      int B = static_cast<CCircle*>(basepointer)->GetRadius(); 

      double distance = sqrt(pow((x - A.x), 2) + pow((y - A.y), 2)); 
      if (distance<=(double)B) 
      { 
       c++; 
      } 
     } 
     else if (dynamic_cast<CLine*> (FigList[i])) 
     { 
      CFigure* basepointer = FigList[i]; 
      Point A = static_cast<CLine*>(basepointer)->getPoint1(); 
      Point B = static_cast<CLine*>(basepointer)->getpoint2(); 
      double distance1 = sqrt(pow((x - A.x), 2) + pow((y - A.y), 2)); //Distance from point to P1 
      double distance2 = sqrt(pow((x - B.x), 2) + pow((y - B.y), 2)); //Distance from Point to P2 
      double distance3 = sqrt(pow((B.x - A.x), 2) + pow((B.y - A.y), 2)); //Distance from P1 to P2 
      if (distance1+distance2==distance3) 
      { 
       c++; 
      } 
     } 
     else 
     { 
      CFigure* basepointer = FigList[i]; 
      Point p1 = static_cast<CTriangle*>(basepointer)->getp1(); 
      Point p2 = static_cast<CTriangle*>(basepointer)->getp2(); 
      Point p3 = static_cast<CTriangle*>(basepointer)->getp3(); 
      float alpha = (((float)p2.y - (float)p3.y)*((float)x - (float)p3.x) + ((float)p3.x - (float)p2.x)*((float)y - (float)p3.y))/
       (((float)p2.y - (float)p3.y)*((float)p1.x - (float)p3.x) + ((float)p3.x - (float)p2.x)*((float)p1.y - (float)p3.y)); 
      float beta = (((float)p3.y - (float)p1.y)*((float)x - (float)p3.x) + ((float)p1.x - (float)p3.x)*((float)y - (float)p3.y))/
       (((float)p2.y - (float)p3.y)*((float)p1.x - (float)p3.x) + ((float)p3.x - (float)p2.x)*((float)p1.y - (float)p3.y)); 
      float gamma = 1.0f - alpha - beta; 
      if (alpha>0 && beta>0 && gamma >0) 
      { 
       c++; 
      } 
     } 
    } 

    ///Add your code here to search for a figure given a point x,y 
    if (c==0) 
    { 
     return NULL; 
    } 

} 

你可以看到,我還沒有決定什麼就又回來,但我的問題是這裏使用動態轉換的最佳解決方案?

-CLine,CTriangle,CRectangle和CCircle都是從CFigure

+9

你應該通過實現多態的方法,每類實現了命中檢測。 –

+0

@OliverCharlesworth我很抱歉,我沒有跟隨,已經有檢測功能在用戶點擊 – AhmedKh

+0

@AhmedKh,什麼奧利弗的意思是,你在做什麼是不使用正確的OOP技術。你基本上有一個關於數字類型的大開關語句。更好的辦法是必須在'CFigure'類抽象的虛方法(我們稱之爲'contains')返回一個'bool'告訴你,如果點在圖與否。然後只需通過FigList並調用'contains'而不需要cast。 – pcarter

回答

1

在課堂CFigure

virtual bool isclicked(int x, int y) = 0; 

這是一個純虛擬函數。 CFigure的所有子類都必須實現它。子類的實現檢查點擊是否在其邊界內,並相應地返回true或false。

的減少ApplicationManager::GetFigure喜歡的東西

CFigure *ApplicationManager::GetFigure(int x, int y) const 
{ 
    for (size_t i = 0; i < FigCount; i++) 
    { 
     if (FigList[i]->isclicked(x,y)) 
     { 
      return FigList[i]; 
     } 
    } 
    return nullptr; 
} 

通過虛函數和多態的神奇,程序會找出子類的isclicked功能需要您進行任何進一步的努力,被稱爲其。

+0

我從來沒有真正想到過,非常感謝! – AhmedKh

0

可以使用虛函數的處理進入每一個派生類,而不是測試的類型來決定如何處理它們派生類。

Virtual Functions

而不是做這個的:

struct B {}; 
struct D1: B {}; 
struct D2: B {}; 

// ... 

void func(B* b) 
{ 
    int c = 0; 

    if(dynamic_cast<D1*>(b)) 
    { 
     // do D1 stuff 
     c = ... 
    } 
    else if(dynamic_cast<D2*>(b)) 
    { 
     // do D2 stuff 
     c = ... 
    } 
} 

你的目標應該是讓每個子類知道如何計算自己:

struct B { virtual int calc() = 0; }; // virtual function calls derived type 
struct D1: B { int calc() override { int c = 0; /* D1 calculation */ return c; } }; 
struct D2: B { int calc() override { int c = 0; /* D2 calculation */ return c; } }; 

// ... 

void func(B* b) 
{ 
    int c = b->calc(); // virtual means the correct type's function is used 
}