I would like to know more about the coordinates of a Rectangle,
in particular:
lower left X
lower left Y
upper right X
upper right Y
Every time, I get confused about how to make dimensions based on these coordinates to draw rectangle.
If possible, can I get a graphical representation briefly about these coordinates positions?
Before someone can explain what the lower-left X, lower-left Y, upper-right X and upper-right Y of a rectangle are about, you need to know about the coordinate system: Where is the Origin (x,y) of a PDF page?
The answer to that question contains all the information you need, except for the graphical representation you are asking for. This is a simple representation of the coordinate system:
The origin of the coordinate system is (0, 0). Positive X values are to the right of the origin, positive Y values are above the origin.
I have drawn a Rectangle and indicates where you can find the lower-left corner (with coordinate (llx, lly)) and the upper-right corner (with coordinate (urx, ury)).
The sides of the rectangle are always in parallel with the X and the Y axis, hence you only need two coordinates to define the rectangle.
Related
Java has a really non-intuitive coordinate scale where the origin (0,0) is at the top left and the Y coordinate increases downwards.
Normally graphs have (0, 0) at the bottom left and the Y coordinate increases upwards.
This makes drawing points and shapes in relation to each other in a graph very difficult.
How can I change it so that the origin is at (0, 0) and Y goes upward?
Thanks
This is not "non-intuitive" if you study display hardware since the screen coordinates also begin in the upper left quadrant.
The solution is to separate your model from your view a la Model-View-Controller, to have your model's coordinates oriented as you see fit, and to write methods that translate model to view and back again where needed.
I have a Canvas on which I've drawn a circle / 360 degree arc. I have the arc start drawing from -90 (the top) rather than the right (0) as is default.
I want to place a rectangle at the top of the same canvas and to reduce the sweep of the arc so that the two do not intersect. I've attached an image to illustrate
So what I need to do is work out what angle is represented by half of the rectangle so that I can adjust where to start drawing my arc. The centre of the circle is at the centre of my Canvas:
[canvas.width /2, canvas.height /2]
I've read some resources like this question but they haven't helped to do much more than make me feel like I know nothing. I tried a few failing formulas ending with this
double adjustment = Math.atan2(rectangleY - circleY, rectangleX - circleX) - Math.atan2(rectangleY - circleY, (rectangleX + rectangleWidth) - circleX);
Can somebody tell me what is the right way to calculate this in Java? I'd also like to know how to find where the rectangle intersects if that's possible (I.e. the width of the shaded orange part on the image) although this is of lesser importance to me right now
Let W be the width of the rectangle, R be radius of the circle, and A be the angle you're looking for.
There's a right-angle triangle with angle A at the center, R as the hypotenuse and W/2 as the side opposite angle A, so
W/2R = sin(A)
so
A = Math.asin(0.5*W/R);
Of course you can't use asin(0.5W/R) when W > 2R
EDIT
To answer the second problem (finding the intersection)
Let H be the distance from the center of the circle to the rectangle. When the rectangle is wide enough that the circle intersects the lower side, there's a right angle triangle with A at the center, R as the hypotenuse, and H as the side adjacent to A
H/R = cos(A) and A = Math.acos(H/R). Calculate both angles and use the smaller one.
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.
I'm facing issue for a class where I need to convert and draw GPS coordinates dynamically into a JPanel. I have this local library data in a file that I parse. The structure is ID, County, Library Name, Latitude, Longitude.
R1,Ramsey,Maplewood,45.0327,-93.0262
R2,Ramsey,Mounds View,45.1059,-93.2104
R3,Ramsey,New Brighton,45.06604,-93.19125
R4,Ramsey,North St. Paul,45.0105,-92.9968
R5,Ramsey,Roseville,45.0072,-93.1558
R6,Ramsey,Shoreview,45.0805,-93.1345
R7,Ramsey,White Bear Lake,45.0831,-93.0092
As you can see, the locations are very close together (7 locations in 170 sq miles, Max Distance apart: 15.5 miles). My code currently can draw and link the nodes properly from hardcoded coordinates instead of the GPS data. I have found algorithms for converting the GPS lat and long to XY coordinates, but once the calculations happen, all the objects print on top of each other since the calculation leads to the same XY because of it being so close. The process needs to be dynamic because I anticipate that the test files to be use will be using 42 locations. What can I do for an equation that will give me XY coordinates that have enough variety to make a decent graphic rendering instead of using random points?
What can I do for an equation that will give me XY coordinates that have enough variety to make a decent graphic rendering instead of using random points?
Find the bounding box. In other words, find the smallest x and smallest y coordinate in your list. Find the largest x and largest y coordinate in your list. Those two points define the bounding box.
Now, translate these location x, y coordinates to drawing x, y coordinates. Your smallest location x, y coordinate becomes drawing coordinate 0, 0. Your largest location x, y coordinate becomes drawing coordinate width, height.
In order to keep the scaling from distorting the image, we have to first calculate the scaling factor for the x coordinate and the y coordinate.
scaling factor x = drawing width / (location maximum x - location minimum x)
scaling factor y = drawing height / (location maximum y - location minimum y)
Then we use the smaller of the two scaling factors.
scaling factor = Math.min(scaling factor x, scaling factor y)
The equations for converting location coordinates to drawing coordinates are:
drawing x = location x * scaling factor
drawing y = location y * scaling factor
Location and scaling factor are doubles, so you don't drop any precision. You have to convert drawing x and drawing y to integers so you can plot them.
Drawing x increases from west to east. Location x probably increases from west to east.
Drawing y increases from north to south. If location y increases from south to north, then you have to take that into account when doing the conversion.
drawing y = drawing height - drawing y
You'll probably will want to add a margin to your drawing area so that none of the locations are drawn on the edge of the drawing area.
Let's say you want a margin of 10 pixels. That would make the actual drawing area 20 pixels larger in width and 20 pixels larger in height.
You then add 10 pixels to the drawing x and the drawing y before you plot your location.
The ideal way to go about this is to find the min-longitude and min-latitude , max-longitude and max-latitude and map them to the [0,0] and [JPanels.width , Jpanels.Height].
this map can be done just
Point map(cure_location){
int X = (curr_location.longitude-min_longitude)*(scalelog);
int Y = (curr_location.latitude-min_latitude)*(scalelon);
return new Point(X,Y);
}
and scalelog and scalelon are
scalelog = (JPanels.width)/(max_longitude-min_longitude)
scalelat = (JPanels.height)/(max_latitude-min_latitude)
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