Skip to content

Instantly share code, notes, and snippets.

@danicheman
Created February 11, 2016 19:02
Show Gist options
  • Select an option

  • Save danicheman/68514c6bfb3530cbddf1 to your computer and use it in GitHub Desktop.

Select an option

Save danicheman/68514c6bfb3530cbddf1 to your computer and use it in GitHub Desktop.

Revisions

  1. danicheman created this gist Feb 11, 2016.
    60 changes: 60 additions & 0 deletions Airplane.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    public class Airways {
    public double distance(int n, int east, int north) {
    double distance = 0;
    double angleInterval = 360 / n;
    //form a triangle using the angles before and after the east/north position angle
    int quadrantOffset = quadrantOffset(east, north);
    double offset = Math.atan2(Math.abs(east), Math.abs(north));
    double totalOffset = offset + quadrantOffset;

    double beforeAngle = 0;
    double afterAngle = 0;
    //get nearest angles before and after totalOffset
    for(double angleIter = 0; angleIter < 360; angleIter += angleInterval) {
    if(angleIter < totalOffset) {
    beforeAngle = angleIter;
    } else {
    afterAngle = angleIter;
    break;
    }
    }

    //pythagorus
    double hyp = Math.sqrt(Math.pow(east,2) + Math.pow(north,2));

    //start with afterAngle
    if (afterAngle == totalOffset) {
    return hyp;
    } else {
    //math time!
    //find the angles to the hypotenuse
    double angle = totalOffset - beforeAngle;
    //double angle2 = afterAngle - totalOffset;

    //soh cah, solve for o, a
    double opp = hyp * Math.sin(angle);
    double adj = hyp * Math.cos(angle);

    return opp + adj;
    }

    // if afterangle > totalOffset

    }

    int quadrantOffset(int east, int north) {
    if(east > 0) {
    if(north > 0) {
    return 270;
    } else {
    return 0;
    }
    } else {
    if(north > 0) {
    return 180;
    } else {
    return 90;
    }
    }
    }
    }