Android examples for java.lang:Math Trigonometric Function
Calculates the "minimum" cosine distance between two instances.
/*//from w ww .jav a 2 s . com * Copyright (C) 2008-2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import android.graphics.RectF; import android.util.Log; import java.util.ArrayList; import java.util.Arrays; import java.io.Closeable; import java.io.IOException; public class Main{ /** * Calculates the "minimum" cosine distance between two instances. * * @param vector1 * @param vector2 * @param numOrientations the maximum number of orientation allowed * @return the distance between the two instances (between 0 and Math.PI) */ static float minimumCosineDistance(float[] vector1, float[] vector2, int numOrientations) { final int len = vector1.length; float a = 0; float b = 0; for (int i = 0; i < len; i += 2) { a += vector1[i] * vector2[i] + vector1[i + 1] * vector2[i + 1]; b += vector1[i] * vector2[i + 1] - vector1[i + 1] * vector2[i]; } if (a != 0) { final float tan = b / a; final double angle = Math.atan(tan); if (numOrientations > 2 && Math.abs(angle) >= Math.PI / numOrientations) { return (float) Math.acos(a); } else { final double cosine = Math.cos(angle); final double sine = cosine * tan; return (float) Math.acos(a * cosine + b * sine); } } else { return (float) Math.PI / 2; } } }