Question

In: Computer Science

Let R and S be two rectangles in the xy-plane whose sides are parallel to the coordinate axes.

 c programming 
Objectives:
Learn about conditional statements and Boolean operators.
Problem:
Let R and S be two rectangles in the xy-plane whose sides are parallel to the coordinate axes. Each rectangle is described by its center and its height and width. Determine if R and S overlap.
Example:
1) Suppose R is the rectangle centered at the origin height and width equal to one and S is the rectangle centered at the point (1,1) with a height equal to 2 and width equal to 3. Then L and S overlap.

2) Suppose R is the rectangle centered at the origin height and width equal to one and S is the rectangle centered at the point (100,100) with a height equal to 2 and width equal to 3. Then L and S do not overlap.

Bonus:
If two given rectangles as described above overlap, their intersection forms a new rectangle. Further determine its center, height, and width

Solutions

Expert Solution

  • If two rectangles touch each other, they are not considered as overlapped.

CODE :

#include
#include

struct Rect
{
    float x;
    float y; // x,y co-ordinate of its center
    float h; // height
    float w; // width
};

int is_overlap(struct Rect a, struct Rect b)
// checks whether the two rectangles overlap or not
// by checking whether the farthest points on the rectangle cross each other or not
{
    if ( a.x - a.w/2 >= b.x + b.w/2 )    // a is on the right side of b
        return 0;

    if ( a.x + a.w/2 <= b.x - b.w/2 )    // a is on the left side of b
        return 0;

    if ( a.y - a.h/2 >= b.y + b.h/2 )    // a is above b
        return 0;

    if ( a.y + a.h/2 <= b.y - b.h/2 )    // a is below b
        return 0;

    return 1;
}

float max(float a, float b)
{
    if(a>b)
        return a;
    else
        return b;
}

float min(float a, float b)
{
    if(a         return a;
    else
        return b;
}

void print_overlap(struct Rect a, struct Rect b)
{
    float left = max(a.x - a.w/2, b.x - b.w/2);     // left is the x co-ordinate of the left most point of the rectangle
    float down = max(a.y - a.h/2, b.y - b.h/2);     // down is the y co-ordinate of the lowest most point of the rectangle
    float right = min(a.x + a.w/2, b.x + b.w/2 );   // right is the x co-ordinate of the right most point of the rectangle
    float top = min(a.y + a.h/2, b.y + b.h/2);      // top is the y co-ordinate of the top most point of the rectangle

    struct Rect c;      // overlap rectangle
    c.x = (left + right)/2;
    c.y = (top + down)/2;
    c.h = top - down;
    c.w = right - left;
    printf("\n\nOverLapped Rectangle : ");
    printf("\nCenter : %.2f %.2f\n", c.x, c.y);
    printf("Height : %.2f\nWidth : %.2f", c.h, c.w);
}

int main()
{
    struct Rect a;
    struct Rect b;
    printf("Enter the center, the height, the width of first rectangle : ");
    scanf("%f %f %f %f", &(a.x), &(a.y), &(a.h), &(a.w));
    printf("Enter the center, the height, the width of second rectangle : ");
    scanf("%f %f %f %f", &(b.x), &(b.y), &(b.h), &(b.w));
    if(is_overlap(a,b) == 1)
    {
        printf("\nBoth the rectangles overlap");
        print_overlap(a,b);
    }
    else
    {
        printf("\nBoth the rectangles don't overlap");
    }
    return 0;
}

Output :


Related Solutions

Let R and S be two rectangles in the xy-plane whose sides are parallel to the coordinate axes. Each rectangle is described by its center and its height and width. Determine if R and S overlap.
 C LANGUAGEObjectives:Learn about conditional statements and Boolean operators.Problem:Let R and S be two rectangles in the xy-plane whose sides are parallel to the coordinate axes. Each rectangle is described by its center and its height and width. Determine if R and S overlap.Example:1) Suppose R is the rectangle centered at the origin height and width equal to one and S is the rectangle centered at the point (1,1) with a height equal to 2 and width equal to 3. Then...
Let R be the two-dimensional region in the first quadrant of the xy- plane bounded by...
Let R be the two-dimensional region in the first quadrant of the xy- plane bounded by the lines y = x and y = 3x, and by the hyperbolas xy = 1 and xy = 3. Let (x,y) = g(u,v) be the two-dimensional transformation of the first quadrant defined by x = u/v, y = v. a) Compute the inverse transformation g−1. b) Draw the region R in the xy-plane and the region g−1(R) in the uv-plane c) Use the...
A water molecule is placed in the xy plane of a coordinate system with oxygen in...
A water molecule is placed in the xy plane of a coordinate system with oxygen in origin, such that each H atom has the same distance to the x-axis. The distance between oxygen and hydrogen is 0.958 Å and the angle between the three atoms is 104.48. The three atoms are considered point masses and the molecule is assumed to be a rigid body. The mass of hydrogen is 1.674*10^-27 kg, the mass of oxygen is 2.657*10^-26 kg, the mass...
5. A loop of wire (whose plane is in the xy plane), is moving in the...
5. A loop of wire (whose plane is in the xy plane), is moving in the +x direction through a uniform B-field (5.0 T, directed into the xy plane...ie., along the -z axis). What is direction of the induced current in the loop? a. Clockwise when viewed from the front b. Counterclockwise when viewed from the front c. The current direction alternates back and forth in the loop d. There is no induced current in the loop 6. Refer to...
Consider a region R bound by the coordinate axes and y = ( 9 + x...
Consider a region R bound by the coordinate axes and y = ( 9 + x 2 ) − 1 2 on 0 ≤ x ≤ 4. a. Find the area of R. b. Suppose R is revolved about the x-axis to form a solid. Find the volume of the solid. c. Suppose R is revolved about the y-axis to form a solid. Find the volume of the solid.
1. Consider the spiral S in the xy-plane given in polar form by r = e...
1. Consider the spiral S in the xy-plane given in polar form by r = e ^θ . (a) (5 points) Find an arc length parametrization for S for which the reference point corresponds to θ = 0. (b) (5 points) Compute the curvature of S at the point where θ = π.
Let S be the surface with equation x2+y2-z2=1. (a) In a single xy-plane, sketch and label...
Let S be the surface with equation x2+y2-z2=1. (a) In a single xy-plane, sketch and label the trace curves z=k for k = -2,-1,0,1,2. In words describe what types of curves these are and how they change. (b) In a single yz-plane, sketch and label the trace curves x=k for k= -2,-1,0,1,2. In other words, describe what types of curves these are and how they change as k varies.
Three long wires are all parallel to each other and are all in the xy plane....
Three long wires are all parallel to each other and are all in the xy plane. Wire 1 runs along the y axis and carries a current of 1.64 A in the +y direction. Wire 2 runs along the x = 25.0 cm line and carries a current of 4.04 A in the −y direction. (a) Determine the location of wire 3 on the x axis so that when it carries a certain current (to be determined later) each wire...
Define our “universe” S as the region in the xy-plane that is described by S =...
Define our “universe” S as the region in the xy-plane that is described by S = {(x, y) : |x| + |y| ≤ 2 and y > −1} . (a) Mathematically and graphically describe three sets A1, A2, and A3 taken from S that are mutually exclusive but not collectively exhaustive. (b) Mathematically and graphically describe three sets B1, B2, and B3 taken from S that are collectively exhaustive but not mutually exclusive. (c) Mathematically and graphically describe three sets...
a) Let D be the disk of radius 4 in the xy-plane centered at the origin....
a) Let D be the disk of radius 4 in the xy-plane centered at the origin. Find the biggest and the smallest values of the function f(x, y) = x 2 + y 2 + 2x − 4y on D. b) Let R be the triangle in the xy-plane with vertices at (0, 0),(10, 0) and (0, 20) (R includes the sides as well as the inside of the triangle). Find the biggest and the smallest values of the function...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT