![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2006
Posts: 115
Rep Power: 3
![]() |
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);
};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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| rs232 class problem | Brent | C++ | 3 | Jun 29th, 2006 5:38 PM |
| 1 class, 1 problem = 1 big headache | Eric the Red | Java | 7 | May 23rd, 2006 11:49 PM |
| Problem with class constructor | paeck | C++ | 8 | Feb 7th, 2006 12:53 PM |
| problem with user defined class mixed with functions | willj729 | C++ | 4 | Oct 9th, 2005 3:26 PM |
| MFC/OpenGL: problem with 'Document' class | brenda | C++ | 11 | May 23rd, 2005 8:10 PM |