Giter Club home page Giter Club logo

Comments (2)

lweber avatar lweber commented on May 22, 2024

PS - I wrote the following brute force code, based on formulas from Wikipedia, to test against Angle2d and Angle3d. This code appears to give the correct answer, but it seems always to return an angle of 180 deg or less whereas Angle2d and Angle3d will return values of 360 deg or less. This code does seem to run about 50% faster than the current Angle2d and Angle3d code:

/**
* Calculate the angle at the vertex between two vectors formed by three 2d points.
*
* @param a A 2d point.
* @param vertex The vertex point.
* @param b A 2d point.
*
* @return The angle, from 0 to 2 * PI, in radians.
*/
public static double calculateAngle(Coord2d a, Coord2d vertex, Coord2d b) {
double lengthAB = Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));
double lengthAV = Math.sqrt(Math.pow(a.x - vertex.x, 2) + Math.pow(a.y - vertex.y, 2));
double lengthBV = Math.sqrt(Math.pow(b.x - vertex.x, 2) + Math.pow(b.y - vertex.y, 2));
double numerator = Math.pow(lengthAV, 2) + Math.pow(lengthBV, 2) - Math.pow(lengthAB, 2);
double denominator = 2 * lengthAV * lengthBV;
return Math.acos(numerator / denominator);
}

/**
 * Calculate the angle at the vertex between two rays formed by three 3d points.
 * 
 * @param a A 3d point.
 * @param vertex The vertex point.
 * @param b A 3d point.
 * 
 * @return The angle, from 0 to 2 * PI, in radians.
 */
public static double calculateAngle(Coord3d a, Coord3d vertex, Coord3d b) {
    double lengthAB = Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2) + Math.pow(a.z - b.z, 2));
    double lengthAV = Math.sqrt(Math.pow(a.x - vertex.x, 2) + Math.pow(a.y - vertex.y, 2) + Math.pow(a.z - vertex.z, 2));
    double lengthBV = Math.sqrt(Math.pow(b.x - vertex.x, 2) + Math.pow(b.y - vertex.y, 2) + Math.pow(b.z - vertex.z, 2));
    double numerator = Math.pow(lengthAV, 2) + Math.pow(lengthBV, 2) - Math.pow(lengthAB, 2);
    double denominator = 2 * lengthAV * lengthBV;
    return Math.acos(numerator / denominator);
}

Thanks,
Lloyd

from jzy3d-api.

jzy3d avatar jzy3d commented on May 22, 2024

Thank you Lloyd.
Looks like method angle() in Angle2d and Angle3d is not called by any class of the framework, so you might change the contract without breaking the framework.
Almost ready for a pull request? :)

from jzy3d-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.