Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

Rectangle Class Reference

A drawing elemnt representing a rectangle. More...

#include <Rectangle.h>

Inheritance diagram for Rectangle:

DrawElement Object List of all members.

Public Methods

 Rectangle ()
 Create a zero size rectangle at the origin.

 Rectangle (const Position &corner1, const Position &corner2, color c)
 Create a rectangle between the two positions.

virtual void add_feature (std::string, float)
 Permit width and height to be specified.

void move (const Position &corner1, const Position &corner2)
 Move the rectangle to a new position.

bool is_hit (const Position &)
 Check if hit and record which corner is closest.

int mutate (const Position &)
 depending on location of first click move corner to new location.

void render (SimpleWindow *)
 Draw rectangle into window.


Private Types

enum  { TR, TL, BL, BR }
 We record which corner was closest to a hit. More...


Private Attributes

float half_width
 We represent a retcangle by its center (in the super class) and half the height and width.

float half_height
 We represent a retcangle by its center (in the super class) and half the height and width.

enum Rectangle:: { ... }  hit_corner
 We record which corner was closest to a hit.


Static Private Attributes

Rectangle::Factory factory
 Factory to create rectangles.


Detailed Description

A drawing elemnt representing a rectangle.

Only rectangles parallel to the x and y axes can be represented.


Member Enumeration Documentation

anonymous enum [private]
 

We record which corner was closest to a hit.

Enumeration values:
TR 
TL 
BL 
BR 

00049 {TR, TL, BL, BR} hit_corner;


Constructor & Destructor Documentation

Rectangle::Rectangle  
 

Create a zero size rectangle at the origin.

00006 : DrawElement(), half_width(0.0), half_height(0.0) {}

Rectangle::Rectangle const Position &    corner1,
const Position &    corner2,
color    c
 

Create a rectangle between the two positions.

It will be parallel to the x and y axes.

Parameters:
corner1  one corner
corner2  another corner (corners are unordered)
c  color to use to fill rectangle

00009   : DrawElement((corner1+corner2)/2,c), hit_corner(TL)
00010 {
00011   Position delta = corner1 - corner2;
00012   half_width = abs(delta.GetXDistance())/2;
00013   half_height = abs(delta.GetYDistance())/2;
00014 }


Member Function Documentation

void Rectangle::add_feature std::string   ,
float   
[virtual]
 

Permit width and height to be specified.

Reimplemented from Object.

00017 {
00018   if (key == "width") {
00019     half_width = abs(value/2.0);
00020   } else if (key == "height") {
00021     half_height = abs(value/2.0);
00022   } else {
00023     Object::add_feature(key,value);
00024   }
00025 }

bool Rectangle::is_hit const Position &    [virtual]
 

Check if hit and record which corner is closest.

Implements DrawElement.

00036 {
00037   if (half_width == 0 || half_height == 0) return false;
00038   Position delta = pos-get_center();
00039   float xoff = delta.GetXDistance();
00040   float yoff = delta.GetYDistance();
00041   if (-half_width <= xoff && xoff <= half_width &&
00042       -half_height <= yoff && yoff <= half_height) {
00043     if (xoff < 0) {
00044       if (yoff < 0)
00045         hit_corner = TL;
00046       else
00047         hit_corner = BL;
00048     } else {
00049       if (yoff < 0)
00050         hit_corner = TR;
00051       else
00052         hit_corner = BR;
00053     }
00054     return true;
00055   }
00056   return false;
00057 }

void Rectangle::move const Position &    corner1,
const Position &    corner2
 

Move the rectangle to a new position.

00028 {
00029   DrawElement::move((corner1+corner2)/2);
00030   Position delta = corner1 - corner2;
00031   half_width = abs(delta.GetXDistance())/2;
00032   half_height = abs(delta.GetYDistance())/2; 
00033 }

int Rectangle::mutate const Position &    [virtual]
 

depending on location of first click move corner to new location.

Returns :
1 if success, 0 otherwise.

Reimplemented from DrawElement.

00060 {
00061   Position old_corner_delta;
00062   switch (hit_corner) {
00063   default:
00064     return 0;
00065   case TL:
00066     old_corner_delta = Position(half_width,half_height);
00067     break;
00068   case TR:
00069     old_corner_delta = Position(-half_width,half_height);
00070     break;
00071   case BR:
00072     old_corner_delta = Position(-half_width,-half_height);
00073     break;
00074   case BL:
00075     old_corner_delta = Position(half_width,-half_height);
00076     break;
00077   }
00078   move(new_corner,get_center()+old_corner_delta);
00079   return 1;
00080 }

void Rectangle::render SimpleWindow   [virtual]
 

Draw rectangle into window.

Implements DrawElement.

00083 {
00084   Position delta = Position(half_width,half_height);
00085   w->RenderRectangle(get_center()-delta,get_center()+delta,get_color(),
00086                      is_selected());
00087 }


Member Data Documentation

Rectangle::Factory Rectangle::factory [static, private]
 

Factory to create rectangles.

float Rectangle::half_height [private]
 

We represent a retcangle by its center (in the super class) and half the height and width.

float Rectangle::half_width [private]
 

We represent a retcangle by its center (in the super class) and half the height and width.

enum { ... } Rectangle::hit_corner [private]
 

We record which corner was closest to a hit.


The documentation for this class was generated from the following files:
Generated on Fri Nov 8 10:52:31 2002 for Draw by doxygen1.2.17