Amazon Interview Question
Web DevelopersCountry: United States
class Vertex{
private:
double _x, _y;
public:
Vertex(double x, double y){
_x = x;
_y = y;
}
Vertex(){
_x = 0;
_y = 0;
}
void setX(double x){
_x = x;
}
void setY(double y){
_y = y;
}
int getX(double x){
return _x;
}
int getY(double x){
return _y;
}
};
class Graphics{
public:
virtual void draw() = 0;
virtual void fill() = 0;
virtual void move() = 0;
virtual void resize() = 0;
virtual double getArea() = 0;
};
class Line:public Graphics{
private:
Vertex _left;
Vertex _right;
double _lineThickness;
public:
void draw();
void fill(){
}
void move();
void resize();
double getLength();
double getArea(){
return 0;
}
double getThickness();
};
class Rectangle: public Graphics{
private:
Vertex _leftUp;
Vertex _leftDown;
Vertex _rightUp;
Vertex _rightDown;
public:
void draw();
void fill();
void move();
void resize();
double getWidth();
double getHeight();
double getArea();
};
class Text: public Graphics{
private:
std::string _text;
Rectangle _rectangleToHoldText;
int _font;
bool _isBold;
bool _isItalic;
public:
void draw(); //Basically create a rectangle and write the text in it.
void fill(){
//Do Nothing
}
void move(); //Move the rectangle
void resize();
double getArea(){
return rectangleToHoldText.getArea();
}
};
class Circle: public Graphics{
private:
Vertex _center;
double _thickness;
double _radius;
public:
void draw();
void fill();
void move();
void resize();
double getArea();
};
Very much off the top of my head.
- Anonymous March 24, 2013Have a GraphicObject (interface or abstract class) that defines common operations and accessor for x,y coordinates. Some GraphicObjects such as paths may be comprised of sub-objects such as vertices (so a path is drawn from vertex to vertext). Also have a selection object that encapsulates several objects as a selection (composite pattern).
Each could also include a draw method that draws itself and its subobjects, or it could have a method which accepts a rendering object (ConcreteRenderer implements Renderer) which figures out how to draw each concrete object. The second approach would be more flexible. For example, you could then have a screen-renderer, a PDF-renderer, a Black-and-white renderer etc.
At the document level, don't expose any of these objects directly. Instead, use a state pattern. Just have a "Change Tool" method which changes the currently active tool, and a mouse-down and mouse-up method which are called when the user presses or releases the mouse on the drawing surface. When "Change Tool" is called, the Document sets the current tool and then subsequent calls to mousedown/up are passed to that tool.
For example, the user may change the tool to the select tool, then press the mouse down, drag, and release. These are passed to the select tool, which calculates which objects are selected and sets the Document's current Selection object to that selection.
Next the user chooses the "Move" tool. Subsequent mouse-downs and ups are interpreted as move requests for the currently active selection. The Move-tool object calls the selection's move method, and the selection calls the move method on all of its children.
Ditto with other commands, such as creation commands.
Also have a mediator which handles interaction between the document and the various GUI elements (such as the tool selection buttons).