In: Computer Science
REQUIREMENTS:
int InRectangle( float pt[2], float rect[4] );
TESTS:
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.