how to divide a rectangle - java

I have four latitude and longitude values which creates a rectangle(area).Now I want to divide the rectangle into grids.
So I define the latlong
(11.20804, 122.22839),(11.20804, 122.52914),(11.06926, 122.52914),(11.06926, 122.22839)
After dividing the grids I want to give each grid an id and want the latitude and longitude values of each grid_id.I am struggling how to divide the rectangle into grids and assign them grid_id.
Any help is appreciated

Something like this. Will need to play around with it and decide if your ok if the bottom won't have a perfect square. This is meant as a starting point, not a complete solution.
I just read that you want grid ids, so guessing like 1,1, so your going to have to adjust this to suite your needs.
double width = 122.52914 - 122.22839;
double height = 11.20804 - 11.06926;
int numbSquares = 10;
int lengthOfOneSquare = width/numbSquares;
HashMap<Double[], String> hmap = new HashMap<Double[], String>();
for (double x = 11.20804; x <= 11.06926 - lengthOfOneSquare; x += lengthOfOneSquare) {
for (double y = 122.22839; y <= 122.52914 - lengthOfOneSquare; y+= lengthOfOneSquare) {
Double[] location = new Double[]{x, y};
String id = x+""+y; <--- converts x and y to a string
hmap.put(location, id);
}
}

Related

Getting area from GPS coordinates

So, I've got a method that returns the area of a shape defined by its points (given in CCW or CW order, it doesn't really matter). I tested it with some shapes and it seems to be working.
The problem is, I want to use this method with GPS coordinates, and I want to return the result in m² or km², but that's definitly not what happens. In fact, I don't even know in which unit the result is when I use this method with that kind of coordinates.
So the question is, how to convert the result I have into m² or km² ? I tried some things, but either it does not work or it's inaccurate.
Here's my method, if you want to check :
public static double getArea(List<Vector2D> points) {
double firstSum = 0, secondSum = 0;
for (int i = 0 ; i < points.size()-1 ; i++) {
firstSum += points.get(i).x * points.get(i+1).y;
secondSum += points.get(i).y * points.get(i+1).x;
}
firstSum += points.get( points.size()-1 ).x * points.get(0).y;
secondSum += points.get( points.size()-1 ).y * points.get(0).x;
return Math.abs((firstSum-secondSum)/2);
}
(Vector2D is the class I use for points, with x as the latitude and y as the longitude)
The problem is that you're not taking the (approximately) spherical nature of the Earth into account. For a start, you need to take into account the radius of the Earth - you could have the same list of latitude and longitude on a smaller (or bigger) planet, but the radius would be different, and consequently the area would be different too.
You can use the approach in this question. It's trivial to convert that to Java:
public static double CalculatePolygonArea(List<Vector2D> coordinates)
{
double area = 0;
if (coordinates.size() > 2)
{
for (int i = 0; i < coordinates.size()-1; i++)
{
Vector2D p1, p2;
p1 = coordinates.get(i);
p2 = coordinates.get(i + 1);
area += Math.toRadians(p2.x - p1.x) * (2 + Math.sin(Math.toRadians(p1.y))
+ Math.sin(Math.toRadians(p2.y)));
}
area = area * R * R / 2;
}
return Math.abs(area);
}
(assuming Vector2D.x is the longitude and Vector2D.y is the latitude).
R is the radius of the Earth. Use a value in the unit you want the area result to be in (e.g. 6_371_000 metres for square metres, 6_371 km for square km, 3_959 miles for square miles...)

Beginner JavaFX, tracking a max radius generated randomly?

I have a project where I have to fill a 600x400 window (JavaFX) with 30 random sized circles with no filling. The largest circle must be filled with a translucent red (and if there are multiple large circles with the same radius only one can be filled). I'm able to get all the circles on the screen fine. My problem is getting the largest circle to be red. I haven't been taught arrays which were used in almost all of my many google searches. I cant figure out how exactly to track the largest circle. His hint to us is : "When it comes to keeping track of the largest circle, remember that two reference variables can point to the same Circle object. Maintain a separate Circle reference variable that always points to the largest circle (so far created). You may want to initialize this variable to a circle that has a radius of 0. You can get the radius of a circle using the getRadius method." I created a circle object and a largestCircle object but don't understand how to make the largestCircle object have the highest radius.
This is the code I have so far:
{
Random gen = new Random();
int x = 0;
int y = 0;
int radius = 0;
double largestRadius = Math.max(radius);
Circle largestCircle = null;
Group root = new Group();
//prints out 30 circles
for (int i = 0; i <= 30; i++)
{
Circle circle = new Circle(x, y, radius);
{
radius = gen.nextInt(66) + 10; //generates random radius from 10 to 75
x = gen.nextInt(600 - 2 * radius) + radius;
y = gen.nextInt(400 - 2 * radius) + radius;
}
if (circle.getRadius() == largestRadius)
{
largestCircle = circle;
largestCircle.setFill(Color.rgb(255, 0, 0, 0.3));
}
circle.setFill(null);
circle.setStroke(Color.rgb(gen.nextInt(256), + gen.nextInt(256), gen.nextInt(256)));
circle.setStrokeWidth(3);
root.getChildren().add(circle);
}
after I generate the random circles how to I find the max radius that was generated and set it to largestCircle? the highest radius a circle can be is 75, but sometimes none of the circles have a radius of 75. How do I set the max to be the highest number the program randomly generates?
Any help would be greatly appreciated! Thank you for your time
How about the following.
It has a two fixes.
-1, use > and not == when figuring if current circle is the largest.
-2, change the color of the largest circle at the end, after all the circles have been made... else you might make multiple circles red.
{
Random gen = new Random();
int x = 0;
int y = 0;
int radius = 0;
double largestRadius = Math.max(radius);
Circle largestCircle = null;
Group root = new Group();
//prints out 30 circles
for (int i = 0; i <= 30; i++)
{
Circle circle = new Circle(x, y, radius);
if (circle.getRadius() > largestRadius)
{
largestCircle = circle;
}
{
radius = gen.nextInt(66) + 10; //generates random radius from 10 to 75
x = gen.nextInt(600 - 2 * radius) + radius;
y = gen.nextInt(400 - 2 * radius) + radius;
}
circle.setFill(null);
circle.setStroke(Color.rgb(gen.nextInt(256), + gen.nextInt(256), gen.nextInt(256)));
circle.setStrokeWidth(3);
root.getChildren().add(circle);
}
largestCircle.setFill(Color.rgb(255, 0, 0, 0.3));
It is generally a good idea to initialize any max variable with a small number that is out of scope for your project. In this case, since radius can not be -1, I would do
double largestRadius = -1;
After this, it doesn't matter how big the radius can be, any radius bigger than -1 will change the largestRadius.
It looks to me like you are only missing one part and that is the if the newly created circle has a radius > largestRadius.
if(circle.getRadius() > largestRadius){
largestCircle = circle;
largestRadius = circle.getRadius();
}
After this, you have checked for if the new circle has a radius greater than AND you have checked if the new circle has a radius equal to. Keeping the if statement that you already have, you will always reference the newest circle with the largestRadius.
I would keep the circle objects in an array. Use a double (or whatever number type is appropriate for your random values) to track the high value with a simple comparison (is my current high value less than the new random value? if so, update high value) each time you generate a random value and create a circle of that size.
Once you have your 30 circles in your array simply loop through it until you find the first occurrence of your high value, when you find it make that circle whatever color.
Circle[] myCircles=new Circle[30];
double largestCircle;
for(int i=0;i<30;i++){
// determine your x,y, and radius here
myCircles[i]=new Circle(x,y,radius);
if(radius>largestCircle) largestCircle=radius;
}
Then to loop thru your myCircles and do things with each one
for(int i=0;i<30;i++){
if(myCircles[i].getRadius()==largestCircle){
// make myCircles[i] red here
}
}

How to get Y value from X value in JFreeChart

I'm using JFreeChart to draw chart. I have XYSeries with points (0, 0), (1, 2), (2, 5) and I want to read Y value for let's say x=1.5.
Is it possible to read value for points which are not in XYSeries? I couldn't find similar topic.
This is not supported directly. It does not make sense in many cases: There simply is no data available for x=1.5. The value there could be 1000.0, or -3.141. You don't know.
However, you're most likely looking for a linear interpolation. The pragmatic approach is thus to find the interval that contains the respective x-value, and interpolate the y-values linearly.
There are some technical caveats. E.g. the XYSeries may be not sorted, or may contain duplicate x-values, in which case there is no unique y-value for a given x-value. But for now, we can assume that the data set does not have these properties.
The following is an example of how this could be implemented. Note that this is not very efficient. If you have to compute many intermediate values (that is, if you intend to call the interpolate method very often), it would be beneficial to create a tree-based data structure that allows looking up the interval in O(logn).
However, if this is not time critical (e.g. if you only intend to show the value in a tooltip or so), you may interpolate the values like this:
import java.util.List;
import org.jfree.data.xy.XYDataItem;
import org.jfree.data.xy.XYSeries;
public class XYInterpolation
{
public static void main(String[] args)
{
XYSeries s = new XYSeries("Series");
s.add(0,0);
s.add(1,2);
s.add(2,5);
double minX = -0.5;
double maxX = 3.0;
int steps = 35;
for (int i=0; i<=steps; i++)
{
double a = (double)i / steps;
double x = minX + a * (maxX - minX);
double y = interpolate(s, x);
System.out.printf("%8.3f : %8.3f\n", x, y);
}
}
private static double interpolate(XYSeries s, double x)
{
if (x <= s.getMinX())
{
return s.getY(0).doubleValue();
}
if (x >= s.getMaxX())
{
return s.getY(s.getItemCount()-1).doubleValue();
}
List<?> items = s.getItems();
for (int i=0; i<items.size()-1; i++)
{
XYDataItem i0 = (XYDataItem) items.get(i);
XYDataItem i1 = (XYDataItem) items.get(i+1);
double x0 = i0.getXValue();
double y0 = i0.getYValue();
double x1 = i1.getXValue();
double y1 = i1.getYValue();
if (x >= x0 && x <= x1)
{
double d = x - x0;
double a = d / (x1-x0);
double y = y0 + a * (y1 - y0);
return y;
}
}
// Should never happen
return 0;
}
}
(This implementation clamps at the limits. This means that for x-values that are smaller than the minimum x-value or larger than the maximum x-value, the y-value of the minimum/maximum x-value will be returned, respectively)
You can use DatasetUtils.findYValue() from package org.jfree.data.general

How to find points of intersection between ellipse and line?

I'm completely stuck.
I have an ellipse, and a line. Line is set by two points, ellipse - by bottom-left and top-right corners. I have to find their points of intersection, using java.
I tried to solve an equation system:
(1) y = kx + m;
x^2/a^2 + y^2/b^2 = 1;
but I could't make things work properly. I assume it's because of java's coordinate system, but it also may be my own mistake somewherem beacuse I'm confused with it.
Is there any better way to find points of intersection and, if not, how can I get them properly?
Thank you in advance.
Code:
double r1 = in_y2-in_y;
double r2 = in_x2-in_x;
double k = r1/r2;
double m = in_y2 - k*in_x2;
double a = Math.abs((double)x2 - (double)x)/2;
double b = Math.abs((double)y2 - (double)y)/2;
double A1 = 1/(a*a) + (k*k)/(b*b);
double B1 = (2*k*m)/b*b;
double C1 = (m*m)/(b*b);
double D = Math.sqrt(B1*B1 - 4*A1*C1);
double ex1 = (-B1 + D/(2*A1));
double ey1 = k*ex1 + m;
double ex2 = (-B1 - D/(2*A1));
double ey2 = k*ex2 + m;
This is probably no longer relevant to the original problem owner, but since I encountered the same question, let me present my answer.
There are three mistakes in the original computation that I can see: (i) the one pointed out by #mprivat, (ii) the bracketing in the assignment to B1 which should instead be
double B1 = (2*k*m)/(b*b);
and (iii) a more fundamental one: the presented computation does not correct for the origin of the ellipse. Since the ellipse is defined by itse circumscribing bounds, there is no guarantee that it is centered on (0,0).
Let's call the center (cx,cy), then the equation for the ellipse becomes
(x-cx)^2/a^2 + (y-cy)^2/b^2 = 1
rather than the original
x^2/a^2 + y^2/b^2 = 1
The simple repair, I think, is to translate the line wrt (cx,cy) and translate the results back, like so:
...
double m = (in_y2-cy) - k*(in_x2-cx);
...
double ex1 = (-B1 + D/(2*A1)) + cx;
double ey1 = k*(ex1-cx) + m + cy;
double ex2 = (-B1 - D/(2*A1)) + cx;
double ey2 = k*(ex2-cx) + m + cy;
The more elegant repair is to solve the correct equation for the ellipse instead, but this results in even more impenetrable formulas for B1 and C1:
double B1 = (2*k*(m-cy))/(b*b) - (2*cx)/(a*a);
double C1 = (m-cy)*(m-cy)/(b*b) - 1 + (cx*cx)/(a*a);
As a final remark, note that this breaks down for vertical lines, as then r2 = 0 so k isn't defined.
Java can't solve the algebra problem, but it can compute the solution once you tell it what to compute.
Sounds like you just need to replace your y in the ellipse's equation with kx+m then solve for x. Looks like it's a simply binomial equation. Write a program that computes x=... based on k, m, a and b. Java can help you compute the roots if you tell it what to compute and what the values of k, m, a and b are.
In your particular case, you want to use Java as a simple calculator...
can you please put your code so that we could see if it's correct?
anyway , here's an algorithm:
http://mathworld.wolfram.com/Ellipse-LineIntersection.html
note that since it has a square root , you might get a solution that is not precise.
Line2D.Double line = new Line2D.Double(x1,y1,x2,y2);
Ellipse2D.Double ellipse = new Ellipse2D.Double(x,y,width,height);
int resolution = 1000;
int x_distance = ellipse.getWidth()/2;
int y_distance = ellipse.getHeight()/2;
double angle = 360.0/(double)resolution;
Point center = new Point(width/2,height/2);
Point point = new Point();
for (int index = 0; index < resolution; index++)
{
int x = (center.x+x_distance)*Math.sin(Math.toRadians(angle*index)));
int y = (center.y+y_distance)*Math.cos(Math.toRadians(angle*index)));
Ellipse2D.Double dot = new Ellipse2D.Double(x,y,1,1);
if (line.intersects(dot.getBounds()))
{
point.setLocation(x,y);
index = resolution;
}
}

Divide a 2D path created from points XY into equal chunks Java

I have an array of points A,B,C,D,E...N which when connected make a path.
How can i divide this path to equal chunks and get position of each chunk XY ?
EDIT : As user Hedja suggested i have created function to process this problem, but i cannot detect situation where chunk is splitted on two subpaths
public ArrayList<PointF> getPositions(ArrayList<PointF> mInput,float mChunkSize){
ArrayList<PointF> mResult = new ArrayList<PointF>();
float mModulo = 0f;
for (int i = 0;i<mInput.size()-1;i++){
//distance to next
float mDistanceAB = MyGameMath.distance(mInput.get(i).x, mInput.get(i).y,mInput.get(i+1).x,mInput.get(i+1).y);
//how many parts will fit
float mCountParts = (float) (mDistanceAB/mChunkSize); //how much parts will fit
//if distance is greater than chunk size
if (Math.abs(mDistanceAB)>=mChunkSize) {
Log.i("Chunk","Index "+(i)+" -> "+(i+1)+" = "+mCountParts+", rest="+mModulo);
float dx = mInput.get(i+1).x-mInput.get(i).x;
float dy = mInput.get(i+1).y-mInput.get(i).y;
float ux = dx/mDistanceAB;
float uy = dy/mDistanceAB;
for (int y=0;y<=mCountParts;y++){
//for every part
float nx = mInput.get(i).x+ux*mChunkSize*y;
float ny = mInput.get(i).y+uy*mChunkSize*y;
//Log.i("Chunk","at:"+nx+","+ny);
mResult.add(new PointF(nx, ny));
}
}
mModulo = mDistanceAB%mChunkSize; //how much left from previous subpath
}
return mResult;
}
So I assume you have something similar to this where Point is an object with attributes x and y.
Point[] points = new Points[]{ //Your Points }
List<Point> chunkedPoints = new ArrayList<Point>();
I also assume by "equal chunks" you mean the distance of each path.
First you'll iterate through the array, as you won't need to calculate the "next point" after the last point, you can add it at the end.:
for(int i = 0; i < points.length-1; i++) { //Skip the last element
//chunking here
}
chunkedPoints.add(points[points.length-1]); //Add the last element
You'll need to find the Unit Vector, that is, the direction you travel to get to the next point. So first you need to get the difference in x and y from one point and the next:
double dx = point[i].x - point[i+1].x;
double dy = point[i].y - point[i+1].y;
Then the distance from that point to the next (simple Pythagoras):
double distance = Math.sqrt(dx*dx+dy*dy);
The unit vector can now be calculated
double ux = dx/distance;
double uy = dy/distance;
So now you know where to travel, you need to specify how far you want to travel along it, I'll call this CHUNK_SIZE.
double nx = point[i].x + ux*CHUNK_SIZE;
double ny = point[i].y + uy*CHUNK_SIZE;
nx and ny is the co-ordinate of your new point. However, you need to check if you've passed the next point so that you can stop. Your problem doesn't specify what you do when you reach an end of a subpath without travelling the chunk size so I'll assume you simply stop at it, so the code becomes:
double nx = point[i].x;
double ny = point[i].y;
for(
//This part can be tidier
int count = 0;
count < CHUNK_SIZE && nx+ux != points[i+1].x && ny+uy != points[i+1].y;
count++
) {
nx += ux;
ny += uy;
}
Point newPoint = new Point(nx, ny);
Now you have your new Point, you can start from there, aim for the same point as before or if it's the same as the next point, start from the point after that. So your loop is now something like
chunkedPoints.add(points[0]);
for(int i = 0; i < points.length-1; i++) { //Skip the last element
Point newPoint;
do {
//chunking
newPoint = new Point(nx, ny);
chunkedPoints.add(newPoint);
} while(!newPoint.equals(points[i+1]));
}
chunkedPoints.add(points[points.length-1]); //Add the last element
Hope that helped.
I haven't tested this, but I've done something very similar in the past, so it should work.
EDIT: Okay, I've seen your edit and honestly have no idea what your question is asking. Sorry.

Categories

Resources