write a C program that Creates a pointer structure that has members for the x an
ID: 3676704 • Letter: W
Question
write a C program that Creates a pointer structure that has members for the x and y coordinates. Use typedef to make a point_t data type. Create a rectangle structure that has a point_t member for the upper left corner of the rectangle, but also a float for the height and a float for the width. Use typedef to make a rectangle_t data type. Create a function called is_in() that takes in a point_t variable and a rectangle_t variable, both by reference, and returns 0 if the point is not in the rectangle and 1 if the point is in the rectangle. Write a program that gets a point from the user and the point for the upper left corner and dimensions of a rectangle and then tells whether the point is in the rectangle or not.
Sample Runs:
Enter a point (x and y): 0.0 0.0
Enter a rectangle (x, y, width, and height): -50.0 -50.0 100.0 100.0
Point is inside the rectangle
Enter a point (x and y): 200.0 200.0
Enter a rectangle (x, y, width, and height): -50.0 -50.0 100.0 100.0
Point is outside the rectangle
Explanation / Answer
#include <stdio.h>
typedef struct
{
int x;
int y;
} point_t;
typedef struct
{
point_t upper_left;
float height,width;
} rectangle_t;
int is_in(point_t* pPoint, rectangle_t* pRect)
{
// check if point is inside or not
if((pRect->upper_left.x+pRect->width >= pPoint->x)&&(pRect->upper_left.x <= pPoint->x) && (pRect->upper_left.y <= pPoint->y) && (pRect->upper_left.y+pRect->height >= pPoint->y) )
return true;
else return false;
}
point_t get_point()
{
point_t pt;
printf ("X: ");
scanf ("%d", &pt.x);
printf ("Y: ");
scanf ("%d", &pt.y);
return pt;
}
int main()
{
rectangle_t rect;
printf("Please enter upper left point coordinates of the rectangle ");
rect.upper_left = get_point();
printf("Please enter height and width of the rectangle ");
printf("Height: ");
scanf("%f", &rect.height);
printf("Width: ");
scanf("%f", &rect.width);
printf("Please coordinates of the point ");
point_t pt=get_point();
if(is_in(&pt, &rect))
printf("Point lies inside rectangle ");
else
printf("Point lies outside rectangle ");
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.