0

Hi this a part of my code : I have a list which its size is three and I consider that 2 last items are in one line (p and q) I need to get the angle between the first item of this list and these two points (p,q)

    private Point partition(List<Point> list, Point p, Point q) {



    double x1 = p.getX();
    double x2 = q.getX();
    double y1 = p.getY();
    double y2 = q.getY();
    double pQ = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
    for (int i = 0; i < list.size()-2; i++) {
        double pointX = list.get(i).getX();
        double pointY = list.get(i).getY();
        double pointQ = Math.sqrt((x2 - pointX) * (x2 - pointX) + (y2 - pointY) * (y2 - pointY));
        double pointP = Math.sqrt((pointX - x1) * (pointX - x1) + (pointY - y1) * (pointY - y1));
        double angle = Math.acos((pQ * pQ - pointP * pointP - pointQ * pointQ) /(- 2 * pointP * pointQ));
        System.out.println(angle);


    }

but instead of printing an angle for the first item it will print :(first item is not in the line of two last items).

1.6288442476732894

those points that print this result are :

[X :143.0  Y: 217.0, X :93.0  Y: 163.0, X :193.0  Y: 165.0]

please help me thanks.

EDITED : really it makes me confused .in such a way it will print this value ,sorry all !!!

user472221
  • 3,014
  • 11
  • 35
  • 42

2 Answers2

2

Your arccos is bad, you need a parenthesis and a - :

 arccos((pQ^2 - pointP^2 - pointQ^2)/(-2 * pointP * pointQ))

see How to calculate an angle from three points?

Community
  • 1
  • 1
0

NaN is a special double value meaning "not a number". It is generated as a result of some "bad" calculations:

  • dividing 0 by 0
  • dividing infinity by infinity (any combination of positive and negative)
  • multiplying 0 by infinity, either positive and negative, and vice-versa
  • adding negative and positive infinity
  • subtracting negative from positive infinity, and vice-versa
  • the square root of a negative number
  • the logarithm of a negative number
  • the inverse sine or cosine of a number not between -1 and 1
  • any calculation involving one or more NaN values.

Check the result of the argument to the inverse cosine function (acos). I bet it is off the valid range.

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510