Finding an equation to connect the max number of possible points - java

I would like to find a way, given a set of any points on a 2 dimensional (or 3 dimensional if possible) plane, to connect as many of these points as possible with an equation, preferably in the form of X^n+BX^n and so on. X being of course a variable, and b and n being any numbers.
This would hopefully work in a way that given say, 50 random points, I would be able to use the equation to draw a line that would pass through as many of these points as possible.
I plan on using this in a compression format where data is converted to X,Y coordinate pairs, the goal is then to create equations that can reproduce these points. The equation would then be stored and the data would be replaced with a pointer to the equation as well as the number to enter into the equation to get the data back.
Any feedback is nice, this is just an idea I thought of during class and wanted to see if it would be possible to implement in a usable format.

To connect n points you need a polynomial of at most degree n-1. You can use Polynomial Regression to form your line.

Related

Euclidean distance between two objects

First of all I know what the Euclidean distance is and what it does or calculates between two vectors.
But my question is about how to calculate the distance between two class objects for example in Java or any other OOP-Language. I read pretty much stuff about machine learning already wrote a classifier using libraries etc. but I want to know how the Euclidean distance is calculated when I have for example this object:
class Object{
String name;
Color color;
int price;
int anotherProperty;
double something;
List<AnotherObject> another;
}
What I already know (If I am not wrong!) is that I have to convert this object to a(n) vector / array representing the properties or 'Features' (called in Machine Learning?)
But how can I do this? It is just this piece of puzzle which I need, to understand even more.
Do I have to collect all possible values for a property to convert it to a number and write it in the array/vector?
Example:
I guess the above object would be represented by an 6-dimensional array or smaller based on the 'Features' which are necessary to calculate.
Let's say Color, Name and the price are those necessary features the array/vector based on the following data:
color: green (Lets say an enum with 5 possible values where green is the third one)
name: "foo" (I would not know how to convert this one maybe using
addition of ascii code?)
price: 14 (Just take the integer?)
would look like this?
[3,324,14]
And if I do this with every Object from the same class I am able to calculate the Euclidean distance. Am I right or did I misunderstand something, or is it completely wrong?
For each data type you need to choose an appropriate method of determing the distance. In many cases each data type may also itself have to be treated as a vector.
For colour, for example, you could express the colour as an RGB value and then take the Euclidian distance (take the 3 differences, square them, sum and then square root). You might want to chose a different colour-space than RGB (e.g., HSI). See here: Colour Difference.
Comparing two strings is easier: a common method is the Levenshtein distance. There is an method in the Apache commons StringUtils class.
Numbers - just take the difference.
Every type will require some consideration for the best way of either generating a distance directly or calculating the a numeric value that can then be subtracted to give a "distance".
Once you have a vector of all of the "values" of all of the fields for each object you can calculate the Euclidian distance (square the differences, sum and square root the sum).
In your case, if you have:
object 1: [3,324,14]
object 2: [5,123,10]
The Euclidian distance is:
sqrt( (3-5)^2 + (324-123)^2 + (14-10)^2 )
But in the case of comparing strings, the Levenshtein algorithm gives you the distance directly without intermediate numbers for the fields.
Think about this problem as a statistics problem. Classify all the attributes into nominal, ordinal, and scale variables. Once you have done that, it is just a multiple dimension distance vector problem.

Java: Reduce array to specific number of averages

The main issue which needs to be solved is:
Let's say I have an array with 8 numbers, e.g. [2,4,8,3,5,4,9,2] and I use them as values for my x axis in an coordinate system to draw a line. But I can only display 3 of this points.
What I need to do now is do reduce the number of points (8) to 3, without manipulating the line too much - so using an average should be an option.
I am NOT looking for the average of the array in a whole - I still need 3 points of the amount of 8 in total.
For an array like [2,4,2,4,2,4,2,4] and 4 numbers out of that array, I could simply use the average "3" of each pair - but that's not possible if the number is uneven.
But how would I do that? Do you know how this process is called in a mathematical way?
To give you some more realistic details about this issue: I have an x axis, which is 720px long and let's say I get 1000 points. Now I have to reduce this 1000 points (2 arrays, one for x and one for y values) to a maximum of 720 points.
Thought about interpolation and stuff like that, but I'm still not quite sure if this is what I am looking for.
Interpolation is good idea. You input your points and get a polynomial function as an output. Then you can use it to draw your line. Check more here : Interpolation over an array (or two)
I would recommend that you fit all the points you have in some fashion and then evaluate at the particular points you need for the display.
There are a myriad of choices for fitting:
Least squares
Piecewise using polynomials or splines
You should consult a text or find a library to help you - something like Apache Commons Math.
It sounds like you are looking for a more advanced mathematical function than a simple average.
I would suggest trying to identify potential algorithms via Mathematica Stack Exchange and then trying to find a Java library that implements any of the potential choices (maybe a new question here).
since its for an X-axis, why not use the
MIN, MAX and (MIN+MAX)/2
for your three points?

Slick2d/lwjgl Curve through Array of Points

I am trying to rewrite the Game "Achtung, die Kurve" (Attention, the Curve)
Now, i am stuck with the Problem:
How can i draw a Curve, which goes through N Points(x/y Coords)
for(int i = 0;i < xList.size();i++)
{
path.lineTo(player1.getShape().getCenterX(),player1.getShape().getCenterY());
}
^This will always create a new Line, so my FPS get to 10 very quickly
Any Help?
This is an algebra problem that can be solved with systems of linear equations. http://en.wikipedia.org/wiki/System_of_linear_equations
Generally, a curve that passes through N points is an (N-1)th degree polynomial. So if you want to find a polynomial that passes through 3 points (e.g. (-1,1), (0, 3), (1, -1)) you would need a quadratic equation like this: ax^2+bx+c=y.
To find the values of a,b, and c, you need the plug the x and y coordinates in, then solve the system of equations.
a(-1)^2+b(-1)+c=1
a(0)^2+b(0)+c=3
a(1)^2+b(1)+c=-1
that simplifiles to
a-b+c=1
c=3
a+b+c=-1
Nicely, we already have c=3. By combining the first equation and the second we can get
2a+2c=0
Since we know c=3, this becomes
2a+3=0
So a=-1.5.
From here we can put these values of a and c into the last equation to get this
-1.5+b+3=-1
Which gives a b=-3.5. Plugging these values of a,b, and c back into the quadratic equation yields this
-1.5x^2-3.5x+3=y
I haven't double checked my math, but if I did it correctly, this will give a quadratic curve that passes through the three points.
Undoubtedly, there is already a library out there for doing this, but I'm sorry to say I don't know what that would be. Hopefully, knowing about the math behind your problem will help you find your answer.

scaling operations on a 2D graph, size of output sequence must not be the same as input

I am looking for an open source package (preferably Java but R or other languages would be ok too) that provides these 2 functions
1) points output_seq[] SCALE(points input_seq[], double factor)
In other words a sequence of doubles (x1,y1), (x2,y2)... is given as input that represents a graph (each point is connected to the next by a straight line) and a scaling factor is given. Then it returns a similar sequence as output. The catch is that the output sequence may have fewer or more elements than the input. For example, if I request magnification by a factor of 2.012 then the output sequence may have twice as many elements as the input. The scaling factor should be a double, not an integer.
Lastly, it's important to return the output sequence as points (doubles), I have very little interest in the actual drawing on a screen beyond proving that it does the right thing.
2) points output_seq[] ROTATE(points input_seq[], double angle)
same as above, except there is no scaling but just rotation, the angle is from 0 to 359.9999 and is given in radians.
The size of the output is always the same as the size of the input.
Again the emphasis is on getting the output sequence as doubles, not so much on the actual drawing on a screen.
If you know the right terminology I should have used then let me know.
Thank you so much.
In Java, Path2D is suitable for 2D floating point coordinates. The lineTo() method will add straight lines to the path. Because Path2D implements the Shape interface, rotate and scale are possible via createTransformedShape(). One approach to interpolation, using PathIterator, is shown here.

Euclidean distance to calculate if something is near something

currently i have using a framework and it has a function called distance2D, and it has this description:
Calculate the Euclidean distance
between two points (considering a
point as a vector object). Disregards
the Z component of the vectors and is
thus a little faster.
and this is how i use it
if(g.getCenterPointGlobal().distance2D(target.getCenterPointGlobal()) > 1)
System.out.println("Near");
i have totally no idea what a Euclidean distance is, i am thinking that it can be used to calculate how far 2 points are? because i am trying to compare distance between 2 objects and if they are near within a certain range i want to do something. how would i be able to use this?
Euclidean distance is the distance between 2 points as if you were using a ruler. I don't know what are the dimensions of your Euclidean space, but be careful because the function you are using just takes in consideration the first two dimensions (x,y). Thus if you have a space with 3 dimensions(x,y,z) it will only use the first two(x,y of x,y,z) to calculate the distance. This may give a wrong result.
For what I understood, if you want to trigger some action when two points are within some range you should make:
<!-- language: lang-java -->
if(g.getCenterPointGlobal().distance2D(target.getCenterPointGlobal()) < RANGE)
System.out.println("Near");
The Euclidean distance is calculated tracing a straight line between two points and measuring as the hypotenuse of a imaginary isosceles triangle between the two lines and a complementary point. This measure is scalar, so it's a good metric for your calculations.
Euclidean geometry is a coordinate system in which space is flat, not curved. You don't need to care about non-Euclidean geometry unless for example you're dealing with coordinates mapped onto a sphere, such as calculating the shortest travel distance between two places on Earth.
I imagine this function will basically use Pythagoras' theorem to calculate the distance between the two objects. However, as the description says, it disregards the Z component. In otherwords, it will only give a correct answer if both points have the same Z value (aka "depth").
If you wish to compare distances and save time, use not the distance itself, but its square: (x1-x2)^2 + (y1-y2)^2. Don't take sqrt. So, your distances will work exactly as euclidian ones, but quickly.

Categories

Resources