Skip to content

Instantly share code, notes, and snippets.

@tarunbod
Created March 15, 2015 23:51
Show Gist options
  • Save tarunbod/8c0c8bc15d5205c1a1d1 to your computer and use it in GitHub Desktop.
Save tarunbod/8c0c8bc15d5205c1a1d1 to your computer and use it in GitHub Desktop.
/**
* TrigCalculator by Tarun Boddupalli
* <p/>
* The MIT License (MIT)
* <p/>
* Copyright (c) 2015 Tarun Boddupalli
* <p/>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p/>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p/>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
public class Triangle {
private double leg1, leg2, hypotenuse, angle1, angle2;
public Triangle() {
leg1 = -1;
hypotenuse = -1;
leg2 = -1;
angle1 = -1;
angle2 = -1;
}
public double getLeg1() {
return leg1;
}
public Triangle setLeg1(double leg1) {
this.leg1 = leg1;
return this;
}
public double getHypotenuse() {
return hypotenuse;
}
public Triangle setHypotenuse(double hypotenuse) {
this.hypotenuse = hypotenuse;
return this;
}
public double getLeg2() {
return leg2;
}
public Triangle setLeg2(double leg2) {
this.leg2 = leg2;
return this;
}
public double getAngle1() {
return angle1;
}
public Triangle setAngle1(double angle1) {
this.angle1 = angle1;
return this;
}
public double getAngle2() {
return angle2;
}
public Triangle setAngle2(double angle2) {
this.angle2 = angle2;
return this;
}
public void calculate() throws InvalidTriangleException {
if (getAngle1() != -1 || getAngle2() != -1) {
if (getLeg1() != -1 || getLeg2() != -1 || getHypotenuse() != -1) {
if (getAngle1() != -1) {
if (getLeg1() != -1) {
setHypotenuse(getLeg1() / Math.cos(Math.toRadians(getAngle1())));
setLeg2(Math.tan(Math.toRadians(getAngle1())) * getLeg1());
} else if (getLeg2() != -1) {
setHypotenuse(getLeg2() / Math.sin(Math.toRadians(getAngle1())));
setLeg1(getLeg2() / Math.tan(Math.toRadians(getAngle1())));
} else if (getHypotenuse() != -1) {
setLeg1(Math.cos(Math.toRadians(getAngle1())) * getHypotenuse());
setLeg2(Math.sin(Math.toRadians(getAngle1())) * getHypotenuse());
}
setAngle2(180 - (90 + getAngle1()));
} else if (getAngle2() != -1) {
if (getLeg1() != -1) {
setHypotenuse(getLeg1() / Math.sin(Math.toRadians(getAngle2())));
setLeg2(getLeg1() / Math.tan(Math.toRadians(getAngle2())));
} else if (getLeg2() != -1) {
setHypotenuse(getLeg2() / Math.cos(Math.toRadians(getAngle2())));
setLeg1(Math.tan(Math.toRadians(getAngle2())) * getLeg2());
} else if (getHypotenuse() != -1) {
setLeg1(Math.sin(Math.toRadians(getAngle2())) * getHypotenuse());
setLeg2(Math.cos(Math.toRadians(getAngle2())) * getHypotenuse());
}
setAngle1(180 - (90 + getAngle2()));
}
} else {
throw new InvalidTriangleException();
}
} else if ((getLeg1() != -1 && getLeg2() != -1) || (getLeg1() != -1 && getHypotenuse() != -1) || (getLeg2() != -1 && getHypotenuse() != -1)) {
if (getLeg1() != -1 && getLeg2() != -1) {
setHypotenuse(Math.sqrt((getLeg1() * getLeg1()) + (getLeg2() * getLeg2())));
setAngle1(Math.toDegrees(Math.atan(getLeg2() / getLeg1())));
setAngle2(Math.toDegrees(Math.atan(getLeg1() / getLeg2())));
} else if (getLeg1() != -1 && getHypotenuse() != -1) {
setLeg2(Math.sqrt((getHypotenuse() * getHypotenuse()) - (getLeg1() * getLeg1())));
setAngle1(Math.toDegrees(Math.acos(getLeg1() / getHypotenuse())));
setAngle2(Math.toDegrees(Math.asin(getLeg1() / getHypotenuse())));
} else if (getLeg2() != -1 && getHypotenuse() != -1) {
setLeg1(Math.sqrt((getHypotenuse() * getHypotenuse()) - (getLeg2() * getLeg2())));
setAngle1(Math.toDegrees(Math.asin(getLeg2() / getHypotenuse())));
setAngle2(Math.toDegrees(Math.acos(getLeg2() / getHypotenuse())));
}
} else {
throw new InvalidTriangleException();
}
}
public String toString() {
return String.format("Triangle: m<1: %f, m<2: %f, L1: %f, L2: %f, HYP: %f", getAngle1(), getAngle2(), getLeg1(), getLeg2(), getHypotenuse());
}
/**
* Calculates a right Triangle when given
* an angle and a side, or two sides.
* Represented as shown:
*
* *
* * *
* * 1 *
* Leg 1 * * Hypotenuse
* * *
* * 2 *
* * * * * * * *
* Leg 2
* Where 1 is Angle 1, and 2 is Angle 2.
* Sample code shown below.
*/
public static void main(String[] args) {
Triangle t = new Triangle()
.setAngle1(30)
.setLeg2(5);
/**
*
* *
* * *
* * *
* ? * 30 * ?
* * *
* * ? *
* * * * * * * *
* 5
*/
t.calculate();
/**
*
* *
* * *
* * *
* 5 √3 * 30 * 10
* * *
* * 60 *
* * * * * * * *
* 5
*/
System.out.println(t);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment