How to get Point values from ArrayList - java

I have a small problem that I hope is easy to solve. In the code below on the second row I have path1.moveTo... but instead of using the touchDownX1 and touchDownY1 coordinates, I thought it would be better to use the first values of X and Y from the touchPoints[0], but I don't know how?
// Path 1
path1.moveTo(touchDownX1, touchDownY1);
for(Point point: touchPoints[0]) {
path1.lineTo(point.x, point.y);
canvas.drawPath(path1, paint1);
}

You have to write touchPoints.get(0) because the [index] notation only works for arrays, not ArrayLists.
Edit:
The rest of the code should work. The way you access x and y is perfectly fine, assuming that the first element touchPoints is a list of points. If the first element of touchPoints is a single point, do not use a loop, just do touchPoints.get(0).x and the same for y.
Edit:
The moveTo method should only be called for the beginning of a contour/shape to set its starting point. There is no reason to call it more than that for a single contour.

Related

Comparing a set of coordinates (strokes?) for a percentage difference

I have 2 paths A and B.
For example:
A = {(1,1), (2,2), (3,3)}
B = {(2,2), (3,3), (5,5), (6,7)}
Each pair of elements is a line, starting at the element before it.
The beginning element is the start of the stroke, and the ending element is the end of the stroke.
I want to compare A and B for similarity, and in the end get a percentage accuracy.
I have done some research already and found references to Hausdorff Distance and Frechet Distance but I cannot figure out how to get these to do what I want them to do.
Thank you!
(The language I am looking to code this in, is Java if any libraries exist for this problem, that would be greatly appreciated)

Checking for ArrayIndexOutOfBounds Java

I have a 3x3 array of integers (0 or 1): int[][] matrix. The goal is to get through the maze (matrix) of 0's and 1's by assuming the 0's are walls. I'm not sure how to go about checking the neighbors.
For example, starting at [0][0] and checking above it, I need to do something like:
if (currentPosition.getColumn()-1 != null && !checkIfWall[getRow()][getColumn()-1]) {
//do stuff
}
Where checkIfWall is a boolean 2D array of walls or not wall. The problem is that checkIfWall returns ArrayOutOfBounds if any of the values are -1. Additionally, I have to write three other if statements to check below, left, and right, which seems tedious. Is there a better way to do this?
The way I would do it is assume that any location outside the array is a wall - so then you can create a function to do a collision test and check the bounds inside that.
boolean isWall(int x, int y) {
if (x<0||x>3)
return true;
if (y<0||y>3)
return true;
return data[x][y]==0;
}
Now you can just call isWall for any co-ordinates you like and it will never error, and so you don't need to worry about where you are in all your other algorithms - anything outside the maze is always treated as a wall.
First of all, I think you're confusing the directions ... you think about "up" as "-1", but usually what happens in a 2d array is that the top left corner is 0,0. and as "y" grows, it's going "down".
In any case, regarding your question, I would solve this by having a method that simply gets the current location and a direction (up,down, right, left, either strings, or better enums). You should have in your file a global variable for the size, so you can verify you are in a "valid" state, and if you're trying to check a value "outside" the array, it should return false (treat as a wall, since you can't go that way).
This will have the benefits of making your code readable as well. for example: if (isValid(this, up)) go up... and so on...

Finding Rectangle which contains a Point

In Java SE 7, I'm trying to solve a problem where I have a series of Rectangles. Through some user interaction, I get a Point. What I need to do is find the (first) Rectangle which contains the Point (if any).
Currently, I'm doing this via the very naieve solution of just storing the Rectangles in an ArrayList, and searching for the containing Rectangle by iterating over the list and using contains(). The problem is that, because this needs to be interactive for the user, this technique starts to be too slow for even a relatively small number of Rectangles (say, 200).
My current code looks something like this:
// Given rects is an ArrayList<Rectangle>, and p is a Point:
for(Rectangle r : rects)
{
if(r.contains(p))
{
return r;
}
}
return null;
Is there a more clever way to solve this problem (namely, in O(log n) instead of O(n), and/or with fewer calls to contains() by eliminating obviously bad candidates early)?
Yes, there is. Build 2 interval trees which will tell you if there is a rectangle between x1 to x2 and between y1 and y2. Then, when you have the co-ordinates of the point, perform O(log n) searches in both the trees.
That'll tell you if there are possibly rectangles around the point of interest. You still need to check if there is a common rectangle given by the two trees.

Storing and Retrieving points (related x and y) quickly in Java. List vs Array

I am attempting to recreate a board game in Java which involves me storing a set of valid places pieces can be placed (for the AI). I thought that perhaps instead of storing as a list of Points, it would be run-time faster if I had an array/list/dictionary of the X coordinates in which there was an array/list of the y coordinates, so once you found the x coordinate you would only have to check its Ys not all the remaining points'.
The trouble I have is that i must change the valid points often. I came up with some possible solutions but have difficulty picking/implementing them:
HashMap < Integer, ArrayList > with X as an integer key and the Ys as an ArrayList.
Problem: I would have to create a new ArrayList every time I add an X.
Also I am unsure about runtime performance of HashMap.
int[X][Y] array initialized to the board size with each point set to its relative location (point 2,3 sets[2][3]) unset point being an invalid integer.
Problem: I would have to iterate through all the points and check every point.
List of Points This would simply be a Linked/Array List of Points.
Problem: Lists are slower than arrays.
How would using a Linked list of Points compare to checking the whole array like above?
Perhaps I should use a 2d linked list? What would be the fastest runtime way to do this?
You're worrying about the wrong things. Accessing collection/map/array items is extremely fast. The graphical part will be way more performance-sensitive. Just use whatever data structure is most natural. It's unlikely that you're going to be storing enough items to really matter anyway. Build it first, then figure out where your performance problems really are.
if you use an ArrayList of Points you have nearly the same performance as with an array (in Java)
and I think this is the fastest solution, because as you already mentioned you have to iterate through the complete int-array and a HashMap and the relying ArrayLists have to be changed depending on changing/adding coordinates

Filling in the Area between of two lines which intersect?

Is it possible to fill in the area between a Dataset plotting a XY line and a ValueMarker ?
See picture for the general idea(Warning: My MS Paint skills are lacking).
As lschin, XYDifferenceRenderer is the best way to do this. In order to have this work you need to create two separate multidimensional double arrays to store to X and Y coordinates. The first array is set to store your a XY line's x and y coor's. The second array is a constant XY line. To set this line up you X values are the same. If your original line is above your constant line, the Y value is the coordinate you choose of position of the constant line. If the original is below the constant then the Y value of the constant is that of your original line. I hope that makes sense and is helpful to anyone, ive attached code below for better understanding.
setConstant = position of your constant line.
The code below is placed in a loop:
indLine[0][i]= XYIndLine.getXValue(1, i);
indLine[1][i] = XYIndLine.getYValue(1, i);
constant[0][i] = XYIndLine.getXValue(1, i);
constant[1][i] = Math.min(setConstant, XYIndLine.getYValue);
once this is done then use addSeries to add the two arrays to a DefaultXYDataset

Categories

Resources