Rectangle2D getY() returns value less than getMaxY() - java

As I interpret the documentation, getY() is supposed to return the upper-left hand Y coordinate of a rectangle; i.e the biggest Y coordinate. However, when calling getMaxY() (which is inherited from the RectangularShape class) I get back a bigger value!
In code:
Path2D bg = polygons.get(polyId2GeoId.get(id));
Rectangle2D bgBox = bg.getBounds2D();
boolean omgwtfbbqrsvp = bgBox.getY()<bgBox.getMaxY();
omgwtfbbqrsvp is true... What am I missing here?
My x values contain negative numbers idk if that makes a difference. It sames like bgBox.getY() == bgBox.getMinY() (which is wrong if getY is upper coord) but bgBox.getX() == bgBox.getMinX() (which is correct if getX is left coord). The heights and widths appear correct.
Thanks!

The problem is that the coordinate system used here has its point (0,0) on the upper left corner. The point (n,n) is on the lower right corner.

Related

itextpdf : rectangle modify width

Is it possible to modify width of existing rectangle ?
I have :
#Override
public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text){
Rectangle rectangle = new Rectangle(rect);
//something like that :
rectangle.setWidth(400f);
}
You can (and should) not use a method called setWidth(). Whatever that method would do would be very ambiguous.
Suppose that you would have a rectangle with lower-left x coordinate equal to 36 and with upper-right x coordinate equal to 559. (I didn't choose these numbers at random: those are the default margins inside the default A4 page when using iText.) Now when you change the width of such a rectangle: do you mean to extend the rectangle to the left, to the right, or both? I hope this example shows that having a setWidth() method doesn't make sense.
Instead, you should use setLeft() or setRight() when you change the x value of the left or right coordinate of the rectangle, you automatically change the width and there can be no confusion about the direction in which you're changing the width.

Two Shape objects appear to be intersecting at the wrong location?

I am trying to create a Shape with the centre of the ship being in the middle.
one.x and one.z is the X and Z positions of the ship. The ship size is about 100 on the X, and 50 on the Z.
Shape my = new Rectangle(
(int) one.x - disToLeft, // upper-left corner X
(int) one.z - disToTop, // upper-left corner Y
disToLeft + disToRight, // width
disToTop + disToBottom // height
);
I'm then rotating the Shape, to of course be facing the correct way. This appears to work:
int rectWidth = (disToLeft + disToRight);
int rectHeight = (disToTop + disToBottom);
AffineTransform tr = new AffineTransform();
// rotating in central axis
tr.rotate(
Math.toRadians(one.rotation),
x + (disToLeft + disToRight) / 2,
z + (disToTop + disToBottom) / 2
);
my = tr.createTransformedShape(my);
I am then doing the exact same thing with another Shape, and testing for intersection. This works.
However, it feels like the Shape is the incorrect dimensions. Or something. My ship is colliding very far out to one side (outside where it graphically exists), but through the other side, I can almost go right through the ship before any collision is detected!
Basically the Shapes are simply intersecting at the wrong location. And I cannot work out why. Either the shape, the location, or the rotation must be wrong.
int disToLeft = 100;
int disToRight = 100;
int disToTop = 150;
int disToBottom = 100;
These are the distance from the centre to the left, right, top, and bottom sides.
I am using Z instead of Y because my game is in a 3D world and the sea level is pretty much the same (hence I don't need to worry about Y axis collision!).
Update:
After doing a lot of testing, I have discovered that the top of the rectangle is in the middle! I have done a lot of messing around, but without being able to graphically see the squares, it's been very hard to test.
This means that the box is on the side of the ship, like this:
Obviously when the ship on the left rotates to what it's like in this picture, a collision is detected.
It seems that your rotation is wrong. From my understanding of math it should be
tr.rotate(Math.toRadians(one.rotation), x + (disToRight - disToLeft) /2, z + (disToBottom - disToTop) /2);
Note the signs and the order of the variables
Edit:
Let's take apart the formula:
Your Rectangle is defined like this:
x-coordinate (x): one.x-disToLeft
y-coordinate (y): one.z-disToTop
width: disToLeft+disToRight
height: disToTop+disToBottom
The centre of the Rectangle (where you are rotating) is therefore:
(x+width/2)
(y+height/2)
if you replace x, width, y and height with the declarations above you get
(one.x-disToLeft + (disToLeft+disToRight)/2)
(one.z-disToTop + (disToTop+disToBottom)/2)
This is already the point you need, but it can be simplyfied:
one.x- disToLeft + (disToLeft+disToRight)/2
is equal to
one.x-(2*disToLeft/2)+(disToLeft/2)+(disToRight/2)
is equal to
one.x-(distoLeft/2) + (disToRight/2)
is equal to
one.x+(disToRight-disToLeft)/2
The other coordinate works exactly the same.

Line intersect Object

Rectangle rEnemy = new Rectangle();
rEnemy.setBounds(0,0,40,40);
Rectangle rPlayer = new Rectangle();
rPlayer.setBounds(200,0,40,40);
my question: how can i make detection/intersection for rPlayer using something like "line" long for 100px?
example: rEnemy is at (0,0), rPlayer is at (100,0) and now rPlayer is intersecting line. there is a method rPlayer.intersect(rEnemy); but i what keep them 100px away from each other and get detection. of course i mean line which is aplicable for others positions of our rectangles because rPlayer and rEnemy are still in move.
I'm not exactly sure what you want, it sounds like you want to check if they are 100px away but intersecting on the x (or y) coordinate, not a 100px radius.
If you're trying to detect if they are within that bounds, you could merely make 1 or 2 rectangles that extend 100px in the x and y coordinates of either the enemy or player and check if the other piece intersects that.
But it would probably be better to just add 100px to the x coordinate of the enemy and check if the player comes within that boundary, that way you aren't drawing more rectangles. I just mentioned that before because I thought it might be easier to visualize.
I just slove this by creating 3rd rect starting from rEnemy cords and making it width and height to rPlayer cords, then:
double calculate = Math.pow(Math.abs(rPlayer.x-rEnemy.x-Camera.posX), 2)+Math.pow(Math.abs(rEnemy.y-rPlayer.y+Camera.posY), 2);
int distance = (int)Math.sqrt(calculate);
variable distance is value of px which separate rPlayer and rEnemy at any position in shortest way.

Change coordinates to bottom left corner

How can I change coordinates to bottom left corner?
I know that's in Java the coordinates begin from Top=Left corner, but I'm asking if can someone help me how can I change it to begin (0,0) coordinates from Bottom-Left corner?
I think it's too late, but for people like me (new to android development). The above answers are correct but here is a more detailed one
If you get the coordinate with respect to top left as (a,b),
then the coordinates with respect to the bottom left are simply
(a, h-b) where h is the height of the view.
Example:
float x = getXcoordinatesonTouch();
float y = getYcoordinatesonTouch();
//should return height
float h = getHeightoftheView();
float transformY = h - y;
//"x" should be as it is
//Now you can show "x" and "transformY"
getHeight() will get you the size height. so (0, getHeight()) will give you the left-bottom point. But take into consideration the height of the object you want to place. So you may want to use
(0, getHeight() - heightOfObject)
Use the value (x, HEIGHT - y).

Check if rectangle contains another rectangle with specific distance between borders

I am trying to write small program to check if one rectangle contains second rectangle and distance betwwen their borders should be less than some specific number like 100 or 50.
When I use Rectangle.contains method it doesnt care about about distnce between borders of both rectangle.
Is there any way this can be achieved?
You could use the contains(Rectancle r) method twice: first to check if the inner rectangle is inside the outer rectangle at all, then temporarily enlarge the inner rectangle by half the threshold into every direction and make the same check again. This time it shouldn't be inside the outer rectangle anymore. So basically something like this:
//Rectangle outer; // Do some proper setup for these two
//Rectangle inner;
int limit = 50;
boolean containsWithinLimits = outer.contains(inner);
inner.setLocation(inner.getX()-limit/2, inner.getY()-limit/2);
inner.setSize(inner.getWidth()+limit, inner.getHeight()+limit);
boolean containsWithinLimits = containsWithinLimits && !outer.contains(inner);
// Now reset the bounds:
inner.setLocation(inner.getX()+limit/2, inner.getY()+limit/2);
inner.setSize(inner.getWidth()-limit, inner.getHeight()-limit);
example code etc?
Even so, you'll want to do the following:
unless im mistaken.
pseudo code:
rectA = outside, rectB = inside
if rectB.left - rectA.left < x then distance is ok else do something
if rectB.top - rectA.top < y then distance is ok else do something
etc etc
If the rectangles are orthogonal, and I assume that you have called contains(Rectangle rect). Then you only have to do extra comparisons:
inside.x - outside.x >= LEFT_BORDER &&
outside.x + outside.width - inside.x - inside.width >= RIGHT_BORDER &&
inside.y - outside.y >= TOP_BORDER &&
outside.y + outside.height - inside.y - inside.height >= BOTTOM_BORDER
LEFT_BORDER, RIGHT_BORDER, TOP_BORDER, BOTTOM_BORDER are for you to define.
In Rectangle2d ,we are having
boolean contains(double x, double y)
--- Tests if a specified coordinate is inside the boundary of this Rectangle2D.
boolean contains(double x, double y, double w, double h)
--- Tests if the interior of this Rectangle2D entirely contains the specified set of rectangular coordinates.
So it is not possible to check about the distance to my knowledge...

Categories

Resources