I am trying to understand the different syntax of creating rectangles - java

I understand this code here. The point of origin is 0,0 or top left of the JFrame and the width of the rectangle is 9 and height covers from bottom to top.
Rectangle left = new Rectangle(0,0,WIDTH/9,HEIGHT);
But I don't quite understand this. What is the point of origin here? Is 9 being multiplied by 8 or is it saying the measurement is 9 by 8? What is the purpose of the multiplication sign?
Rectangle right = new Rectangle((WIDTH/9)*8,0,WIDTH/9,HEIGHT);

What is the purpose of the multiplication sign?
The x origin of the rectangle is 8/9 of the way across the JFrame. It's right justified (I assume).

Rectangle right = new Rectangle( (WIDTH/9)*8, 0, WIDTH/9, HEIGHT);
This means that the x origin is real 9/8th of the WIDTH. And its width is 1/9th WIDTH variable. Looks like this would move the rectangle horizontally.

Without seeing the whole code it's hard to know, but I'd assume that WIDTH is the total width of whatever will contain the two rectangles. In that case, you'll end up with two rectangles that themselves have a width one-ninth of the total width, and occupy the left and right sides of the container.
Since the co-ordinates are the top left corner of the rectangle, to make the one-ninth width rectangle occupy the right side of the container, the x co-ordinate needs to be eight-ninths of the total width, which is what (WIDTH/9)*8 calculates.

a bit of reworking of the values gives us
Rectangle right = new Rectangle(WIDTH-(WIDTH/9),0,WIDTH/9,HEIGHT);
this means that the right side of right falls on WIDTH

Related

Find the size of maze corridors in a image

So I am building a application to solve mazes one of the options is upload a picture and it will solve it. However upon solving the maze the output will look like this.
I would like to figure out how to make my program find the proper corridor size and have the solution look like this with the pathway completely full
My data is put into a array with 1's representing the walls and 0's the spaces like this. So far I have thought about trying to find the smallest distance between 1's but that runs into problems with circular mazes and writing on the maze. I have thought about filling the distance between the walls but that runs into problems at intersections.
I am drawing on the image using
image.setRGB(x, y, Color.RED.getRGB());
with the image being a BufferedImage.
I am truly all out of ideas and don't know how to come at this problem any help would be appreciated.
Each square in your grid has a certain size. Say wsq * hsq for "width of square times height of square".
Given your much more fine-grained (x, y), you can find in which square it is by dividing x by wsq and y by wsh:
int xsq = x / wsq;
int ysq = y / ysq;
The area to paint red would be from (xsq * wsq, ysq * hsq) and have width/height (wsq, hsq). and you could paint that red, but it would mean that you paint over the walls. So you have to adjust the area you're going to fill with red color by the size of the walls. If the walls are all two pixels thick, you need to add 1 to the x and the y coordinate of the square, and substract 2 from the widht and the height.
And you could fill it again (with a Graphics2D) for every time that you are now calling image.setRGB or you could remember which squares that you already filled.
Note: since you are working with regular-sized squares, you can also optimize your maze-solving algorithm to work in a grid of squares of size (wsq, hsq) rather than the individual pixels in the image.

Basing the Recangle in upper-left side of the rootPane

I am very new at JavaFX. Currently working with 8th version. I am facing the following problem. When I am trying to draw a rectangle, it is center aligned. I added a eventHandler, which gives me the coordinates of the mouse click. So I just need this rectangle to be drawn from upper-left, but not from center, so I can just pass the coordinates of the mouse' clicks and then this rectangle
so the y axis would be from the upper left to south, and the y axis from the upper left to east. So
carRect = new Rectangle(0,0,70,36);
This code means, that this rectangle would be drawn upper-left aligned but not center aligned with width 70 and height 36. What am I missing ? I know, that I can pass negative values, but I need them just in the way I get them from the eventHandler.
Just used Normal Pane instead of StackPane and that solved the problem.

Why is the center of a rectangle (x+width/2, y+height/2) in Java?

Since (x, y) is the upper-left corner of the rectangle, shouldn't the center be (x+width/2, y-height/2)? But the textbook I'm learning says it's the titular point.
The upper-left point is (0,0) and positive direction of y-axis is downward, the positive direction of x-axis is rightward.
To make you have a better understanding , refer to the image:
Assume the rectangle's width is 20 and the height is 15.
If you want to know why it's like this, pls refer to https://gamedev.stackexchange.com/a/83571/48636
Hope it helps.
In the java, you can say that the Y axis is inverted. Top left corner of the screen is the (0,0) point. If you go to right side of the screen, X increases, if you go down, Y increases. That is why y+height/2 is used instead of y-height/2. An example is shown in the image below.

libGDX Coordinate System

I'm trying to properly configure my Camera and Sprites in libGDX to show up in a 2D coordinate system properly with the origin at the bottom left hand corner.
I set up my Camera like this:
cameraWidth = Gdx.graphics.getWidth();
cameraHeight = Gdx.graphics.getHeight();
camera = new OrthographicCamera(1, cameraHeight/cameraWidth);
And I set up my Sprites like this:
sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);
sprite.setScale(scale);
sprite.setPosition(startX,startY);
My problem is with sprite.setSize(x,y). If I set all the sprites to have a size of (1, texture aspect ratio), then everything draws with the right display ratio (not smushed or stretched), but nothing draws in the correct place. For example, if I draw something at (0,0), it will draw with its bottom left corner off the left side of the screen and up a number of pixels.
I've noticed by changing around the ratio I can get things to draw in different places - namely if I set it to (1, display aspect ratio) things look pretty close to drawing in the right place - they just draw from their center, not their bottom left corner, as LibGDX specifies. The only problem is that the images all appear as smushed or stretched, which is no good.
This seems like a simple problem and I just want to know how to set this up so I can have a sensible coordinate system that draws things in the right place and in the right aspect ratio. Thanks.
Once you change your viewport to match the screen's aspect ratio then (0, 0) will no longer be at the bottom left of the screen unless the screen is square. If the screen is wider than it is high then the visible portion of the x axis will still go from 0.0 to 1.0, but 0.0 on the y axis will now be somewhere off the bottom of the screen.
If you adjust the camera so that (0, 0) is at the bottom left of the screen, and remember that the visible y axis will only go up to grapicsHeight / graphicsWidth then that should solve your coordinate problem.
I would recommend setting the camera to point to the middle of the screen rather than the bottom left. There's an example here that does exactly that, drawing a 2:1 rectangle which is always in the centre of the screen, always with a 2:1 ratio no matter how much you resize it.
I've found a solution to this problem:
Set the camera to ortho (even though it's already an orthographic camera)
camera.setToOrtho(false,1,screen height / screen width);
Also, each sprite must have its position set to (x - sprite.getWidth()/2, y - sprite.getHeight()/2. I extended the Sprite class and overrode the setPosition method to account for this. Now, every time the position is set, the Sprites end up going where you "would think they'd go", with setPosition(0,0) putting it in the bottom left and setPosition(1,height/width) in the top left.
Oddly enough, this draws every sprite centered around the (x,y) point, which would make sense since width/2 and height/2 were subtracted from the position, except not subtracting the values does not make setPosition center the sprite via the bottom left corner - it's centered in a way I haven't figured out.

Point of Origin, Width, Height

Rectangle left = new Rectangle(0,0,WIDTH/9,HEIGHT);
In this code, if I increase the width why does it look like it extends farther over to the left on the JFrame? And if I decrease it why does it extend out to the right? Does this not work like a coordinate plain? Height makes a little more sense to me. If height is increased it extends up and if it is decreased it extends down.
Swing draws with 0,0 being the top left corner, with positive X extending out to the right, and positive Y extending downwards. This can be confusing as some people expect 0,0 to be the bottom left corner.
If you are having trouble figuring out where thing are as opposed to where you think they would be, i would suggest just drawing the points of your shapes, and play around with that rather then trying to draw rectangles etc first.

Categories

Resources