#include #include "rectangle.h" Rectangle::Rectangle(int l, int t, int w, int h, string name, string clr) { left = l; top = t; /* width must be at least equal to the width of name plus one character of blank space on either side of name */ if (w < (name.length() + 2)) width = name.length() + 2; else width = w; /* height must be at least 3--one character for the label plus one character of blank space above and below the label */ if (h < 3) height = 3; else height = h; label = name; color = clr; } Rectangle::~Rectangle() { } void Rectangle::draw() { // code to draw a rectangle on a screen--too complicated to show here } void Rectangle::move(int x, int y) { left = x; top = y; } /* The rectangle always needs to be big enough to accommodate the text label plus have one character of blank space on either side of the label. resize therefore needs to check whether the new width is less than this threshold amount, and if so, set the width to this threshold amount. Similarly the height needs to be at least 3--one character high for the label, and one character of blankspace above and below the string. In a real interface we would have to compute the pixel height of a character. For simplicity we will assume here that a character has a height of 1. */ void Rectangle::resize(int wd, int ht) { if (wd < (label.length() + 2)) width = label.length() + 2; else width = wd; if (ht < 3) ht = 3; else height = ht; } /* store name in the label field. After setting the label, make sure that the width of the rectangle is enough to accommodate the new label. */ void Rectangle::setLabel(string name) { label = name; if (width < (label.length() + 2)) width = label.length() + 2; } /* store newColor in the color field. */ void Rectangle::setColor(string newColor) { color = newColor; } /* A rectangle contains a point if the x-value of the point lies between the left and right sides of the rectangle and the y-value of the point lies between the top and bottom of the rectangle */ int Rectangle::containsPt(int x, int y) { return ((left <= x) && (x <= (left + width)) && (top <= y) && (y <= (top + height))); } int Rectangle::getLeft() { return left; } int Rectangle::getTop() { return top; } int Rectangle::getWidth() { return width; } int Rectangle::getHeight() { return height; } string Rectangle::getLabel() { return label; } string Rectangle::getColor() { return color; }