#include <Rectangle.h>
Inheritance diagram for Rectangle:
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. |
Only rectangles parallel to the x and y axes can be represented.
|
We record which corner was closest to a hit.
00049 {TR, TL, BL, BR} hit_corner; |
|
Create a zero size rectangle at the origin.
00006 : DrawElement(), half_width(0.0), half_height(0.0) {} |
|
Create a rectangle between the two positions. It will be parallel to the x and y axes.
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 } |
|
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 } |
|
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 } |
|
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 } |
|
depending on location of first click move corner to new location.
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 } |
|
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 } |
|
Factory to create rectangles.
|
|
We represent a retcangle by its center (in the super class) and half the height and width.
|
|
We represent a retcangle by its center (in the super class) and half the height and width.
|
|
We record which corner was closest to a hit.
|