Skip to content

Instantly share code, notes, and snippets.

@jhumigas
Last active October 13, 2016 19:18
Show Gist options
  • Save jhumigas/935cbce42326a853c4db483df8708f61 to your computer and use it in GitHub Desktop.
Save jhumigas/935cbce42326a853c4db483df8708f61 to your computer and use it in GitHub Desktop.
Sine in Java between three points
import java.awt.geom.Point2D;
public class Sinus{
/**
* Computes the sine of the angle between three points.
* Use the properties of the cross product between two vectors in a 2D euclidean space.
* The positive angles are counter-clockwise.
* @param pt1 Point defined by a location in a coordinate space
* @param pt2 Point defined by a location in a coordinate space
* @param pt3 Point defined by a location in a coordinate space
* @return the value of sin between the vector p2p3 and p2p1
*/
public static double sine(Point2D pt1, Point2D pt2, Point2D pt3){
// Arrays to store the coordinates of the vectors
double a[] = new double[2];
double b[] = new double[2];
// Computing p3p2 and p1p2 vectors
a[0] = pt3.getX() - pt2.getX();
a[1] = pt3.getY() - pt2.getY();
b[0] = pt1.getX() - pt2.getX();
b[1] = pt1.getY() - pt2.getY();
return (a[0]*b[1] - a[1]*b[0])/(pt3.distance(pt2) * pt1.distance(pt2));
}
public static void main(String[] args) {
Point2D pt1 = new Point2D.Double(0, 10);
Point2D pt2 = new Point2D.Double(0, 0);
Point2D pt3 = new Point2D.Double(10, 0);
System.out.println(sine(pt1, pt2, pt3));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment