C# PointBetween
Dark Mode
In this section were going to finish the PointBetween class and create Unit Tests for it.
Let’ start of by defining a new Interface and Class for PointBetween.
C#
x
17
17
1
//IPointBetween.cs
2
using GeometryCoreProject.WireframeBaseInterfaces;
3
namespace GeometryTypesProject.GeometryTypesInterfaces
4
{
5
public interface IPointBetween : IPointBase3D
6
{
7
IPointBase3D RefPoint1 { get;}
8
IPointBase3D RefPoint2 { get;}
9
bool Orientation { get; }
10
void SetRefPoint1(IPointBase3D iRefPoint);
11
void SetRefPoint2(IPointBase3D iRefPoint);
12
void SetRefPoints(IPointBase3D iRefPoint1, IPointBase3D iRefPoint2);
13
void SetOrientation(bool iOrientation);
14
void SetRatio(double iRatio);
15
double GetRatio();
16
}
17
}
Then implement the PointBetween Class.
C#
1
90
90
1
//PointBetween.cs
2
using GeometryCoreProject.WireframeBaseClasses;
3
using GeometryCoreProject.WireframeBaseInterfaces;
4
using GeometryTypesProject.GeometryTypesInterfaces;
5
using System;
6
namespace GeometryTypesProject.GeometryTypesClasses
7
{
8
public class PointBetween : PointBase3D, IPointBetween
9
{
10
double _Ratio = 0.5;
11
IVectorBase3D _RefVector
12
{
13
get
14
{
15
IVectorBase3D ReturnVector = new VectorBase3D();
16
ReturnVector.I = (this.RefPoint2.X - this.RefPoint1.X);
17
ReturnVector.J = (this.RefPoint2.Y - this.RefPoint1.Y);
18
ReturnVector.K = (this.RefPoint2.Z - this.RefPoint1.Z);
19
return ReturnVector;
20
}
21
}
22
public IPointBase3D RefPoint1 { get; private set; } = null;
23
public IPointBase3D RefPoint2 { get; private set; } = null;
24
public bool Orientation { get; private set; } = true;
25
public PointBetween() { }
26
public PointBetween(IPointBase3D iPoint1, IPointBase3D iPoint2)
27
{
28
this.RefPoint1 = iPoint1;
29
this.RefPoint2 = iPoint2;
30
IPointBase3D TranslatedPoint = this.TranslatePointAlongVector(this.RefPoint1, this._RefVector, this._RefVector.Magnitude * this._Ratio);
31
this.SetCoordinates(TranslatedPoint.GetCoordinates());
32
}
33
public PointBetween(IPointBase3D iPoint1, IPointBase3D iPoint2, double iRatio)
34
{
35
this.RefPoint1 = iPoint1;
36
this.RefPoint2 = iPoint2;
37
this.SetRatio(iRatio);
38
IPointBase3D TranslatedPoint = this.TranslatePointAlongVector(this.RefPoint1, this._RefVector, this._RefVector.Magnitude * this._Ratio);
39
this.SetCoordinates(TranslatedPoint.GetCoordinates());
40
}
41
public PointBetween(IPointBase3D iPoint1, IPointBase3D iPoint2, double iRatio, bool iOrientation)
42
{
43
this.RefPoint1 = iPoint1;
44
this.RefPoint2 = iPoint2;
45
this.SetRatio(iRatio);
46
double CurrentRatio = (iOrientation == true) ? iRatio : 1 - iRatio;
47
IPointBase3D TranslatedPoint = this.TranslatePointAlongVector(this.RefPoint1, this._RefVector, this._RefVector.Magnitude * CurrentRatio);
48
this.SetCoordinates(TranslatedPoint.GetCoordinates());
49
}
50
public void SetRefPoint1(IPointBase3D iRefPoint)
51
{
52
this.RefPoint1 = iRefPoint;
53
this.UpdateThis();
54
}
55
public void SetRefPoint2(IPointBase3D iRefPoint)
56
{
57
this.RefPoint2 = iRefPoint;
58
this.UpdateThis();
59
}
60
public void SetRefPoints(IPointBase3D iRefPoint1, IPointBase3D iRefPoint2)
61
{
62
this.RefPoint1 = iRefPoint1;
63
this.RefPoint2 = iRefPoint2;
64
this.UpdateThis();
65
}
66
public void SetOrientation(bool iOrientation)
67
{
68
this.Orientation = iOrientation;
69
this.UpdateThis();
70
}
71
public double GetRatio()
72
{
73
return this._Ratio;
74
}
75
public void SetRatio(double iRatio)
76
{
77
if (Math.Abs(iRatio) > 1 || iRatio < 0) { throw new Exception("Ratio Value Must be Between 0 and 1."); }
78
this._Ratio = (this.Orientation == true) ? iRatio : 1 - iRatio;
79
this.UpdateThis();
80
}
81
void UpdateThis()
82
{
83
IPointBase3D RefPoint = (this.Orientation == true) ? this.RefPoint1 : this.RefPoint2;
84
IVectorBase3D RefVector = (this.Orientation == true) ? this._RefVector : this._RefVector.InverseVector;
85
IPointBase3D TranslatedPoint = this.TranslatePointAlongVector(RefPoint, RefVector, this._RefVector.Magnitude * this._Ratio);
86
this.SetCoordinates(TranslatedPoint.GetCoordinates());
87
}
88
}
89
}
90
Then in the IWireframeFactory Interface and Class, we’ll add a new method for AddNewPointBetween
C#
1
12
12
1
//IWireframeFactory.cs
2
using GeometryCoreProject.WireframeBaseInterfaces;
3
using GeometryTypesProject.GeometryTypesInterfaces;
4
namespace GeometryFactoriesProject.FactoryBaseInterfaces
5
{
6
public interface IWireframeFactory
7
{
8
IPointBetween AddNewPointBetween(IPointBase3D iPoint1, IPointBase3D iPoint2, double iRatio = 0.5, bool Orientation = true);
9
IPointByCoordinates AddNewPointCoord(double iX, double iY, double iZ);
10
IPointByCoordinates AddNewPointCoordWithReference(double iX, double iY, double iZ, IPointBase3D iRefPoint = null, IAxisSystemBase3D iRefAxisSystem = null);
11
}
12
}
Then in the WireframeFactory Class we will define the new method for AddNewPointBetween
C#
1
42
42
1
//WireframeFactory.cs
2
using GeometryCoreProject.WireframeBaseInterfaces;
3
using GeometryFactoriesProject.FactoryBaseInterfaces;
4
using GeometryTypesProject.GeometryTypesClasses;
5
using GeometryTypesProject.GeometryTypesInterfaces;
6
namespace GeometryFactoriesProject.FactoryBaseClasses
7
{
8
public class WireframeFactory : IWireframeFactory
9
{
10
public IPointBetween AddNewPointBetween(IPointBase3D iPoint1, IPointBase3D iPoint2, double iRatio = 0.5, bool iOrientation = true)
11
{
12
return new PointBetween(iPoint1, iPoint2, iRatio, iOrientation);
13
}
14
public IPointByCoordinates AddNewPointCoord(double iX, double iY, double iZ)
15
{
16
return new PointByCoordinates(iX,iY,iZ);
17
}
18
public IPointByCoordinates AddNewPointCoordWithReference(double iX, double iY, double iZ, IPointBase3D iRefPoint = null, IAxisSystemBase3D iRefAxisSystem = null)
19
{
20
if(iRefPoint == null && iRefAxisSystem ==null)
21
{
22
return new PointByCoordinates(iX, iY, iZ);
23
}
24
else
25
{
26
if (iRefPoint == null)
27
{
28
return new PointByCoordinates(iX, iY, iZ, iRefAxisSystem);
29
}
30
else if (iRefAxisSystem == null)
31
{
32
return new PointByCoordinates(iX, iY, iZ, iRefPoint);
33
}
34
else
35
{
36
return new PointByCoordinates(iX, iY, iZ, iRefPoint, iRefAxisSystem);
37
}
38
}
39
}
40
}
41
}
42
And finally we need to define a Unit Test Class, to do this we need to add a new Class to the existing PointConstructors Unit Test Folder.

We can now fully define the unit tests for PointBetween
C#
1
146
146
1
//PointBetweenUnitTest.cs
2
using GeometryFactoriesProject.FactoryBaseClasses;
3
using GeometryTypesProject.GeometryTypesInterfaces;
4
using System;
5
using Xunit;
6
using FluentAssertions;
7
namespace GeometryUnitTestsProject.PointConstructors
8
{
9
public class PointBetweenUnitTest
10
{
11
// Create an Instance of the Factory
12
WireframeFactory _WireframeFactory = new WireframeFactory();
13
IPointByCoordinates _PointByCoordinates1 = null;
14
IPointByCoordinates _PointByCoordinates2 = null;
15
// Constructor
16
public PointBetweenUnitTest()
17
{
18
this._PointByCoordinates1 = this._WireframeFactory.AddNewPointCoord(10,10,10);
19
this._PointByCoordinates2 = this._WireframeFactory.AddNewPointCoord(100, 100, 100);
20
}
21
[Fact]
22
// Create Point at 55,55,55
23
public void PointBetweenTest1()
24
{
25
// Create an Instance of the IPointByCoordinates
26
IPointBetween PointBetween = this._WireframeFactory.AddNewPointBetween(this._PointByCoordinates1, this._PointByCoordinates2);
27
// Test the Point X,Y,Z Values
28
PointBetween.X.Should().Be(55);
29
PointBetween.Y.Should().Be(55);
30
PointBetween.Z.Should().Be(55);
31
}
32
[Fact]
33
// Create Point at 32.5,32.5,32.5
34
public void PointBetweenTest2()
35
{
36
// Create an Instance of the IPointByCoordinates
37
IPointBetween PointBetween = this._WireframeFactory.AddNewPointBetween(this._PointByCoordinates1, this._PointByCoordinates2, 0.25);
38
// Test the Point X,Y,Z Values
39
PointBetween.X.Should().Be(32.5);
40
PointBetween.Y.Should().Be(32.5);
41
PointBetween.Z.Should().Be(32.5);
42
}
43
[Fact]
44
// Create Point at 77.5,77.5,77.5 by Inverting the Ratio
45
public void PointBetweenTest3()
46
{
47
// Create an Instance of the IPointByCoordinates
48
IPointBetween PointBetween = this._WireframeFactory.AddNewPointBetween(this._PointByCoordinates1, this._PointByCoordinates2, 0.25,false);
49
// Test the Point X,Y,Z Values
50
PointBetween.X.Should().Be(77.5);
51
PointBetween.Y.Should().Be(77.5);
52
PointBetween.Z.Should().Be(77.5);
53
}
54
[Fact]
55
/* Create Point at 32.5,32.5,32.5
56
Then Invert Orientation*/
57
public void PointBetweenTest4()
58
{
59
// Create an Instance of the IPointByCoordinates
60
IPointBetween PointBetween = this._WireframeFactory.AddNewPointBetween(this._PointByCoordinates1, this._PointByCoordinates2, 0.25);
61
PointBetween.SetOrientation(false);
62
// Test the Point X,Y,Z Values
63
PointBetween.X.Should().Be(77.5);
64
PointBetween.Y.Should().Be(77.5);
65
PointBetween.Z.Should().Be(77.5);
66
}
67
[Fact]
68
/* Create Point at 32.5,32.5,32.5
69
Then Change the Ratio to 0.75*/
70
public void PointBetweenTest5()
71
{
72
// Create an Instance of the IPointByCoordinates
73
IPointBetween PointBetween = this._WireframeFactory.AddNewPointBetween(this._PointByCoordinates1, this._PointByCoordinates2, 0.25);
74
PointBetween.SetRatio(0.75);
75
// Test the Point X,Y,Z Values
76
PointBetween.X.Should().Be(77.5);
77
PointBetween.Y.Should().Be(77.5);
78
PointBetween.Z.Should().Be(77.5);
79
}
80
[Fact]
81
/* Create Point at 32.5,32.5,32.5
82
Then Change the Ratio to 0.75*/
83
public void PointBetweenTest6()
84
{
85
// Create an Instance of the IPointByCoordinates
86
IPointBetween PointBetween = this._WireframeFactory.AddNewPointBetween(this._PointByCoordinates1, this._PointByCoordinates2, 0.25);
87
PointBetween.SetRatio(0.75);
88
PointBetween.SetOrientation(false);
89
// Test the Point X,Y,Z Values
90
PointBetween.X.Should().Be(32.5);
91
PointBetween.Y.Should().Be(32.5);
92
PointBetween.Z.Should().Be(32.5);
93
}
94
[Fact]
95
/* Create Point at 32.5,32.5,32.5
96
Then Replace Point 1*/
97
public void PointBetweenTest7()
98
{
99
// Create an Instance of the IPointByCoordinates
100
IPointBetween PointBetween = this._WireframeFactory.AddNewPointBetween(this._PointByCoordinates1, this._PointByCoordinates2, 0.25);
101
// Create a New Point to Replace Point 1
102
IPointByCoordinates PointByCoordinates1New = this._WireframeFactory.AddNewPointCoord(25, 250, 2.5);
103
PointBetween.SetRefPoint1(PointByCoordinates1New);
104
// Test the Point X,Y,Z Values
105
PointBetween.X.Should().Be(43.75);
106
PointBetween.Y.Should().Be(212.5);
107
PointBetween.Z.Should().Be(26.875);
108
}
109
[Fact]
110
/* Create Point at 32.5,32.5,32.5
111
Then Replace Point 1
112
Then Repalce Point 2
113
Change the Ratio to .75
114
Invert the Orientation*/
115
public void PointBetweenTest8()
116
{
117
// Create an Instance of the IPointByCoordinates
118
IPointBetween PointBetween = this._WireframeFactory.AddNewPointBetween(this._PointByCoordinates1, this._PointByCoordinates2, 0.25);
119
// Create a New Point to Replace Point 1
120
IPointByCoordinates PointByCoordinates1New = this._WireframeFactory.AddNewPointCoord(25, 250, 2.5);
121
PointBetween.SetRefPoint1(PointByCoordinates1New);
122
// Create a New Point to Replace Point 2
123
PointBetween.SetRefPoint2(this._PointByCoordinates1);
124
// Change the Ratio
125
PointBetween.SetRatio(0.75);
126
// Change the Orientation
127
PointBetween.SetOrientation(false);
128
// Test the Point X,Y,Z Values
129
PointBetween.X.Should().Be(21.25);
130
PointBetween.Y.Should().Be(190);
131
PointBetween.Z.Should().Be(4.375);
132
}
133
[Fact]
134
/* Create Point at 32.5,32.5,32.5
135
Then Change the Ratio to 1.5 to throw an Error*/
136
public void PointBetweenTest9()
137
{
138
// Create an Instance of the IPointByCoordinates
139
IPointBetween PointBetween = this._WireframeFactory.AddNewPointBetween(this._PointByCoordinates1, this._PointByCoordinates2, 0.25);
140
//Set the ReferencePoint
141
Action comparison = () => { PointBetween.SetRatio(1.5); };
142
comparison.Should().Throw<Exception>();
143
}
144
}
145
}
146