Thread: need some help

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    52

    need some help

    I need some help with this programming project I am creating. The first two things I had to do I got just fine. The first two things were to compute the area of r and to compute the center of r, returning it as a point value. The next two thing I am having trouble with. Here's what I need help with:
    1) Move r by x units in the x direction and y units in the y direction, returning the modified version of r. (x and y are additional arguments to the function.)
    2) Determine whether a point p lies within r, returning TRUE or FALSE. (p is an additional argument of type struct point.)

    I think I may have did the wrong thing for the second objective. I'm not sure. The complete direction was "compute the center of r, returning it as a point value." I don't think I used the point value and if anyone has any suggestions, please help. Here is the code I already have.

    Code:
    #include <stdio.h>
    
    struct point { int x, y;};
    struct rectangle { struct point upper_left, lower_right;} r;
    
    int area_of_r(struct rectangle rect);
    float center_of_rx(struct rectangle rect);
    float center_of_ry(struct rectangle rect);
    
    int main() {
        
      printf( "Input upper left corner of r : " );
       scanf( "%d %d", &r.upper_left.x, &r.upper_left.y );
        
      printf( "Input lower right corner of r : " );
       scanf( "%d %d", &r.lower_right.x, &r.lower_right.y );
      printf( "Area of r is %d\n", area_of_r(r) );
    
      printf( "The center of r is ( %.2f, %.2f )\n", center_of_rx(r), center_of_ry(r));
        
    return 0;
    
    }
    
    int area_of_r ( struct rectangle rect ) {
      int area=0;
      
     area = (rect.lower_right.x - rect.upper_left.x ) *
            ( rect.upper_left.y - rect.lower_right.y);
        
      return area;
    }
    
    float center_of_rx(struct rectangle rect) {
      float center=0;
      center = (rect.lower_right.x - rect.lower_right.y)/2;
    
    return center;
    }
    
    float center_of_ry(struct rectangle rect) {
      float center=0;
      center = (rect.upper_left.y - rect.upper_left.x)/2;
    
     return center;
    }

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Your code that determines the center point is wrong for a start. Surely the center x coordinate is the average of the upper left/bottom right x coordinates? ie (x1+x2)/2 and for y (y1+y2)/2, but your functions are (x2-y2)/2 and (y1-x1)/2 which makes little sense.

    As for the question of determining if another point is in the rectangle, you just check if the x coord of the point is between the x coordinate of the left side and the right side, then you check if the y coord of the point is between the y coordinate of the top side and bottom side, simple.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    52
    Ok, I figured out how I messed up the second part and I fixed it where it returns a point value. So here's the new code:

    Code:
    #include <stdio.h>
    
    struct point { float x, y;} center;
    struct rectangle { struct point upper_left, lower_right;} r;
    
    float area_of_r(struct rectangle rect);
    float center_of_r(struct rectangle rect);
    
    int main() {
        
      printf( "Input upper left corner of r : " );
       scanf( "%f %f", &r.upper_left.x, &r.upper_left.y );
        
      printf( "Input lower right corner of r : " );
       scanf( "%f %f", &r.lower_right.x, &r.lower_right.y );
      printf( "Area of r is %.2f\n", area_of_r(r) );
      center_of_r(r);
      printf( "The center of r is ( %.2f, %.2f )\n", center.x, center.y);
        
    return 0;
    
    }
    
    float area_of_r ( struct rectangle rect ) {
      float area=0;
      
     area = (rect.lower_right.x - rect.upper_left.x ) *
            ( rect.upper_left.y - rect.lower_right.y);
        
      return area;
    }
    
    float center_of_r(struct rectangle rect) {
      
      center.x = (rect.upper_left.x + rect.lower_right.x)/2;
      center.y = (rect.upper_left.y + rect.lower_right.y)/2;
    
    }
    Last edited by the_winky_files; 10-23-2005 at 07:33 PM.

  4. #4
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    That code is still wrong, see my above post.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    52
    yea, sorry. I didn't notice the actual math part being wrong. I just knew that the center was supposed to use the structure point when being printed. Is this correct now? I edited above.
    Last edited by the_winky_files; 10-23-2005 at 07:37 PM.

  6. #6
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Looks okay.

    It assumes that upper_left.x < lower_right.x and upper_left.y > lower_right.y in order to compute the area correctly, though. You could fix that with checking, or calculating the absolute value of the difference (the fabs function).

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    52
    Ok, I have everything completed finally but I'm getting one last problem with the function. For some reason when it runs the 'p_evaluation' function, and I input the points to be tested equal to one of the points provided in 'upper_left' or 'lower_right' it returns FALSE. This is a point on the graph of the rectangle and it seems like my condition I have made would make this true. Any suggestions?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct point { float x, y;} center, units, p;
    struct rectangle { struct point upper_left, lower_right;} r;
    
    float area_of_r(struct rectangle rect);
    float center_of_r(struct rectangle rect);
    float move_units(struct rectangle rect);
    int p_evaluation(struct rectangle rect);
    
    int main() {
      char condition[6];
      int i;
      
      printf( "Input upper left corner of r : " );
       scanf( "%f %f", &r.upper_left.x, &r.upper_left.y );
        
      printf( "Input lower right corner of r : " );
       scanf( "%f %f", &r.lower_right.x, &r.lower_right.y );
      printf( "Area of r is %.2f\n", area_of_r(r) );
      center_of_r(r);
      printf( "The center of r is ( %.2f, %.2f )\n", center.x, center.y);
    
      move_units(r);
      i = p_evaluation(r);
      if(i == 1)
        strcpy(condition, "TRUE");
       else
        strcpy(condition, "FALSE");   
    
      printf("\nThe point p evaluation returned: %s\n", condition);
        
    return 0;
    
    }
    
    float area_of_r ( struct rectangle rect ) {
      float area=0;
      
     area = (rect.lower_right.x - rect.upper_left.x )*( rect.upper_left.y - rect.lower_right.y);
        
      return area;
    }
    
    float center_of_r(struct rectangle rect) {
      
      center.x = (rect.upper_left.x + rect.lower_right.x)/2;
      center.y = (rect.upper_left.y + rect.lower_right.y)/2;
    
    }
    
    float move_units(struct rectangle rect) {
    
      printf("Type the number of units to move the rectangle: ");
       scanf("%f %f", &units.x, &units.y);
      printf("\nThe modified points of the rectangle are: \nUpper left: ( %.2f, %.2f ) \nLower right: ( %.2f, %.2f)\n"
             , rect.upper_left.x + units.x, rect.upper_left.y + units.y, 
             rect.lower_right.x + units.x, rect.lower_right.y + units.y);
    
    }
    
    int p_evaluation(struct rectangle rect) {
      int i=0, j=1;
    
      printf("Type a point to test if it is within the rectangle: ");
       scanf("%f %f", &p.x, &p.y);
    
      if(rect.upper_left.x <= p.x <= rect.lower_right.x && rect.upper_left.y >= p.y >= rect.lower_right.y)
        return j;
      else
        return i;
    }

  8. #8
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Although the expression x <= y <= z makes sense in maths, it won't do what you expect in C.

    What it will do, is evaluate x <= y (and return 1 if true, 0 if false), and it will then take the result of that evaluation (1 or 0), and apply it to <= z. It will return the result of 1 <= z or 0 <=z as appropriate. This is clearly not what you want.

    You need if (x <= y && y <= z) instead.

  9. #9
    Registered User
    Join Date
    Sep 2005
    Posts
    52
    awesome, thanks for the help. I got it going good now.

Popular pages Recent additions subscribe to a feed