Bridges-C++  3.1.1
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:
32  setRectangle(0., 0., 1., 1.);
33  }
34 
40  Rectangle (int w, int h) {
41  setRectangle (0., 0., w, h);
42  }
43 
51  Rectangle (float locx, float locy, float w, float h) {
52  setRectangle (locx, locy, w, h);
53  }
54 
60  string getDataStructType() {
61  return "rect";
62  }
63 
69  string getName() const {
70  return "rect";
71  }
72 
78  void setWidth(float w) {
79  if (w <= 0.) {
80  throw "Illegal Size Value! Please enter a value in the range(0-300)";
81  }
82  width = w;
83  }
84 
90  void setHeight(float h) {
91  if (h <= 0.) {
92  throw "Illegal Size Value! Please enter a value in the range(0-300)";
93  }
94  height = h;
95  }
96 
103  void translate(float tx, float ty) {
104  const float *loc = getLocation();
105  float myloc[] = {loc[0], loc[1]};
106  translatePoint (myloc, tx, ty);
107  setLocation(myloc[0], myloc[1]);
108  }
115  void scale(float sx, float sy) {
116  // scale the height, width
117  // center remains the same
118  float pt[2] = {(float) width, (float) height};
119  scalePoint (pt, sx, sy);
120  width = pt[0];
121  height = pt[1];
122  }
123 
130  vector<float> getDimensions() const {
131 
132  vector<float> dims(4);
133  const float *location = getLocation();
134 
135  dims[0] = location[0];
136  dims[1] = location[0] + width;
137  dims[2] = location[1];
138  dims[3] = location[1] + height;
139 
140  return dims;
141  }
142 
143  /*
144  * This method sets the location and size of the rectangle
145  *
146  * @pram locx x coordinate of location
147  * @pram locy y coordinate of location
148  * @param w width of rectangle
149  * @param h height of rectangle
150  *
151  * @return none
152  */
153  void setRectangle(float locx, float locy, float w, float h) {
154  if (w <= 0 || h <= 0)
155  throw "Width, Height must be positive";
156  setLocation (float(locx), float(locy));
157  width = w;
158  height = h;
159  setShapeType("rect");
160  }
161 
167  virtual const string getSymbolRepresentation() const {
168 
169  string shape_json = getSymbolAttributeRepresentation();
170 
171  string shape = getShapeType();
172  shape_json +=
173  QUOTE + "name" + QUOTE + COLON + QUOTE + getLabel() + QUOTE + COMMA +
174  QUOTE + "shape" + QUOTE + COLON + QUOTE + shape + QUOTE + COMMA;
175 
176  // set up width and height of rectangles
177  if (shape == "rect")
178  shape_json += QUOTE + "width" + QUOTE + COLON + to_string(width) + COMMA +
179  QUOTE + "height" + QUOTE + COLON + to_string(height) +
180  CLOSE_CURLY;
181 
182  return shape_json;
183 
184  }
185  };
186  }
187 } // namespace bridges
188 
189 #endif
void setHeight(float h)
Definition: Rectangle.h:90
Rectangle(int w, int h)
Definition: Rectangle.h:40
void translate(float tx, float ty)
Definition: Rectangle.h:103
const string COLON
Definition: DataStructure.h:51
Rectangle(float locx, float locy, float w, float h)
Definition: Rectangle.h:51
virtual const string getSymbolRepresentation() const
Definition: Rectangle.h:167
STL namespace.
void setRectangle(float locx, float locy, float w, float h)
Definition: Rectangle.h:153
void scale(float sx, float sy)
Definition: Rectangle.h:115
const string CLOSE_CURLY
Definition: DataStructure.h:53
string getDataStructType()
Definition: Rectangle.h:60
This is an abstract class for deriving a number of Symbol shape objects, for use in a SymbolCollectio...
Definition: Symbol.h:32
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)
Definition: Rectangle.h:78
string getName() const
Definition: Rectangle.h:69
Rectangle()
Definition: Rectangle.h:31
vector< float > getDimensions() const
Definition: Rectangle.h:130
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