Bridges-C++  3.2.0
Bridges(C++API)
Rectangle.h
Go to the documentation of this file.
1 #include "DataStructure.h"
2 #include "Symbol.h"
3 #include <vector>
4 
5 
6 using namespace std;
7 
8 #ifndef RECTANGLE_H
9 
10 #define RECTANGLE_H
11 
12 namespace bridges {
13  namespace datastructure {
22  class Rectangle : public Symbol {
23  private:
24  // height, width of rectangle
25  float width = 1.0, height = 1.0;
26 
27  public:
33  setRectangle(0., 0., 1., 1.);
34  }
35 
41  Rectangle (int w, int h) {
42  setRectangle (0., 0., w, h);
43  }
44 
53  Rectangle (float locx, float locy, float w, float h) {
54  setRectangle (locx, locy, w, h);
55  }
56 
62  string getDataStructType() {
63  return "rect";
64  }
65 
71  string getName() const {
72  return "rect";
73  }
74 
80  void setWidth(float w) {
81  if (w <= 0.) {
82  throw "Illegal Size Value! Please enter a value in the range(0-300)";
83  }
84  width = w;
85  }
86 
92  void setHeight(float h) {
93  if (h <= 0.) {
94  throw "Illegal Size Value! Please enter a value in the range(0-300)";
95  }
96  height = h;
97  }
98 
105  void translate(float tx, float ty) {
106  const float *loc = getLocation();
107  float myloc[] = {loc[0], loc[1]};
108  translatePoint (myloc, tx, ty);
109  setLocation(myloc[0], myloc[1]);
110  }
117  void scale(float sx, float sy) {
118  // scale the height, width
119  // center remains the same
120  float pt[2] = {(float) width, (float) height};
121  scalePoint (pt, sx, sy);
122  width = pt[0];
123  height = pt[1];
124  }
125 
132  vector<float> getDimensions() const {
133 
134  vector<float> dims(4);
135  const float *location = getLocation();
136 
137  dims[0] = location[0];
138  dims[1] = location[0] + width;
139  dims[2] = location[1];
140  dims[3] = location[1] + height;
141 
142  return dims;
143  }
144 
145  /*
146  * @brief This method sets the location and size of the rectangle
147  *
148  * @pram locx x coordinate of location
149  * @pram locy y coordinate of location
150  * @param w width of rectangle
151  * @param h height of rectangle
152  *
153  * @return none
154  */
155  void setRectangle(float locx, float locy, float w, float h) {
156  if (w <= 0 || h <= 0)
157  throw "Width, Height must be positive";
158  setLocation (float(locx), float(locy));
159  width = w;
160  height = h;
161  setShapeType("rect");
162  }
163 
169  virtual const string getSymbolRepresentation() const {
170 
171  string shape_json = getSymbolAttributeRepresentation();
172 
173  string shape = getShapeType();
174  shape_json +=
175  QUOTE + "name" + QUOTE + COLON + QUOTE + getLabel() + QUOTE + COMMA +
176  QUOTE + "shape" + QUOTE + COLON + QUOTE + shape + QUOTE + COMMA;
177 
178  // set up width and height of rectangles
179  if (shape == "rect")
180  shape_json += QUOTE + "width" + QUOTE + COLON + to_string(width) + COMMA +
181  QUOTE + "height" + QUOTE + COLON + to_string(height) +
182  CLOSE_CURLY;
183 
184  return shape_json;
185 
186  }
187  };
188  }
189 } // namespace bridges
190 
191 #endif
void setHeight(float h)
This method sets the shape height.
Definition: Rectangle.h:92
Rectangle(int w, int h)
create rectangle with width w and height h
Definition: Rectangle.h:41
void translate(float tx, float ty)
Translate the rectangle.
Definition: Rectangle.h:105
const string COLON
Definition: DataStructure.h:51
Rectangle(float locx, float locy, float w, float h)
create rectangle with width w and height h and located at given position
Definition: Rectangle.h:53
virtual const string getSymbolRepresentation() const
This method returns the JSON representation of the shape.
Definition: Rectangle.h:169
STL namespace.
void setRectangle(float locx, float locy, float w, float h)
Definition: Rectangle.h:155
void scale(float sx, float sy)
Scale the rectangle about its center.
Definition: Rectangle.h:117
const string CLOSE_CURLY
Definition: DataStructure.h:53
string getDataStructType()
This method gets the data type name.
Definition: Rectangle.h:62
This is an abstract class for deriving a number of Symbol shape objects, for use in a SymbolCollectio...
Definition: Symbol.h:33
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4
void setWidth(float w)
This method sets the shape width.
Definition: Rectangle.h:80
string getName() const
This method gets the name of the shape.
Definition: Rectangle.h:71
Rectangle()
default constructor - rectangle with lower left at origin and unit length sides
Definition: Rectangle.h:32
vector< float > getDimensions() const
Definition: Rectangle.h:132
const string COMMA
Definition: DataStructure.h:50
const string QUOTE
Definition: DataStructure.h:49
This class defines a rectangle and is part of the symbol collection. A rectangle has height and width...
Definition: Rectangle.h:22