Quarter verification function



  • For entry of coordinates (2.0) ; (2.2); (0.2); (0.0) square is not found. I understand the function doesn't take many cases into account, but why when I ask a square, the function doesn't give anything?

    bool TryToFormSquare (const Point& p1, const Point& p2,
                          const Point& p3, const Point& p4,
                          Rect& rect)
    {
      if ( (sqrt((p2.x-p1.x) * (p2.x-p1.x) + (p2.y-p1.y) * (p2.y-p1.y))) == (sqrt((p3.x-p2.x)*(p3.x-p2.x)+(p3.y-p2.y)*(p3.y-p2.y)))==(sqrt((p4.x-p3.x)*(p4.x-p3.x)+(p4.y-p3.y)*(p4.y-p3.y)))==(sqrt((p1.x-p4.x)*(p1.x-p4.x)+(p1.y-p4.y)*(p1.y-p4.y))))
      {
        return true;
      }
      else if ((sqrt((p1.x-p3.x)*(p1.x-p3.x)+(p1.y-p3.y)*(p1.y-p3.y)))==(sqrt((p3.x-p2.x)*(p3.x-p2.x)+(p3.y-p2.y)*(p3.y-p2.y)))==(sqrt((p4.x-p2.x)*(p4.x-p2.x)+(p4.y-p2.y)*(p4.y-p2.y)))==(sqrt((p1.x-p4.x)*(p1.x-p4.x)+(p1.y-p4.y)*(p1.y-p4.y))))
      {
        return true;
      }
      else if ((sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)))==(sqrt((p4.x-p2.x)*(p4.x-p2.x)+(p4.y-p2.y)*(p4.y-p2.y)))==(sqrt((p3.x-p4.x)*(p3.x-p4.x)+(p3.y-p4.y)*(p3.y-p4.y)))==(sqrt((p1.x-p3.x)*(p1.x-p3.x)+(p1.y-p3.y)*(p1.y-p3.y))))
     {
       return true;
     }
     else
     {
       return false;
     }
    }
    

    Since I'm a newcomer, I can't answer my questions, so here's the code of the correct function: bool TryToFormSquare(const Point& p1, const Point& p2, const Point& p3, const Point& p4, Rect& rect) { if (((sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y)))==(sqrt((p3.x-p2.x)*(p3.x-p2.x)+(p3.y-p2.y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.x)*(p3.x-p4.x)+(p3.y-p4.y)*(p3.y-p4.y)))==(sqrt((p1.x-p3.x)*(p1.x-p3.x)+(p1.y-p3.y)*(p1.y-p3.y))))&&((sqrt((p1.x-p4.x)*(p1.x-p4.x)+(p1.y-p4.y)*(p1.y-p4.y)))==(sqrt((p3.x-p2.x)*(p3.x-p2.x)+(p3.y-p2.y)*(p3.y-p2.y))))) {return true;} else {return false;} }



    1. Read the code - remove it.
    2. sqrt() is not necessary - the equality of the numbers and the equality of the squares of the chiel is equivalent and the calculations vary.
    3. Bloki if-else doesn't really need it. Once the condition worked, we went out.
    4. XZ, how does design aa=bb==cc==dd, provided. I would write aa==dd " tie aa===bb " .
    5. It may be necessary to separate the wording of the distance (or square) between two points.

Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2