| brad sue |
Oct 21st, 2006 7:49 PM |
class problem
Hi,
Please I would for someone to check if the function I wrote is fine.This is the context:
I have 2 classes Point and Rectangle
:
class Point{
private:
int x;
int y;
public:
Point();
Point(int,int);
~Point();
void setPoint(int,int);
int getX(void) const;
int getY(void) const;
};
class Rectangle{
private:
int height;
int width;
Point upLeft;
char Paint;
public:
Rectangle(void);
Rectangle(Point,int,int,char);
~Rectangle();
void set(Point,int,int,char);//set values of existing rectangle
void setPaint(char);
void setPoint(Point);//sets upper left corner of rectangle
void setPoint(int, int);//sets upper left corner of rectangle
void setSize(int,int);//sets height and width
int getX(void);//Return upp left corners' x coordinate
int getY(void);//Return upp left corners' y coordinate
int getWidth(void);
int getHeight(void);
char getPaint(void);
void setUnion(Rectangle, Rectangle);
};
I want to implement void setUnion(Rectangle r1, Rectangle r2).
SetUnion sets the location and dimension ofthe rectangle to be the union of
rectangles r1 and r2.
the function follows this algorithm:
The result's x is the smaller of r1's x and r2's x
The result's y is the smaller of r1's y and r2's y
The lower-right corner's x (lrx) is the maximum of
(r1's x + r1's width) and (r2's x + r2's width)
The lower-right corner's y (lry) is the maximum of
(r1's y + r1's height) and (r2's y + r2's height)
The result's width is lrx-x
The result's height is lry-y
This what I did:
( I believe that the function should be called like: r5.setUnion(r1,r3);)
:
void Rectangle::setUnion(Rectangle r1,Rectangle r2)
{
int xu,yu;// coordinate of upper left point union of 2 rectangles
int lrx,lry;//coordinate of lower right point union of 2 rectangles
if( r1.getX()> r2.getX() )
xu=r2.getX();
else
xu=r1.getX();
if( r1.getY() > r2.getY() )
yu=r2.getY();
else
yu=r1.getY();
if( (r1.getX()+r1.getWidth() ) < (r2.getX()+r2.getWidth() ) )
lrx= r2.getX()+r2.getWidth();
else
lrx=r1.getX()+r1.getWidth();
if( (r1.getY()+r1.getHeight() ) < (r2.getY()+r2.getHeight() ) )
lry= r2.getY()+r2.getHeight()
else
lry=r1.getY()+r1.getHeight();
upleft.setPoint(xu,yu); // Here I am not sure
width=lrx-xu;
height=lry-yu;
}
If I understand the problem correctly, the starting point ( LOCATION) is always the upper left corner .From there you do all the manipulations.
Thank you for your help
B
|