Skip to content

Commit 8d85354

Browse files
committed
Clean up RightTriangleAbstract and convert to immutable.
1 parent ff67668 commit 8d85354

File tree

1 file changed

+74
-108
lines changed

1 file changed

+74
-108
lines changed

DecimalEx/RightTriangleAbstract.cs

Lines changed: 74 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,26 @@
88
/// </remarks>
99
public struct RightTriangleAbstract
1010
{
11-
12-
private decimal _lengthA;
13-
private decimal _angleA;
14-
private decimal _lengthB;
15-
private decimal _angleB;
16-
17-
private decimal _hypotenuse;
11+
/// <summary>
12+
/// Gets the angle adjacent to side A. Angle is in degrees.
13+
/// </summary>
14+
public decimal AngleA { get; private set; }
15+
/// <summary>
16+
/// Gets the angle adjacent to side B. Angle is in degrees.
17+
/// </summary>
18+
public decimal AngleB { get; private set; }
19+
/// <summary>
20+
/// Gets the length of the hypotenuse.
21+
/// </summary>
22+
public decimal Hypotenuse { get; private set; }
23+
/// <summary>
24+
/// Gets the length of side A.
25+
/// </summary>
26+
public decimal LengthA { get; private set; }
27+
/// <summary>
28+
/// Gets the length of side B.
29+
/// </summary>
30+
public decimal LengthB { get; private set; }
1831

1932
/// <summary>
2033
/// Creates a right triangle from its two sides.
@@ -23,138 +36,91 @@ public struct RightTriangleAbstract
2336
/// <param name="lengthB">Length of side B.</param>
2437
public static RightTriangleAbstract FromTwoSides(decimal lengthA, decimal lengthB)
2538
{
26-
var t = default(RightTriangleAbstract);
27-
28-
t._FromTwoSides(lengthA, lengthB);
29-
39+
var t = new RightTriangleAbstract
40+
{
41+
LengthA = lengthA,
42+
LengthB = lengthB,
43+
Hypotenuse = RightTriangle.GetHypFromSides(lengthA, lengthB),
44+
AngleA = RightTriangle.GetAngleFromSides(lengthB, lengthA),
45+
AngleB = RightTriangle.GetAngleFromSides(lengthA, lengthB)
46+
};
3047
return t;
3148
}
3249

33-
private void _FromTwoSides(decimal lengthA, decimal lengthB)
34-
{
35-
_lengthA = lengthA;
36-
_lengthB = lengthB;
37-
_hypotenuse = RightTriangle.GetHypFromSides(lengthA, lengthB);
38-
_angleA = RightTriangle.GetAngleFromSides(lengthB, lengthA);
39-
_angleB = RightTriangle.GetAngleFromSides(lengthA, lengthB);
40-
41-
}
4250
/// <summary>
4351
/// Creates a right triangle from one side and the hypotenuse.
52+
/// This side is treated as side A.
4453
/// </summary>
4554
/// <param name="lengthA">Length of side A.</param>
4655
/// <param name="hypotenuse">Length of the hypotenuse.</param>
47-
public static object FromSideAHypotenuse(decimal lengthA, decimal hypotenuse)
56+
public static RightTriangleAbstract FromSideAHypotenuse(decimal lengthA, decimal hypotenuse)
4857
{
49-
50-
RightTriangleAbstract t = default(RightTriangleAbstract);
51-
52-
t._FromSideAHypotenuse(lengthA, hypotenuse);
53-
58+
var t = new RightTriangleAbstract
59+
{
60+
LengthA = lengthA,
61+
Hypotenuse = hypotenuse,
62+
LengthB = RightTriangle.GetSideFromSideHyp(lengthA, hypotenuse),
63+
AngleA = RightTriangle.GetAngleFromAdjSideHyp(lengthA, hypotenuse),
64+
AngleB = RightTriangle.GetAngleFromOppSideHyp(lengthA, hypotenuse)
65+
};
5466
return t;
55-
5667
}
5768

58-
private void _FromSideAHypotenuse(decimal lengthA, decimal hypotenuse)
59-
{
60-
_lengthA = lengthA;
61-
_hypotenuse = hypotenuse;
62-
_lengthB = RightTriangle.GetSideFromSideHyp(lengthA, hypotenuse);
63-
_angleA = RightTriangle.GetAngleFromAdjSideHyp(lengthA, hypotenuse);
64-
_angleB = RightTriangle.GetAngleFromOppSideHyp(lengthA, hypotenuse);
65-
66-
}
6769
/// <summary>
6870
/// Creates a right triangle from one side and its adjacent angle in degrees.
71+
/// This side and angle are treated as side/angle A.
6972
/// </summary>
7073
/// <param name="lengthA">Length of side A.</param>
7174
/// <param name="angleA">Angle adjacent to side A in degrees.</param>
72-
public static object FromSideAAngleA(decimal lengthA, decimal angleA)
75+
public static RightTriangleAbstract FromSideAAngleA(decimal lengthA, decimal angleA)
7376
{
74-
75-
RightTriangleAbstract t = default(RightTriangleAbstract);
76-
77-
t._FromSideAAngleA(lengthA, angleA);
78-
77+
var t = new RightTriangleAbstract
78+
{
79+
LengthA = lengthA,
80+
AngleA = angleA,
81+
LengthB = RightTriangle.GetSideFromOppAngleOppSide(angleA, lengthA),
82+
AngleB = RightTriangle.GetAngleFromOtherAngle(angleA),
83+
Hypotenuse = RightTriangle.GetHypFromSideAdjAngle(lengthA, angleA)
84+
};
7985
return t;
80-
8186
}
8287

83-
private void _FromSideAAngleA(decimal lengthA, decimal angleA)
84-
{
85-
_lengthA = lengthA;
86-
_angleA = angleA;
87-
_lengthB = RightTriangle.GetSideFromOppAngleOppSide(angleA, lengthA);
88-
_angleB = RightTriangle.GetAngleFromOtherAngle(angleA);
89-
_hypotenuse = RightTriangle.GetHypFromSideAdjAngle(lengthA, angleA);
90-
91-
// tan(A) = LB / LA
92-
// LB = tan(A) * LA
93-
_lengthB = DecimalEx.Tan(DecimalEx.ToRad(angleA)) * lengthA;
94-
_angleB = 90m - angleA;
95-
96-
// cos(A) = LA / H
97-
// H = LA / cos(A)
98-
_hypotenuse = lengthA / DecimalEx.Cos(DecimalEx.ToRad(angleA));
99-
100-
}
101-
102-
private void _FromSideBAngleB(decimal lengthB, decimal angleB)
103-
{
104-
_lengthB = lengthB;
105-
_angleB = angleB;
106-
107-
// tan(B) = LA / LB
108-
// LA = tan(B) * LB
109-
_lengthA = DecimalEx.Tan(DecimalEx.ToRad(angleB)) * lengthB;
110-
_angleA = 90m - angleB;
111-
112-
// cos(B) = LB / H
113-
// H = LB / cos(B)
114-
_hypotenuse = lengthB / DecimalEx.Cos(angleB);
115-
116-
}
117-
/// <summary>
118-
/// Gets or sets the angle adjacent to side A. Angle is in degrees.
119-
/// </summary>
120-
public decimal AngleA
121-
{
122-
get { return _angleA; }
123-
set { _FromSideAAngleA(_lengthA, value); }
124-
}
12588
/// <summary>
126-
/// Gets or sets the angle adjacent to side B. Angle is in degrees.
127-
/// </summary>
128-
public decimal AngleB
129-
{
130-
get { return _angleB; }
131-
set { _FromSideBAngleB(_lengthB, value); }
132-
}
133-
/// <summary>
134-
/// Gets or sets the length of the hypotenuse.
89+
/// Creates a right triangle from one side and the hypotenuse.
90+
/// This side is treated as side B.
13591
/// </summary>
136-
public decimal Hypotenuse
92+
/// <param name="lengthA">Length of side B.</param>
93+
/// <param name="hypotenuse">Length of the hypotenuse.</param>
94+
public static RightTriangleAbstract FromSideBHypotenuse(decimal lengthA, decimal hypotenuse)
13795
{
138-
get { return _hypotenuse; }
139-
set { _hypotenuse = value; }
96+
return FromSideAHypotenuse(lengthA, hypotenuse).SwapSides();
14097
}
98+
14199
/// <summary>
142-
/// Gets or sets the length of side A.
100+
/// Creates a right triangle from one side and its adjacent angle in degrees.
101+
/// This side and angle are treated as side/angle A.
143102
/// </summary>
144-
public decimal LengthA
103+
/// <param name="lengthB">Length of side B.</param>
104+
/// <param name="angleB">Angle adjacent to side B in degrees.</param>
105+
public static RightTriangleAbstract FromSideBAngleB(decimal lengthB, decimal angleB)
145106
{
146-
get { return _lengthA; }
147-
set { _FromSideAAngleA(value, _angleA); }
107+
return FromSideAAngleA(lengthB, angleB).SwapSides();
148108
}
109+
149110
/// <summary>
150-
/// Gets or sets the length of side B.
111+
/// Swaps which sides / angles are considered A and B.
151112
/// </summary>
152-
public decimal LengthB
113+
public RightTriangleAbstract SwapSides()
153114
{
154-
get { return _lengthB; }
155-
set { _FromSideBAngleB(value, _angleB); }
115+
var t = new RightTriangleAbstract
116+
{
117+
LengthA = LengthB,
118+
AngleA = AngleB,
119+
LengthB = LengthA,
120+
AngleB = AngleA,
121+
Hypotenuse = Hypotenuse,
122+
};
123+
return t;
156124
}
157-
158125
}
159-
160-
}
126+
}

0 commit comments

Comments
 (0)