Question

In: Computer Science

REQUIREMENTS: Write a function that matches the following declaration: int InRectangle( float pt[2], float rect[4] );...

REQUIREMENTS:

  1. Write a function that matches the following declaration:
    int InRectangle( float pt[2], float rect[4] );
  2. Argument pt[2] defines a point on the plane: pt[0] is the x-coordinate, pt[1] is the y-coordinate.
  3. Argument rect[4] defines a rectangle on the same plane. rect[0] and rect[1] define the x- and y- cordinates respectively of one corner of the rectangle. rect[2] and rect[3] define the opposite corner.
  4. Coordinates may be any valid floating point value, including negative values.
  5. The function returns int 0 (false) for any point that lies outside the rectangle, and 1 (true) for any other point (i.e. points inside and on the boundary of the rectangle).

TESTS:

  1. // declaration of function to test
  2. int InRectangle( float pt[2], float rect[4] );
  3. int main( int argc, char* argv[] )
  4. {
  5. // define a rectangle from (1,1) to (2,2)
  6. float rect[4] = {1.0, 1.0, 2.0, 2.0 };
  7. // define a point that is inside the rectangle
  8. float p_in[2] = { 1.5, 1.5 };
  9. // define a point that is outside the rectangle
  10. float p_out[2] = {2.5, 0.5};
  11. // define a point that is on the edge of the rectangle
  12. float p_edge[2] = {1.0, 1.0};
  13. // InRectangle() should return 0 (false) for points that are NOT in
  14. // the rectangle, and non-zero (true) for points that are in the
  15. // rectangle. Points on the edge are considered *in* the rectangle.
  16. // test 1
  17. if( InRectangle( p_in, rect ) == 0 )
  18. {
  19. puts( "error: should return true for p_in." );
  20. return 1; // indicate error
  21. }
  22. // test 2
  23. if( InRectangle( p_out, rect ) != 0 )
  24. {
  25. puts( "error: should return false for p_out." );
  26. return 1; // indicate error
  27. }
  28. // test 3
  29. if( InRectangle( p_edge, rect ) == 0 )
  30. {
  31. puts( "error: should return true for p_edge." );
  32. return 1; // indicate error
  33. }
  34. return 0; // all tests passed
  35. }

Solutions

Expert Solution

Here iam providing the answer hope this helps you.

If you have any doubts comment me i will clarify you.Please give me a like that helps me a lot.Thank you

C Code:-

// declaration of function to test
#include<stdio.h>
int InRectangle( float pt[2], float rect[4] );
int main( int argc, char* argv[] ){
        // define a rectangle from (1,1) to (2,2)
        float rect[4] = {1.0, 1.0, 2.0, 2.0 };
        // define a point that is inside the rectangle
        float p_in[2] = { 1.5, 1.5 };
        // define a point that is outside the rectangle
        float p_out[2] = {2.5, 0.5};
        // define a point that is on the edge of the rectangle
        float p_edge[2] = {1.0, 1.0};
        // InRectangle() should return 0 (false) for points that are NOT in
        // the rectangle, and non-zero (true) for points that are in the
        // rectangle. Points on the edge are considered *in* the rectangle.
        // test 1
        if( InRectangle( p_in, rect ) == 0 )
                {
                puts( "error: should return true for p_in." );
                return 1; // indicate error
                }
        // test 2
        if( InRectangle( p_out, rect ) != 0 )
                {
                puts( "error: should return false for p_out." );
                return 1; // indicate error
                }
        // test 3
        if( InRectangle( p_edge, rect ) == 0 )
                {
                puts( "error: should return true for p_edge." );
                return 1; // indicate error
                }
        return 0; // all tests passed
}
int InRectangle( float pt[2], float rect[4] ){
        //here we are comparing the values of point cordinates and diagonal cordinates
        if((pt[0] >= rect[0] && pt[0] <= rect[2] && pt[1] >= rect[1] and pt[1] <= rect[3]) ||(pt[0] <= rect[0] && pt[0] >= rect[2] && pt[1] <= rect[1] and pt[1] >= rect[3]))
                //if it enters into this then it is inside the rectangle or one the edge
        return 1;
    else 
        //it is for outside the rectangle
        return 0;
}

Output:-

Here it satisifies the all the conditions that's why it doesn't give any error.


Related Solutions

C Practice 1: 1) Write a forward declaration for the following C function int triple_it (int...
C Practice 1: 1) Write a forward declaration for the following C function int triple_it (int x) { return (x * 3); } 2) What is C syntax to declare two variables, one called num of integer type and another called farray which is an array of 10 floating point numbers? 3) Write a C function array_max(int a[], int len) that takes an integer array & its length as its parameters and returns the largest value in the array. 4)...
Write a function called is_valid_phone_number matches that takes two int arrays and their respective sizes, and...
Write a function called is_valid_phone_number matches that takes two int arrays and their respective sizes, and returns the number of consecutive values that match between the two arrays starting at index 0. Suppose the two arrays are {3, 2, 5, 6, 1, 3} and {3, 2, 5, 2, 6, 1, 3} then the function should return 3 since the consecutive matches are for values 3, 2, and 5. in C++ with explanations
int main() { float A[4] = { 0.51, 1.23, 7.4, 10.88}; float B[4] = { -11.1,...
int main() { float A[4] = { 0.51, 1.23, 7.4, 10.88}; float B[4] = { -11.1, 78.044, 12.009, -9.99}; float s = 0.0; for (int i=0; i<4; i++) { s = s + A[i]*B[i]; } } change this c languange to arm assembly languange extension of vfp registor
Write a Haskell function combine :: Int -> Int -> Int -> Int with the following...
Write a Haskell function combine :: Int -> Int -> Int -> Int with the following behavior: • When x, y, and z all correspond to digit values (i.e., integers between 0 and 9, inclusive), combine x y z returns the integer given by the sequence of digits x y z. (That is, x is treated as the digit in the hundreds place, y is treated as the digit in the tens place, and z is treated as the digit...
Which of the following pairs of coordination complexes are linkage isomers? a.[Pt(Cl)2(SCN)4]4− and [Pt(Cl)2(NCS)4]4− b.[Pt(Cl)2(SCN)4]4− and...
Which of the following pairs of coordination complexes are linkage isomers? a.[Pt(Cl)2(SCN)4]4− and [Pt(Cl)2(NCS)4]4− b.[Pt(Cl)2(SCN)4]4− and [Pt(Cl)4(SCN)2]4− c.K4[Pt(Cl)2(SCN)4] and Na4[Pt(Cl)2(SCN)4]
4, Make the table project with C++. Write a function with the following interface: void multiplyTable(int...
4, Make the table project with C++. Write a function with the following interface: void multiplyTable(int num) This function should display the multiplication table for values from 1...num. For example, if the function is passed 10 when it is called, it should display the following: 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20...
2. Let the function fun be defined as int fun(int*k) {       *k += 4;       return...
2. Let the function fun be defined as int fun(int*k) {       *k += 4;       return 3 * (*k) - 1; } Suppose fun is used in a program as follows: void main() {       int i = 10, j = 10, sum1, sum2;       sum1 = (i / 2) + fun(&i);       sum2 = fun(&j) + (j / 2); } What are the values of sum1 and sum2 a. operands in the expressions are evaluated left to right? b. operands...
Suppose A is (10, 2, 5, 9, 1, 8, 2, 4). Consider the function: int BBOX(int...
Suppose A is (10, 2, 5, 9, 1, 8, 2, 4). Consider the function: int BBOX(int n, int k)             if (n <= 0) return 0;             else if (A[n] < k) return (1+ 2*BBOX(n-1,k+1));             else return BBOX(n-1,k-2);             Find BBOX(8, 5)
"sum_between" function Write a function named "sum_between" that receives 2 parameters - "start" (an int) and...
"sum_between" function Write a function named "sum_between" that receives 2 parameters - "start" (an int) and "end" (an int). It should return the sum (total) of all of the integers between (and including) "start" and "end". If "end" is less than "start", the function should return -1 instead. e.g. if you give the function a start of 10 and an end of 15, it should return 75 (i.e. 10+11+12+13+14+15)
Given the following processing array declaration with initialisation: int[][] foo = {{1,2,3,4,5},{2,3,4,5,6},{3,4,5,6,7},{4,5,6,7,8}}; Write a loop that...
Given the following processing array declaration with initialisation: int[][] foo = {{1,2,3,4,5},{2,3,4,5,6},{3,4,5,6,7},{4,5,6,7,8}}; Write a loop that will modify  foo so it instead contains the values {{1,4,9,16,25},{4,9,16,25,36},{9,16,25,32,49},{16,25,32,49,64}}.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT