Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) Shape

 

Shape is a class for a shape.

 

 

 

 

 

shape.h

 

//---------------------------------------------------------------------------
/*
Shape, shape class
Copyright (C) 2011 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppShape.htm
//---------------------------------------------------------------------------
#ifndef SHAPE_H
#define SHAPE_H
//---------------------------------------------------------------------------
#include <string>
#include <vector>
//---------------------------------------------------------------------------
#include <boost/checked_delete.hpp>
#include <boost/signals2.hpp>
//---------------------------------------------------------------------------
///Shape is a class for a regular polygonal shape
///
///A Shape can be a circle, ellipse, triange, square, rhombus, rectangle
///A Shape cannot yet be a minus, plus, star
struct Shape
{
  Shape(
    const int n_corners,
    const double rotation,
    const unsigned char red = 255,
    const unsigned char green = 255,
    const unsigned char blue = 255);

  ///Get the blueness of the Shape its color
  unsigned char GetBlue() const { return m_blue; }

  ///Get the greenness of the Shape its color
  unsigned char GetGreen() const { return m_green; }

  ///Get the number of corners of the Shape
  double GetNumberOfCorners() const { return m_n_corners; }

  ///Get the redness of the Shape its color
  unsigned char GetRed() const { return m_red; }

  ///Get the rotation of the Shape
  double GetRotation() const { return m_rotation; }

  ///Set the number of corners of the Shape
  void SetNumberOfCorners(const int n_corners);

  ///Set the rotation of the Shape
  void SetRotation(const double rotation);

  ///The signal emitted when the Shape position is changed
  mutable boost::signals2::signal<void ()> m_signal_changed;

  private:
  //Shape can only be deleted by Boost smart pointers
  virtual ~Shape() {}
  friend void boost::checked_delete<>(Shape*);

  friend bool operator==(const Shape& lhs, const Shape& rhs);

  ///The number of corners
  int m_n_corners;

  ///The rotation
  ///
  ///The rotation is a value between 0.0 and 2.0 * M_PI,
  ///in which  0.0 * M_PI is the equivalent to 12:00 o'clock
  ///and which 0.5 * M_PI is the equivalent to  3:00 o'clock
  ///Values not in this range are accepted nonetheless
  double m_rotation;

  ///The shape its redness
  unsigned char m_red;

  ///The shape its greenness
  unsigned char m_green;

  ///The shape its blueness
  unsigned char m_blue;

  public:
  static double GetAngle(const double dX, const double dY);
  static double GetDistance(const double dX, const double dY);
  static const std::string GetVersion();
  static const std::vector<std::string> GetVersionHistory();
};
//---------------------------------------------------------------------------
bool operator==(const Shape& lhs, const Shape& rhs);
//---------------------------------------------------------------------------
#endif // SHAPE_H
//---------------------------------------------------------------------------

 

 

 

 

 

shape.cpp

 

//---------------------------------------------------------------------------
/*
Shape, shape class
Copyright (C) 2011 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppShape.htm
//---------------------------------------------------------------------------
#include <cassert>
#include <cmath>
//---------------------------------------------------------------------------
#include "shape.h"
//#include "trace.h"
//---------------------------------------------------------------------------
Shape::Shape(
  const int n_corners,
  const double rotation,
  const unsigned char red,
  const unsigned char green,
  const unsigned char blue)
  : m_red(red),
    m_green(green),
    m_blue(blue)
{
  SetNumberOfCorners(n_corners);
  SetRotation(rotation);
}
//---------------------------------------------------------------------------
const std::string Shape::GetVersion()
{
  return "2.0";
}
//---------------------------------------------------------------------------
const std::vector<std::string> Shape::GetVersionHistory()
{
  std::vector<std::string> v;
  v.push_back("2011-07-13: Version 1.0: initial version");
  v.push_back("2011-08-08: Version 2.0: conformized architecture to MysteryMachineWidget");
  return v;
}
//---------------------------------------------------------------------------
void Shape::SetNumberOfCorners(const int n_corners)
{
  assert(n_corners >= 0);
  m_n_corners = n_corners;
}
//---------------------------------------------------------------------------
void Shape::SetRotation(const double rotation)
{
  if (m_rotation != rotation)
  {
    m_rotation = rotation;
    m_signal_changed();
  }
}
//---------------------------------------------------------------------------
//From www.richelbilderbeek.nl/CppGetAngle.htm
double Shape::GetAngle(const double dX, const double dY)
{
  //In which quadrant are we?
  if (dX > 0.0)
  {
    if (dY > 0.0)
    {
      //dX > 0.0 && dY > 0.0
      //Quadrant IV
      assert(dX > 0.0 && dY > 0.0);
      const double angle = (1.0 * M_PI) - std::atan(dX / dY);
      assert(angle > 0.5 * M_PI && angle < 1.0 * M_PI);
      return angle;
    }
    else if (dY < 0.0)
    {
      //dX > 0.0 && dY <= 0.0
      //Quadrant I
      assert(dX > 0.0 && dY < 0.0);
      const double angle = (0.0 * M_PI) - std::atan(dX / dY);
      assert(angle > 0.0 * M_PI && angle < 0.5 * M_PI);
      return angle;
    }
    else
    {
      //dX > 0.0 && dY == 0.0
      //On Y-axis, right side
      assert(dX > 0.0 && dY == 0.0);
      const double angle = 0.5 * M_PI;
      return angle;
    }
  }
  else if (dX < 0.0)
  {
    if (dY > 0.0)
    {
      //dX < 0.0 && dY > 0.0
      //Quadrant III
      assert(dX < 0.0 && dY > 0.0);
      const double angle = (1.0 * M_PI) - std::atan(dX / dY);
      assert(angle > 1.0 * M_PI && angle < 1.5 * M_PI);
      return angle;
    }
    else if (dY < 0.0)
    {
      //dX < 0.0 && dY < 0.0
      //Quadrant II
      assert(dX < 0.0 && dY < 0.0);
      const double angle = (2.0 * M_PI) - std::atan(dX / dY);
      assert(angle > 1.5 * M_PI && angle < 2.0 * M_PI);
      return angle;
    }
    else
    {
      //dX < 0.0 && dY == 0.0
      //On X-axis
      assert(dX < 0.0 && dY == 0.0);
      const double angle = 1.5 * M_PI;
      return angle;
    }
  }
  else
  {
    if (dY > 0.0)
    {
      //dX == 0 && dY > 0.0)
      //On Y-axis, right side of origin
      assert(dX==0.0 && dY > 0.0);
      const double angle = 1.0 * M_PI;
      return angle;
    }
    else if (dY < 0.0)
    {
      //dX == 0 && dY < 0.0)
      //On Y-axis, left side of origin
      assert(dX==0.0 && dY < 0.0);
      const double angle = 0.0 * M_PI;
      return angle;
    }
    else
    {
      //dX == 0.0 && dY == 0.0)
      //On origin
      assert(dX==0.0 && dY == 0.0);
      const double angle = 0.0 * M_PI;
      return angle;
    }
  }
}
//---------------------------------------------------------------------------
//From www.richelbilderbeek.nl/CppGetDistance.htm
double Shape::GetDistance(const double dX, const double dY)
{
  return std::sqrt( (dX * dX) + (dY * dY) );
}
//---------------------------------------------------------------------------
bool operator==(const Shape& lhs, const Shape& rhs)
{
  return lhs.m_blue == rhs.m_blue
    &&   lhs.m_green == rhs.m_green
    &&   lhs.m_n_corners == rhs.m_n_corners
    &&   lhs.m_red == rhs.m_red
    &&   lhs.m_rotation == rhs.m_rotation;
}
//---------------------------------------------------------------------------

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml