10 Replies - 12802 Views - Last Post: 14 October 2009 - 04:39 PM Rate Topic: -----

#1 winterwind   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 22-September 09

writing own .contains method for rectangles

Posted 13 October 2009 - 02:29 PM

Hi,

I am having trouble with this problem:

You might notice the “contains” method that comes with the java.awt.Rectangle class we’ve been using returns false if the point is on the lower or right edge of the Rectangle (for example the program will refuse to deliver to a customer at (1,8) or (4,2) even though these points are in store 1’s range. Instead of using the Rectangle’s “contains” method, write your own method that takes a Rectangle, a x value, and a y value, and returns true (and false otherwise) if the point (x,y) is located inside or on ANY edge of the Rectangle parameter. Use your new method in the program wherever you used the Rectangle’s “contains” method before.

So basically I have to write a method that will return true or false when I input a rectangle, and a point.

I know that Rectangle store will have a starting point (upper left corner), and a certain width and height. So the input of x and y will have to fall inside of these constraints of this rectangle.

In general, if (a, b ) represents the upper left corner of the rectangle, respectively, and (c,d) represents the height and width of the rectangle, respectively, point (x,y) lies in (or on the border) of the rectangle if and only if:

a <= x <= a + c

AND

b <= y <= b + d

(Please correct me on the above logic if I am indeed incorrect. That is just my approximation.)

I am having trouble coding this. I'm not sure how to extract the (a,b ) [upper left corner coordinates] and (c, d) [height and width] from the input rectangle. Would this even be the right approach?

Any assistance would be much appreciated!

This post has been edited by winterwind: 13 October 2009 - 02:33 PM


Is This A Good Question/Topic? 0
  • +

Replies To: writing own .contains method for rectangles

#2 theautokustomizer   User is offline

  • D.I.C Regular

Reputation: 16
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: writing own .contains method for rectangles

Posted 13 October 2009 - 02:42 PM

We have a policy here at D.I.C., we need to see what you have to this point in code. Thank you.
Was This Post Helpful? 0
  • +
  • -

#3 winterwind   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 22-September 09

Re: writing own .contains method for rectangles

Posted 13 October 2009 - 03:30 PM

This is the code for what I basically explained above:

 
public static boolean newContains(Rectangle(a, b, c, d), int x, int y)
{
  if (a <= x <= (a + c) && b <= y <= (b + d))
{
  return true;
}
  else
{
  return false;
}
}


Am I heading in the right direction? Is this how I would write a method for use with the rectangle object?

This post has been edited by winterwind: 13 October 2009 - 03:33 PM

Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: writing own .contains method for rectangles

Posted 13 October 2009 - 06:39 PM

You cannot pass a parameter like that

public static boolean newContains(Rectangle(a, b, c, d), int x, int y);

Assuming that you have a class like that

class Rectangle {
	int a, b, c, d;

	// constructor
	Rectangle(int x, int y, int width, int height) {
		this.x = x;
		this.y = y;
		c = width;
		d = height;
	}

	// then your Rectangle can have a contain method that uses your logic
	public boolean newContains(int x, int y)
	{
		  return (a <= x <= (a + c) && b <= y <= (b + d))
	 }
}


Was This Post Helpful? 0
  • +
  • -

#5 winterwind   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 22-September 09

Re: writing own .contains method for rectangles

Posted 13 October 2009 - 08:25 PM

View Postpbl, on 13 Oct, 2009 - 05:39 PM, said:

You cannot pass a parameter like that

public static boolean newContains(Rectangle(a, b, c, d), int x, int y);

Assuming that you have a class like that

class Rectangle {
	int a, b, c, d;

	// constructor
	Rectangle(int x, int y, int width, int height) {
		this.x = x;
		this.y = y;
		c = width;
		d = height;
	}

	// then your Rectangle can have a contain method that uses your logic
	public boolean newContains(int x, int y)
	{
		  return (a <= x <= (a + c) && b <= y <= (b + d))
	 }
}




It says that it can't find the variable x when I try to compile this...?
Was This Post Helpful? 0
  • +
  • -

#6 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: writing own .contains method for rectangles

Posted 13 October 2009 - 08:30 PM

Sorry... I went to fast

   // constructor
	Rectangle(int x, int y, int width, int height) {
		a = x;
		b = y;
		c = width;
		d = height;
	}


Was This Post Helpful? 0
  • +
  • -

#7 winterwind   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 22-September 09

Re: writing own .contains method for rectangles

Posted 13 October 2009 - 09:18 PM

View Postpbl, on 13 Oct, 2009 - 07:30 PM, said:

Sorry... I went to fast

   // constructor
	Rectangle(int x, int y, int width, int height) {
		a = x;
		b = y;
		c = width;
		d = height;
	}



Thanks. It still doesn't compile correctly though. The compiler faults this line:

return (a <= x <= (a + c) && b <= y <= (b + d));

This error message is returned with the above line highlighted:

operator <= cannot be applied to boolean,int

Is <= (or >= or > or <, etc., etc.) not allowed in booleans??
Was This Post Helpful? 0
  • +
  • -

#8 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: writing own .contains method for rectangles

Posted 13 October 2009 - 09:26 PM

Right... you cannot 2 test 2 conditions in Java using

x < y < 10

you have to separate the test and use the and && operator

This should do the trick

return ((a <= x && x <= (a + c)) && (b <= y && y <= (b + d)));
Was This Post Helpful? 1
  • +
  • -

#9 winterwind   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 22-September 09

Re: writing own .contains method for rectangles

Posted 13 October 2009 - 09:55 PM

Thanks! Works like a charm. :)
Was This Post Helpful? 0
  • +
  • -

#10 alunduil   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 13-October 09

Re: writing own .contains method for rectangles

Posted 14 October 2009 - 12:19 PM

View Postwinterwind, on 13 Oct, 2009 - 02:30 PM, said:

 
public static boolean newContains(Rectangle(a, b, c, d), int x, int y)
{
  if (a <= x <= (a + c) && b <= y <= (b + d))
{
  return true;
}
  else
{
  return false;
}
}


Try this instead:

static class NewRectangle extends Rectangle {
  public static boolean NewContains(int x, int y) {
	return (this.x <= x <= (this.width + this.x) && this.y <= y <= (this.height + this.y));
  }
}



Sorry this response took so long. My computer has been having some difficulties.

What this does is inherits from the default Rectangle class so we have all of the functionality of it and then simply create our own function for checking if a point (specified with <x, y>) is in the rectangle. Any questions post back.

Regards,

Alunduil

This post has been edited by alunduil: 14 October 2009 - 12:20 PM

Was This Post Helpful? 0
  • +
  • -

#11 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: writing own .contains method for rectangles

Posted 14 October 2009 - 04:39 PM

View Postalunduil, on 14 Oct, 2009 - 11:19 AM, said:

View Postwinterwind, on 13 Oct, 2009 - 02:30 PM, said:



Try this instead:

static class NewRectangle extends Rectangle {
  public static boolean NewContains(int x, int y) {
	return (this.x <= x <= (this.width + this.x) && this.y <= y <= (this.height + this.y));
  }
}



Sorry this response took so long. My computer has been having some difficulties.

What this does is inherits from the default Rectangle class so we have all of the functionality of it and then simply create our own function for checking if a point (specified with <x, y>) is in the rectangle. Any questions post back.

Regards,

Alunduil


Still wrong.. you need the && and there is no this.x :)

Actually, in a problem like that,I prefer to eliminate the wrong candidates instead of putting a lot of &&

boolean contains(int x, int y) {
   if(x < a)
	  return false;
   if(x > a + c)
	  return false;
   if(y < b)
	  return false;
   if(y > b + d)
	  return false;
   // ok I am in
   return true;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1