# Thread: more with my 1st structure program

1. > can't typecast the varibles in function ?
Well you can, but that isn't going to help since the final result is still an integer (centre.x is an int), so you'll automatically lose any precious fractions at that point.

now my next issue is the moving of the rectangle to new
coordinates along the x-y axis

I am guessing here, but would I not ASK USER to give me the the amount to move in x direction, the amount to move in y direction and then printout the points of the new rectangle, IF the problem says: "move r by x units in the x direction and y units in the y direction; x and y are additional arguments to the function" ???

What is the prototype of such function??

I created a bitmap file to show my rectangles, but see I cannot attach bitmaps to this board. Argh!

Code:
```#include <stdio.h>

/********************************/
/* declare structures           */
/********************************/

struct point {
int x, y;
};

struct rectangle {
struct point upper_left, lower_right;
} ;

/*********************************/
/* function prototypes           */
/*********************************/

int area_of_r(struct rectangle rect);
struct point center_of_r(struct rectangle rect);
struct point move_r(struct rectangle rect, int x, int y);

int main() {

struct rectangle r;
struct point center;
int area;

int coord_x, coord_y;

printf( "input upper left corner of rectangle r "
"as 0 0\n");
scanf( "%d %d", &r.upper_left.x, &r.upper_left.y );
printf( "input lower right corner of rectangle r  "
"as 0 0\n");
scanf( "%d %d", &r.lower_right.x, &r.lower_right.y );

area = area_of_r(r);
printf( "\nThe area of rectangle r is "
"%d\n", area);

center = center_of_r(r);
printf("\nThe center point of rectangle r is "
"%d %d\n\n",center.x, center.y);

return 0;
}

int area_of_r ( struct rectangle rect )
{
int area;
area = (rect.lower_right.x - rect.upper_left.x) *
(rect.upper_left.y - rect.lower_right.y);
return area;
}

struct point center_of_r(struct rectangle rect)
{
struct point center;

center.x = ((rect.lower_right.x) +
(rect.upper_left.x))/2;
center.y = ((rect.upper_left.y) +
(rect.lower_right.y))/2;

return center;
}

/*  starting on this function to move rectangle to new coordinates */

struct point move_r(struct rectangle rect, int x, int y)
{
struct point modified_r;

modified_r.upper_left.x = ((rect.upper_left.x)+x);

}

```

3. > but would I not ASK USER to give me the the amount to move in x direction, the amount to move in y direction
That's a good start

> What is the prototype of such function?
- a rectangle
- a new x position
- a new y position
- a moved rectangle

So this seems like a good idea
struct rectangle move_r(struct rectangle rect, int x, int y);

4. not trying to be an ignoramus here, but why
would I use the prototype::
struct rectangle move_r(struct rectangle rect, int x, int y);

why wouldn't I use ::
struct point move_r(struct rectangle rect, int x, int y); ????

assuming using rectangle structure as suggested:
in the main i would have to declare some variables to use in the call of the above function, right? (say move_x and move_y)
and another rectangle structure (say new_r)

then to prompt user for input:
Code:
```printf("please enter distance to move on x axis");
printf("please enter distance to move on y axis");
printf("enter whole number values as # #\n");
scanf("%d %d",&move_x,&move_y);```
then output could be this:
Code:
```printf("coords of moved rectangle are : \n ");
printf("upper left corner : (%d,%d)\n",new_r.upper_left.x,new_r.upper_left.y);
printf("lower right corner : (%d,%d)\n",new_r.lower_right.x,new_r.lower_right.y);```
Is this even close?????

5. Expanding on your code segment...
Code:
```printf("please enter distance to move on x axis");
printf("please enter distance to move on y axis");
printf("enter whole number values as # #\n");
scanf("%d %d",&move_x,&move_y);

new_r = move_r(struct rectangle rect, int x, int y);

printf("coords of moved rectangle are : \n ");
printf("upper left corner : (%d,%d)\n", new_r.upper_left.x,new_r.upper_left.y);
printf("lower right corner : (%d,%d)\n",new_r.lower_right.x,new_r.lower_right.y);```
The way that you are referencing new_r (like new_r.upper_left.x), you are treating it like it's a rectangle, so that's really what it should be. Hence, move_r needs to return a rectangle.

6. I got the program almost done.

now I need to find if a point (p) lies in the rectangle r (the original rectangle now)

instructions say to return TRUE or FALSE
and p is an additional argument of type struct point

now what is a cool way to do this???

Shall I use typedef BOOL or what??

7. well forgot to give current coded program
btw, someone not on the board helped me with function finding
center so that points with float values will print.

here the code

Code:
```#include <stdio.h>

/********************************/
/* declare structures           */
/********************************/

struct point {
int x, y;
};

struct rectangle {
struct point upper_left, lower_right;
} ;

/*********************************/
/* function prototypes           */
/*********************************/

int area_of_r(struct rectangle rect);
float center_of_r(struct rectangle rect);
void move_r(struct rectangle rect, int x, int y);

int main() {

struct rectangle r;
int area;
int move_x, move_y;

printf( "input upper left corner of rectangle r "
"as 0 0\n");
scanf( "%d %d", &r.upper_left.x, &r.upper_left.y );
printf( "input lower right corner of rectangle r  "
"as 0 0\n");
scanf( "%d %d", &r.lower_right.x, &r.lower_right.y );

area = area_of_r(r);
printf( "\nThe area of rectangle r is "
"%d\n", area);

center_of_r(r);

printf("input the amount to move on the x and y axes\n");
printf("input as # #\n");
scanf("%d %d",&move_x,&move_y);
move_r(r, move_x,move_y);

return 0;
}

int area_of_r ( struct rectangle rect )
{
int area;
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)
{
float center_x, center_y;

center_x = (rect.lower_right.x + rect.upper_left.x)/2;
center_y = (rect.upper_left.y + rect.lower_right.y)/2;

if ((rect.lower_right.x-rect.upper_left.x)%2 == 1)
center_x += .5;
if ((rect.upper_left.y-rect.lower_right.y)%2 == 1)
center_y += .5;

printf("The center point of rectangle r is : "
"(%3.2f,%3.2f)\n",center_x,center_y);

}

void move_r(struct rectangle rect, int x, int y)
{
printf("The new coordinates of moved rectangle:\n");
printf("Upper left point  = (%d,%d)\n",rect.upper_left.x+x,
rect.upper_left.y+y);
printf("Upper right point = (%d,%d)\n",rect.lower_right.x+x,
rect.upper_left.y+y);
printf("Lower left point  = (%d,%d)\n",rect.upper_left.x+x,
rect.lower_right.y+y);
printf("Lower right point = (%d,%d)\n",rect.lower_right.x+x,
rect.lower_right.y+y);

return ;
}```

8. #define TRUE 1
#define FALSE 0

int is_point_within( struct rectangle r, struct point p)
{
if( p.x > r.upper_left.x && p.x < r.lower_right.x &&
p.y > r.upper_left.y && p.y < r.lower_right.y)
{
return TRUE;
}
return FALSE; // <- this happens if the above doesn't return TRUE
}

9. A couple more things:

You should definately change ALL of the int variables in your structs to floats!!!

Why? Because you need that accuracy to properly use a rectangle!

You should definately make the area and center point variables of the struct!!!

Why? Because if you don't, you will HAVE TO DECLARE new area and center point variables FOR EVERY NEW RECTANGLE!!!

So:

struct point
{
float x;
float y;
};

struct rectangle
{
float area;
struct point upper_left;
struct point lower_right;
struct point center;
}

Don't you think?

10. I got everything to work as I want it (the only function I needed as a float to work was the center_of_r function because it is the only one that can give float values. (I am not doing more than the program specifically asks for here)

What I would like to do is put in a loop so user can check more than one point to see if it lies in rectangle r (the rectangle before it is moved).

I tried a couple things; cannot get it to work right.

here's what I have (in blue); but it is causing an infinite loop.
I don't know what to change.

Code:
```#include <stdio.h>

/********************************/
/* declare structures           */
/********************************/

struct point {
int x, y;
};

struct rectangle {
struct point upper_left, lower_right;
} ;

/*********************************/
/* function prototypes           */
/*********************************/

int area_of_r(struct rectangle rect);
float center_of_r(struct rectangle rect);
void move_r(struct rectangle rect, int x, int y);
void is_point_in_r(struct rectangle rect, struct point p);

int main() {

struct rectangle r;
int area;
int move_x, move_y;
int x,y;
struct point p;

/******************************************/
/* prompt user for two points on graph to */
/* create rectangle r                     */
/******************************************/

printf( "input upper left corner of rectangle r "
"as # # : ");
scanf( "%d %d", &r.upper_left.x, &r.upper_left.y );
printf("\n");
printf( "input lower right corner of rectangle r "
"as # # : ");
scanf( "%d %d", &r.lower_right.x, &r.lower_right.y );
printf("\n");

/*******************************/
/* call function area_of_r     */
/* and print result            */
/*******************************/

area = area_of_r(r);
printf( "\nThe area of rectangle r is "
"%d\n\n", area);

/*******************************/
/* call function center_of_r   */
/*******************************/

center_of_r(r);

/**************************************/
/* prompt user to input the amount    */
/* of movement along the x and y axes */
/* to move rectangle r                */
/**************************************/

printf("Input the amount to move on the x and y axes\n");
printf("Input as # #\n");
scanf("%d %d",&move_x,&move_y);

/*******************************/
/* call function move_r        */
/*******************************/

move_r(r, move_x,move_y);

/******************************************/
/* prompt user for coordinates of a point */
/* on a x-y graph to later find if point  */
/* lies inside rectangle r                */
/******************************************/

printf("Input coordinates of a point to see if it lies\n");
printf("within rectangle.");
printf("Input as # #  :  " );
scanf("%d %d",&p.x,&p.y);

/*******************************/
/* call function is_point_in_r */
/*******************************/

is_point_in_r(r,p);

/*  not sure if this should go here or in the function at the end of program
*/
printf("To check another point give coordinates as # #\n");
printf("Input 0 0 if done : ");
scanf("%d %d",&x,&y);

while (x!=0 && y!=0)  {

is_point_in_r(r,p);
}

return 0;
}

/*************************************************/
/* function to calculate the area of rectangle r */
/*************************************************/

int area_of_r ( struct rectangle rect )
{
int area;
area = (rect.lower_right.x - rect.upper_left.x) *
(rect.upper_left.y - rect.lower_right.y);
return area;
}

/*************************************************/
/* function to find center point of rectangle r  */
/* made as float function to take into account   */
/* center could point could be inbetween two     */
/* integer values                                */
/*************************************************/

float center_of_r(struct rectangle rect)
{
float center_x, center_y;

center_x = (rect.lower_right.x + rect.upper_left.x)/2;
center_y = (rect.upper_left.y + rect.lower_right.y)/2;

if ((rect.lower_right.x-rect.upper_left.x)%2 == 1)
center_x += .5;
if ((rect.upper_left.y-rect.lower_right.y)%2 == 1)
center_y += .5;

printf("The center point of rectangle r is : "
"(%3.2f,%3.2f)\n",center_x,center_y);

}

/******************************************************/
/* function to move points of original rectangle r to */
/* new location on graph; function prints out         */
/* coordinates of new location corner points          */
/******************************************************/

void move_r(struct rectangle rect, int x, int y)
{
printf("The new coordinates of moved rectangle:\n");
printf("Upper left point  = (%d,%d)\n",rect.upper_left.x+x,
rect.upper_left.y+y);
printf("Upper right point = (%d,%d)\n",
rect.lower_right.x+x, rect.upper_left.y+y);
printf("Lower left point  = (%d,%d)\n",rect.upper_left.x+x,
rect.lower_right.y+y);
printf("Lower right point = (%d,%d)\n",
rect.lower_right.x+x,rect.lower_right.y+y);

return ;
}

/***************************************************/
/* function passes point coordinates input by user */
/* and finds if point lies within rectangle r      */
/* returns TRUE is it does; returns FALSE if not   */
/***************************************************/

void is_point_in_r(struct rectangle rect, struct point p)
{
int x,y;
if(rect.upper_left.x <= p.x && p.x <= rect.lower_right.x
&& rect.lower_right.y <= p.y && p.y <= rect.upper_left.y)
printf("TRUE\n");
else
printf("FALSE\n");

return ;
}
```

11. I tried changing the while loop, but it breaks and never lets me try another round of coordinates WHY?

Code:
```
/******************************************/
/* prompt user for coordinates of a point */
/* on a x-y graph to later find if point  */
/* lies inside rectangle r                */
/******************************************/

printf("Input coordinates of a point to see if it lies\n");
printf("within rectangle.");
printf("Input as # #  :  " );
scanf("%d %d",&p.x,&p.y);

/*******************************/
/* call function is_point_in_r */
/*******************************/

is_point_in_r(r,p);     <----------- 1st call works

while (x!=0 && y!=0)  {         <---------------- this loop is not working
printf("To check another point give coordinates as "
"# #\n");
printf("Input 0 0 if done : ");
scanf("%d %d",&x,&y);
if (x==0 && y==0)
continue;     <------ tried break; too, doesn;t work
else
is_point_in_r(r,p);

}

printf("You are DONE !!");

return 0;
}```

12. Just need a good old forever loop and a break statement. continue does not break out of loops, replace that statement with break.

Also, the code you just posted sems to be using x and y where it should be using p.x and p.y

13. Finally, finally, got this program done.
Thanks.
I caught the p.x and p.y problem...after much trial and error