The Artima Developer Community
Sponsored Link

Java Answers Forum
boolean

3 replies on 1 page. Most recent reply: Apr 7, 2006 12:54 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
star yu

Posts: 2
Nickname: starlightt
Registered: Apr, 2006

boolean Posted: Apr 5, 2006 5:10 PM
Reply to this message Reply
Advertisement
Hey. my first post. How do you write a java class method that takes two Rectangle object as parameter and returns true if the rectangle overlape each other when given x and y corordinate, length and width for both rectangle. I know you suppose to use a boolean meathod and one way is to match up all four coordinate but is there any other way?


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: boolean Posted: Apr 5, 2006 10:22 PM
Reply to this message Reply
No, not really.

Btw, before comparing the coordinates you should check if one of the Rectangle objects is null. Return false in this case.

star yu

Posts: 2
Nickname: starlightt
Registered: Apr, 2006

Re: boolean Posted: Apr 6, 2006 7:46 PM
Reply to this message Reply
I was thinking of using only one cordinate of both Rectangle. like the given bottomLeft X AND Y of the both rectangle. and set it to a range according the height and width given.Since in math, there must be a fix distance between two bottom left corner in order for triangle to overlap no matter the position of the rectangle. but the thing is i did all the math but the only thing is i can't do it in java due to the syntax and everything else.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: boolean Posted: Apr 7, 2006 12:54 AM
Reply to this message Reply
Do they have to overlap exactly or do you want to get the region where they overlap?

You could just use the equals method of a rectangle in the first case.

return rectangle1.equals(rectangle2)

If that doesn't work for any reason just compare every parameter of rectangle.

return rectangle1.x == rectangle2.x && rectangle1.y == rectangle2.y && rectangle1.width == rectangle2.width && rectangle1.height == rectangle2.height;


2nd case: get the overlapping region.
1. Test, if there IS an overlapping region
1.1 rectangle2.x must not be bigger than rectangle1.x+rectangle1.width
1.2 rectangle1.x must not be bigger than rectangle2.x+rectangle2.width
1.3 rectangle2.y must not be bigger than rectangle1.y+rectangle1.height
1.4 rectangle1.x must not be bigger than rectangle2.y+rectangle2.height

2. find maxX, maxY,
double maxX = Math.max(rectangle1.x, rectangle2.x);
double maxY = Math.max(rectangle1.y, rectangle2.y);

double minX2 = Math.min(rectangle1.x + rectangle1.width, rectangle2.x + rectangle2.width);
double minY2 = Math.min(rectangle1.y + rectangle1.height, rectangle2.y + rectangle1.height);
double newWidth = minX2 - maxX;
double newWidth = minY2 - maxY;

new Rectangle(maxX, maxY, newWidth, newHeight);

Note: You can skip the test if you just check if newWidth or newHeight are negative. In that case return null.


Since I think that you have SOME knowledge about Java (or should get it doing this exercise), it shouldn't be much of a problem to put this in a class and to write the methods.




For your solution with just one coordinate post here your math code in pseudo-code (basic-like if you want). I can help you translate it to java.
But I can assure you that the classic way is faster.
I used 4 compare operations and 3 logical operations.
If the x-value is different, it will only be one compare operation and maybe (don't know exactly how it's implemented) one logical operation.

Flat View: This topic has 3 replies on 1 page
Topic: realated to mpxj api Previous Topic   Next Topic Topic: Microsoft Virtual Machine Problem

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use