[#2079] Display percentage length unit in stability info

This commit is contained in:
SiboVG 2023-05-24 00:49:12 +02:00
parent d2a9d11a9c
commit 6f3f1ed67d
3 changed files with 52 additions and 11 deletions

View File

@ -456,7 +456,7 @@ public class UnitGroup {
* @param rocket the rocket from which to calculate the caliber
* @return the unit group
*/
public static UnitGroup stabilityUnits(Rocket rocket) {
public static StabilityUnitGroup stabilityUnits(Rocket rocket) {
return new StabilityUnitGroup(rocket);
}
@ -467,7 +467,7 @@ public class UnitGroup {
* @param config the rocket configuration from which to calculate the caliber
* @return the unit group
*/
public static UnitGroup stabilityUnits(FlightConfiguration config) {
public static StabilityUnitGroup stabilityUnits(FlightConfiguration config) {
return new StabilityUnitGroup(config);
}
@ -717,7 +717,8 @@ public class UnitGroup {
* A private class that switches the CaliberUnit to a rocket-specific CaliberUnit.
* All other methods are passed through to UNITS_STABILITY.
*/
private static class StabilityUnitGroup extends UnitGroup {
public static class StabilityUnitGroup extends UnitGroup {
private final PercentageOfLengthUnit percentageOfLengthUnit;
public StabilityUnitGroup(double ref) { this(new CaliberUnit(ref), new PercentageOfLengthUnit(ref)); }
@ -728,6 +729,7 @@ public class UnitGroup {
public StabilityUnitGroup(FlightConfiguration config) { this(new CaliberUnit(config), new PercentageOfLengthUnit(config)); }
private StabilityUnitGroup(CaliberUnit caliberUnit, PercentageOfLengthUnit percentageOfLengthUnit) {
this.percentageOfLengthUnit = percentageOfLengthUnit;
this.units.addAll(UnitGroup.UNITS_STABILITY.units);
this.defaultUnit = UnitGroup.UNITS_STABILITY.defaultUnit;
for (int i = 0; i < units.size(); i++) {
@ -746,5 +748,13 @@ public class UnitGroup {
super.setDefaultUnit(n);
UNITS_STABILITY.setDefaultUnit(n);
}
/**
* Returns the percentage of length unit. (Stability in %)
* @return the percentage of length unit.
*/
public Unit getPercentageOfLengthUnit() {
return this.percentageOfLengthUnit;
}
}
}

View File

@ -41,7 +41,7 @@ public class RocketInfo implements FigureElement {
private final Caret cpCaret = new CPCaret(0,0);
private final Caret cgCaret = new CGCaret(0,0);
private UnitGroup stabilityUnits;
private UnitGroup.StabilityUnitGroup stabilityUnits;
private FlightConfiguration configuration;
private double cg = 0, cp = 0;
@ -201,7 +201,7 @@ public class RocketInfo implements FigureElement {
GlyphVector cgValue = createText(getCg());
GlyphVector cpValue = createText(getCp());
GlyphVector stabValue = createText(getStability());
GlyphVector stabValue = createText(getStabilityCombined());
//// CG:
GlyphVector cgText = createText(trans.get("RocketInfo.cgText"));
@ -276,16 +276,47 @@ public class RocketInfo implements FigureElement {
public String getMassWithMotors(Unit u) {
return u.toStringUnit(massWithMotors);
}
/**
* Get the stability, in calibers.
* Get the stability in both the selected stability unit and in percentage, e.g. "2.4 cal (14.1 %)".
* If the current unit is already the percentage length unit, only use that.
*
* @return the current stability margin
* @return the current stability margin in the currently selected stability unit and in percentage
*/
public String getStability () {
return stabilityUnits.getDefaultUnit().toStringUnit(cp-cg);
public String getStabilityCombined() {
Unit defaultUnit = stabilityUnits.getDefaultUnit();
String stability = getStability();
if (Double.isNaN(getStabilityValue()) || defaultUnit == stabilityUnits.getPercentageOfLengthUnit()) {
return stability;
}
String stabilityPercentage = getStabilityPercentage();
return stability + " (" + stabilityPercentage + ")";
}
/**
* Get the stability in the currently selected unit.
* @return the current stability margin in the currently selected stability unit
*/
private String getStability() {
return stabilityUnits.getDefaultUnit().toStringUnit(getStabilityValue());
}
/**
* Get the stability in the percentage length unit.
* @return the current stability margin in the percentage length unit
*/
private String getStabilityPercentage() {
return stabilityUnits.getPercentageOfLengthUnit().toStringUnit(getStabilityValue());
}
private double getStabilityValue() {
return cp - cg;
}
/**
* Get the center of pressure in default length units.
*

View File

@ -256,7 +256,7 @@ public class DesignReport {
canvas.showText(text.getMassWithMotors(UnitGroup.UNITS_MASS.getDefaultUnit()));
canvas.newlineShowText(STABILITY);
canvas.showText(text.getStability());
canvas.showText(text.getStabilityCombined());
canvas.newlineShowText(CG);
canvas.showText(text.getCg());