Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
Old Oct 21st, 2006, 7:49 PM   #1
brad sue
Hobbyist Programmer
 
Join Date: Mar 2006
Posts: 120
Rep Power: 3 brad sue is on a distinguished road
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
brad sue is offline   Reply With Quote
 

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
rs232 class problem Brent C++ 3 Jun 29th, 2006 6:38 PM
1 class, 1 problem = 1 big headache Eric the Red Java 7 May 24th, 2006 12:49 AM
Problem with class constructor paeck C++ 8 Feb 7th, 2006 1:53 PM
problem with user defined class mixed with functions willj729 C++ 4 Oct 9th, 2005 4:26 PM
MFC/OpenGL: problem with 'Document' class brenda C++ 11 May 23rd, 2005 9:10 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:08 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC