Jump to content

Recommended Posts

Posted

Define and implement a function which takes an input of two rectangles A and B and returns a boolean value which indicates if the two rectangles overlap or not (i.e. it should return 'true' if they overlap and ‘false’ otherwise). Both input rectangles should be defined as 4 integers each where the first two integers are representing the 'x' and 'y' coordinates of the lower left point followed by two integers which represent the 'width' and 'height' of the rectangle. The rectangles are considered overlapping if they have at least one common point.

 

Input:

Wrap your function defined and implemented above into a program which should read a line from standard input and write the result on the standard output. Your program should read from standard input 8 integers separated by spaces. The first four will be the definition of rectangle A and the second four will be the definition of rectangle B (both as per the definition in the description - i.e. 'x', 'y', 'width' and 'height').

Output:

For each line in the input parse the inputs into the rectangles, invoke the function defined in the description and then print a line to standard output with the result as a string (i.e. print 'true' if A and B intersect and 'false' otherwise).

 
questions:
 
Test1
Input: 0 0 1 1 -1 -1 1 1
expected Output: true
 
 
Test2:
Input: 1 1 1 1 -1 -1 2 1
expected output: false
 
Test3
Input: 1 1 1 1 -1 -1 3 1
 expected  output:false
 
Test4:
Input:1 1 1 1 -1 -1 3 3
expected Output:true

 

 

Posted

question ardham aindha neeku?

Posted

{

 

if

No Java = True

 

Print("Learn Java")

 

else

 

Print ('Practice Java")

 

}

Posted

naku java radu....nenu veera tech meeda working....my brother entry level job search chestundu....vallu ee test pettaru....

 

question ardham aindha neeku?

 

 

{

 

if

No Java = True

 

Print("Learn Java")

 

else

 

Print ('Practice Java")

 

}

 

Posted

Algoirthm for your help-- 

our main objective is to determine all the co-ordinates of rectangles first with given values.

then if any of the 4 co-ordinates of one rectangle (A) equals any of the4 co-ordinates the other rectangle(. B.)..

then PRINT TRUE(which means Overalpping ani)

 

test1-

co-ordinates of rectangle A are - (0,0), (0,1) (1,0) (1,1)

co-ordinates of rectangle B are - (-1,1), (1,-1) (-1,-0) (1,1)

 

since there are co-ordinates that match ..hence they overlap hence Answer should be TRUE

Posted

//returns true when intersection is found, false otherwise.
//when returning true, rectangle 'out' holds the intersection of r1 and r2.
private static boolean intersection2(Rectangle r1, Rectangle r2,
Rectangle out) {
float xmin = Math.max(r1.x, r2.x);
float xmax1 = r1.x + r1.width;
float xmax2 = r2.x + r2.width;
float xmax = Math.min(xmax1, xmax2);
if (xmax > xmin) {
float ymin = Math.max(r1.y, r2.y);
float ymax1 = r1.y + r1.height;
float ymax2 = r2.y + r2.height;
float ymax = Math.min(ymax1, ymax2);
if (ymax > ymin) {
out.x = xmin;
out.y = ymin;
out.width = xmax - xmin;
out.height = ymax - ymin;
return true;
}
}
return false;
}

Posted

try chestundu...ee code thoni...to get the results

//returns true when intersection is found, false otherwise.
//when returning true, rectangle 'out' holds the intersection of r1 and r2.
private static boolean intersection2(Rectangle r1, Rectangle r2,
Rectangle out) {
float xmin = Math.max(r1.x, r2.x);
float xmax1 = r1.x + r1.width;
float xmax2 = r2.x + r2.width;
float xmax = Math.min(xmax1, xmax2);
if (xmax > xmin) {
float ymin = Math.max(r1.y, r2.y);
float ymax1 = r1.y + r1.height;
float ymax2 = r2.y + r2.height;
float ymax = Math.min(ymax1, ymax2);
if (ymax > ymin) {
out.x = xmin;
out.y = ymin;
out.width = xmax - xmin;
out.height = ymax - ymin;
return true;
}
}
return false;
}

 

Posted
public void RectangleOverlap_Util()
        {
            for (int i = 0; i < 5; i++)
            {

                var line = Console.ReadLine();
                var numbers = line.Split(' ');
                int rec1_x = Convert.ToInt32(numbers[0]);
                int rec1_y = Convert.ToInt32(numbers[1]);
                int rec2_x = Convert.ToInt32(numbers[4]);
                int rec2_y = Convert.ToInt32(numbers[5]);

                //from other given inputs find the upper right corner(why? if we have extreme points
                //we check if the value(x or y) is greater than the points of other rectangle

                int rec1_x_1 = rec1_x +Convert.ToInt32(numbers[2]);
                int rec1_y_1 = rec1_y + Convert.ToInt32(numbers[3]);
                int rec2_x_1 = rec2_x + Convert.ToInt32(numbers[6]);
                int rec2_y_1 = rec2_y +Convert.ToInt32(numbers[7]);
                bool ret = RectangleOverlapCheck(rec1_x, rec1_y, rec2_x, rec2_y, rec1_x_1, rec1_y_1, rec2_x_1, rec2_y_1);
                Console.WriteLine(ret);
            }

            
        }
        public bool RectangleOverlapCheck(int rec1_x, int rec1_y, int rec2_x, int rec2_y, int rec1_x_1, int rec1_y_1, int rec2_x_1, int rec2_y_1)
        {
            //just check if they over lap
            // 2 cases they may be side by side or up and down

            //side by side case

            if (rec1_x_1 < rec2_x || rec2_x_1 < rec1_x)
                return false;

            //up or down
            if (rec1_y > rec2_y_1 || rec1_y_1 < rec2_y)
                return false;

            return true;
        }
Posted

rec1_x: Rectangle 1 lower left corner x coordinate

 rec1_y: Rectangle 1 lower left corner y coordinate  

rec1_x_1: Rectangle 1 upper right corner x coordinate 

 rec1_y_1: Rectangle 1 upper right corner y coordinate  

 

 

rec2_x: Rectangle 2 lower left corner x coordinate 

 rec2_y: Rectangle 2 lower left corner x coordinate  

rec2_x_1: Rectangle 2 upper right corner x coordinate 

 rec2_y_1: Rectangle 2 upper right corner y coordinate  

Posted
can you please check below code:
 
TEST1 result okkati false ostundi.....
 
 
public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String s= in.readLine() ;
   String b[]=s.split(" ");
   int a[] =new int[8];
 
 for(int i=0;i<a.length;i++)
  a[i]=Integer.parseInt(b[i]);
   Rectangle r1 = new Rectangle(a[0],a[1],a[2],a[3]);
  Rectangle r2 = new Rectangle(a[4],a[5],a[6],a[7]);
  System.out.println(intersection2(r1,r2));
}
 
  private static boolean intersection2(Rectangle r1, Rectangle r2
      ) {
      float xmin = Math.max(r1.x, r2.x);
      float xmax1 = r1.x + r1.width;
      float xmax2 = r2.x + r2.width;
      float xmax = Math.min(xmax1, xmax2);
      if (xmax> xmin) {
          float ymin = Math.max(r1.y, r2.y);
          float ymax1 = r1.y + r1.height;
          float ymax2 = r2.y + r2.height;
          float ymax = Math.min(ymax1, ymax2);
          if (ymax > ymin) {
                   return true;
          }
      }
      return false;
  }
Posted

na code ani correct reuslts vastunnayi

 

can you please check below code:
 
TEST1 result okkati false ostundi.....
 
 
public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String s= in.readLine() ;
   String b[]=s.split(" ");
   int a[] =new int[8];
 
 for(int i=0;i<a.length;i++)
  a[i]=Integer.parseInt(b[i]);
   Rectangle r1 = new Rectangle(a[0],a[1],a[2],a[3]);
  Rectangle r2 = new Rectangle(a[4],a[5],a[6],a[7]);
  System.out.println(intersection2(r1,r2));
}
 
  private static boolean intersection2(Rectangle r1, Rectangle r2
      ) {
      float xmin = Math.max(r1.x, r2.x);
      float xmax1 = r1.x + r1.width;
      float xmax2 = r2.x + r2.width;
      float xmax = Math.min(xmax1, xmax2);
      if (xmax> xmin) {
          float ymin = Math.max(r1.y, r2.y);
          float ymax1 = r1.y + r1.height;
          float ymax2 = r2.y + r2.height;
          float ymax = Math.min(ymax1, ymax2);
          if (ymax > ymin) {
                   return true;
          }
      }
      return false;
  }

 

 

Posted

na code ani correct reuslts vastunnayi

thanks mama....time ayepoendi anta...

×
×
  • Create New...