# Thread: How to find the edges/border of a rect? (java.awt)

1. Member Join Date
Jul 2011
Posts
13
Rep Power
0

## How to find the edges/border of a rect? (java.awt)

So, i made an rect constructor, and made the rect object, but i can't get the math right, on how to find the edges/border of it. I need to "detect" when something hits one of the four egdes. But my code makes both objects unable to move at all.

I got this:
Java Code:
```	public boolean toPaddle(){

speedx = (rand.nextInt(2)+1)-4;
return true;

}
return false;
}```
and the methods for my paddle egdes:

Java Code:
```	public int getX(){
return p_width;
}

public int getY(){
return p_height;
}

public int getWidth(){

int w = x_pos + p_width;
return w;
}

public int getHeight(){

int h = y_pos + p_height;
return h;
}```
Regards Naxix  Reply With Quote

2. ## Re: How to find the edges/border of a rect? (java.awt)

The code for the paddle edges is incorrect. They should return x_pos, y_pos, x_pos+p_width and y_pos+p_height respectively.

kind regards,

Jos  Reply With Quote

3. ## Re: How to find the edges/border of a rect? (java.awt)

To detect if something hits a box you want to determine if the point lays on the box. Given some rectangle with an x and a y coordinate, the points which lie on the rectangle are as follows:
Java Code:
```(x, y), (x+1, y), (x+2, y)... (x+width, y)
(x, y+1), (x, y+2)... (x, y+height)
(x+width, y), (x+width, y+1), (x+width, y+2)... (x+width, y+height)
(x, y+height), (x+1, y+height), (x+2, y+height)... (x+width, y+height)```
The easiest way to determine if something hits is to check if the point is in range.

Java Code:
```if point is in range (x, x+width)
determine whether the points y is close to top or bottom
if point is in range (y, y+height)
determine whether the  points x is close to left or right```
I wish I hadn't been lazy writing my tutorial, it would probably help you out with this.

My best advice is to visualize the box and the (x,y) point you want to check and figure it out by hand.  Reply With Quote

4. Member Join Date
Jul 2011
Posts
13
Rep Power
0

## Re: How to find the edges/border of a rect? (java.awt) Originally Posted by sunde887 To detect if something hits a box you want to determine if the point lays on the box. Given some rectangle with an x and a y coordinate, the points which lie on the rectangle are as follows:
Java Code:
```(x, y), (x+1, y), (x+2, y)... (x+width, y)
(x, y+1), (x, y+2)... (x, y+height)
(x+width, y), (x+width, y+1), (x+width, y+2)... (x+width, y+height)
(x, y+height), (x+1, y+height), (x+2, y+height)... (x+width, y+height)[```
The easiest way to determine if something hits is to check if the point is in range.

Java Code:
```if point is in range (x, x+width)
determine whether the points y is close to top or bottom
if point is in range (y, y+height)
determine whether the  points x is close to left or right```
I wish I hadn't been lazy writing my tutorial, it would probably help you out with this.

My best advice is to visualize the box and the (x,y) point you want to check and figure it out by hand.
So, how would i make the if-statement check if the object is in range? Since it's a square, i'm not sure how to do it, but i guess something with math.sqrt, would be involve?

Regards Naxix
Last edited by Naxix; 10-17-2011 at 07:51 PM.  Reply With Quote

5. ## Re: How to find the edges/border of a rect? (java.awt) Originally Posted by Naxix So, how would i make the if-statement check if the object is in range? Since it's a square, i'm not sure how to do it, but i guess something with math.sqrt, would be involve?

Regards Naxix
Don't complicate matter more than necessary; if you hav a rectangle defined by xl, xh, yl and yh (the x and y coordinates of the corners) and a point x,y, that point is inside the rectangle if xl <= x <= xh and yl <= y <= yh. You should be able to implement that in Java ....

kind regards,

Jos  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•