C# PointBetween

C# PointBetween

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.

//IPointBetween.cs
using GeometryCoreProject.WireframeBaseInterfaces;
namespace GeometryTypesProject.GeometryTypesInterfaces
{
    public interface IPointBetween : IPointBase3D
    {
        IPointBase3D RefPoint1 { get;}
        IPointBase3D RefPoint2 { get;}
        bool Orientation { get; }
        void SetRefPoint1(IPointBase3D iRefPoint);
        void SetRefPoint2(IPointBase3D iRefPoint);
        void SetRefPoints(IPointBase3D iRefPoint1, IPointBase3D iRefPoint2);
        void SetOrientation(bool iOrientation);
        void SetRatio(double iRatio);
        double GetRatio();
    }
}

Then implement the PointBetween Class.

//PointBetween.cs
using GeometryCoreProject.WireframeBaseClasses;
using GeometryCoreProject.WireframeBaseInterfaces;
using GeometryTypesProject.GeometryTypesInterfaces;
using System;
namespace GeometryTypesProject.GeometryTypesClasses
{
    public class PointBetween : PointBase3D, IPointBetween
    {
        double _Ratio = 0.5;
        IVectorBase3D _RefVector
        {
            get
            {
                IVectorBase3D ReturnVector = new VectorBase3D();
                ReturnVector.I = (this.RefPoint2.X - this.RefPoint1.X);
                ReturnVector.J = (this.RefPoint2.Y - this.RefPoint1.Y);
                ReturnVector.K = (this.RefPoint2.Z - this.RefPoint1.Z);
                return ReturnVector;
            }
        }
        public IPointBase3D RefPoint1 { get; private set; } = null;
        public IPointBase3D RefPoint2 { get; private set; } = null;
        public bool Orientation { get; private set; } = true;
        public PointBetween() { }
        public PointBetween(IPointBase3D iPoint1, IPointBase3D iPoint2)
        {
            this.RefPoint1 = iPoint1;
            this.RefPoint2 = iPoint2;
            IPointBase3D TranslatedPoint = this.TranslatePointAlongVector(this.RefPoint1, this._RefVector, this._RefVector.Magnitude * this._Ratio);
            this.SetCoordinates(TranslatedPoint.GetCoordinates());
        }
        public PointBetween(IPointBase3D iPoint1, IPointBase3D iPoint2, double iRatio)
        {
            this.RefPoint1 = iPoint1;
            this.RefPoint2 = iPoint2;
            this.SetRatio(iRatio);
            IPointBase3D TranslatedPoint = this.TranslatePointAlongVector(this.RefPoint1, this._RefVector, this._RefVector.Magnitude * this._Ratio);
            this.SetCoordinates(TranslatedPoint.GetCoordinates());
        }
        public PointBetween(IPointBase3D iPoint1, IPointBase3D iPoint2, double iRatio, bool iOrientation)
        {
            this.RefPoint1 = iPoint1;
            this.RefPoint2 = iPoint2;
            this.SetRatio(iRatio);
            double CurrentRatio = (iOrientation == true) ? iRatio : 1 - iRatio;
            IPointBase3D TranslatedPoint = this.TranslatePointAlongVector(this.RefPoint1, this._RefVector, this._RefVector.Magnitude * CurrentRatio);
            this.SetCoordinates(TranslatedPoint.GetCoordinates());
        }
        public void SetRefPoint1(IPointBase3D iRefPoint)
        {
            this.RefPoint1 = iRefPoint;
            this.UpdateThis();
        }
        public void SetRefPoint2(IPointBase3D iRefPoint)
        {
            this.RefPoint2 = iRefPoint;
            this.UpdateThis();
        }
        public void SetRefPoints(IPointBase3D iRefPoint1, IPointBase3D iRefPoint2)
        {
            this.RefPoint1 = iRefPoint1;
            this.RefPoint2 = iRefPoint2;
            this.UpdateThis();
        }
        public void SetOrientation(bool iOrientation)
        {
            this.Orientation = iOrientation;
            this.UpdateThis();
        }
        public double GetRatio()
        {
            return this._Ratio;
        }
        public void SetRatio(double iRatio)
        {
            if (Math.Abs(iRatio) > 1 || iRatio < 0) { throw new Exception("Ratio Value Must be Between 0 and 1."); }
            this._Ratio = (this.Orientation == true) ? iRatio : 1 - iRatio;
            this.UpdateThis();
        }
        void UpdateThis()
        {
            IPointBase3D RefPoint = (this.Orientation == true) ? this.RefPoint1 : this.RefPoint2;
            IVectorBase3D RefVector = (this.Orientation == true) ? this._RefVector : this._RefVector.InverseVector;
            IPointBase3D TranslatedPoint = this.TranslatePointAlongVector(RefPoint, RefVector, this._RefVector.Magnitude * this._Ratio);
            this.SetCoordinates(TranslatedPoint.GetCoordinates());
        }
    }
}

Then in the IWireframeFactory Interface and Class, we’ll add a new method for AddNewPointBetween

//IWireframeFactory.cs
using GeometryCoreProject.WireframeBaseInterfaces;
using GeometryTypesProject.GeometryTypesInterfaces;
namespace GeometryFactoriesProject.FactoryBaseInterfaces
{
    public interface IWireframeFactory
    {
        IPointBetween AddNewPointBetween(IPointBase3D iPoint1, IPointBase3D iPoint2, double iRatio = 0.5, bool Orientation = true);
        IPointByCoordinates AddNewPointCoord(double iX, double iY, double iZ);
        IPointByCoordinates AddNewPointCoordWithReference(double iX, double iY, double iZ, IPointBase3D iRefPoint = null, IAxisSystemBase3D iRefAxisSystem = null);
    }
}

Then in the WireframeFactory Class we will define the new method for AddNewPointBetween

//WireframeFactory.cs
using GeometryCoreProject.WireframeBaseInterfaces;
using GeometryFactoriesProject.FactoryBaseInterfaces;
using GeometryTypesProject.GeometryTypesClasses;
using GeometryTypesProject.GeometryTypesInterfaces;
namespace GeometryFactoriesProject.FactoryBaseClasses
{
    public class WireframeFactory : IWireframeFactory
    {
        public IPointBetween AddNewPointBetween(IPointBase3D iPoint1, IPointBase3D iPoint2, double iRatio = 0.5, bool iOrientation = true)
        {
            return new PointBetween(iPoint1, iPoint2, iRatio, iOrientation);
        }
        public IPointByCoordinates AddNewPointCoord(double iX, double iY, double iZ)
        {
            return new PointByCoordinates(iX,iY,iZ);
        }
        public IPointByCoordinates AddNewPointCoordWithReference(double iX, double iY, double iZ, IPointBase3D iRefPoint = null, IAxisSystemBase3D iRefAxisSystem = null)
        {
            if(iRefPoint == null && iRefAxisSystem ==null)
            {
                return new PointByCoordinates(iX, iY, iZ);
            }
            else
            {
                if (iRefPoint == null)
                {
                    return new PointByCoordinates(iX, iY, iZ, iRefAxisSystem);
                }
                else if (iRefAxisSystem == null)
                {
                    return new PointByCoordinates(iX, iY, iZ, iRefPoint);
                }
                else
                {
                    return new PointByCoordinates(iX, iY, iZ, iRefPoint, iRefAxisSystem);
                }
            }
        }
    }
}

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.

Point Between Unit Test Class

We can now fully define the unit tests for PointBetween

//PointBetweenUnitTest.cs
using GeometryFactoriesProject.FactoryBaseClasses;
using GeometryTypesProject.GeometryTypesInterfaces;
using System;
using Xunit;
using FluentAssertions;
namespace GeometryUnitTestsProject.PointConstructors
{
    public class PointBetweenUnitTest
    {
        // Create an Instance of the Factory
        WireframeFactory _WireframeFactory = new WireframeFactory();
        IPointByCoordinates _PointByCoordinates1 = null;
        IPointByCoordinates _PointByCoordinates2 = null;
        // Constructor
        public PointBetweenUnitTest()
        {
            this._PointByCoordinates1 = this._WireframeFactory.AddNewPointCoord(10,10,10);
            this._PointByCoordinates2 = this._WireframeFactory.AddNewPointCoord(100, 100, 100);
        }
        [Fact]
        // Create Point at 55,55,55
        public void PointBetweenTest1()
        {
            // Create an Instance of the IPointByCoordinates
            IPointBetween PointBetween = this._WireframeFactory.AddNewPointBetween(this._PointByCoordinates1, this._PointByCoordinates2);
            // Test the Point X,Y,Z Values
            PointBetween.X.Should().Be(55);
            PointBetween.Y.Should().Be(55);
            PointBetween.Z.Should().Be(55);
        }
        [Fact]
        // Create Point at 32.5,32.5,32.5
        public void PointBetweenTest2()
        {
            // Create an Instance of the IPointByCoordinates
            IPointBetween PointBetween = this._WireframeFactory.AddNewPointBetween(this._PointByCoordinates1, this._PointByCoordinates2, 0.25);
            // Test the Point X,Y,Z Values
            PointBetween.X.Should().Be(32.5);
            PointBetween.Y.Should().Be(32.5);
            PointBetween.Z.Should().Be(32.5);
        }
        [Fact]
        // Create Point at 77.5,77.5,77.5 by Inverting the Ratio
        public void PointBetweenTest3()
        {
            // Create an Instance of the IPointByCoordinates
            IPointBetween PointBetween = this._WireframeFactory.AddNewPointBetween(this._PointByCoordinates1, this._PointByCoordinates2, 0.25,false);
            // Test the Point X,Y,Z Values
            PointBetween.X.Should().Be(77.5);
            PointBetween.Y.Should().Be(77.5);
            PointBetween.Z.Should().Be(77.5);
        }
        [Fact]
        /* Create Point at 32.5,32.5,32.5
        Then Invert Orientation*/
        public void PointBetweenTest4()
        {
            // Create an Instance of the IPointByCoordinates
            IPointBetween PointBetween = this._WireframeFactory.AddNewPointBetween(this._PointByCoordinates1, this._PointByCoordinates2, 0.25);
            PointBetween.SetOrientation(false);
            // Test the Point X,Y,Z Values
            PointBetween.X.Should().Be(77.5);
            PointBetween.Y.Should().Be(77.5);
            PointBetween.Z.Should().Be(77.5);
        }
        [Fact]
        /* Create Point at 32.5,32.5,32.5
        Then Change the Ratio to 0.75*/
        public void PointBetweenTest5()
        {
            // Create an Instance of the IPointByCoordinates
            IPointBetween PointBetween = this._WireframeFactory.AddNewPointBetween(this._PointByCoordinates1, this._PointByCoordinates2, 0.25);
            PointBetween.SetRatio(0.75);
            // Test the Point X,Y,Z Values
            PointBetween.X.Should().Be(77.5);
            PointBetween.Y.Should().Be(77.5);
            PointBetween.Z.Should().Be(77.5);
        }
        [Fact]
        /* Create Point at 32.5,32.5,32.5
        Then Change the Ratio to 0.75*/
        public void PointBetweenTest6()
        {
            // Create an Instance of the IPointByCoordinates
            IPointBetween PointBetween = this._WireframeFactory.AddNewPointBetween(this._PointByCoordinates1, this._PointByCoordinates2, 0.25);
            PointBetween.SetRatio(0.75);
            PointBetween.SetOrientation(false);
            // Test the Point X,Y,Z Values
            PointBetween.X.Should().Be(32.5);
            PointBetween.Y.Should().Be(32.5);
            PointBetween.Z.Should().Be(32.5);
        }
        [Fact]
        /* Create Point at 32.5,32.5,32.5
        Then Replace Point 1*/
        public void PointBetweenTest7()
        {
            // Create an Instance of the IPointByCoordinates
            IPointBetween PointBetween = this._WireframeFactory.AddNewPointBetween(this._PointByCoordinates1, this._PointByCoordinates2, 0.25);
            // Create a New Point to Replace Point 1
            IPointByCoordinates PointByCoordinates1New = this._WireframeFactory.AddNewPointCoord(25, 250, 2.5);
            PointBetween.SetRefPoint1(PointByCoordinates1New);
            // Test the Point X,Y,Z Values
            PointBetween.X.Should().Be(43.75);
            PointBetween.Y.Should().Be(212.5);
            PointBetween.Z.Should().Be(26.875);
        }
        [Fact]
        /* Create Point at 32.5,32.5,32.5
        Then Replace Point 1
        Then Repalce Point 2
        Change the Ratio to .75
        Invert the Orientation*/
        public void PointBetweenTest8()
        {
            // Create an Instance of the IPointByCoordinates
            IPointBetween PointBetween = this._WireframeFactory.AddNewPointBetween(this._PointByCoordinates1, this._PointByCoordinates2, 0.25);
            // Create a New Point to Replace Point 1
            IPointByCoordinates PointByCoordinates1New = this._WireframeFactory.AddNewPointCoord(25, 250, 2.5);
            PointBetween.SetRefPoint1(PointByCoordinates1New);
            // Create a New Point to Replace Point 2
            PointBetween.SetRefPoint2(this._PointByCoordinates1);
            // Change the Ratio
            PointBetween.SetRatio(0.75);
            // Change the Orientation
            PointBetween.SetOrientation(false);
            // Test the Point X,Y,Z Values
            PointBetween.X.Should().Be(21.25);
            PointBetween.Y.Should().Be(190);
            PointBetween.Z.Should().Be(4.375);
        }
        [Fact]
        /* Create Point at 32.5,32.5,32.5
        Then Change the Ratio to 1.5 to throw an Error*/
        public void PointBetweenTest9()
        {
            // Create an Instance of the IPointByCoordinates
            IPointBetween PointBetween = this._WireframeFactory.AddNewPointBetween(this._PointByCoordinates1, this._PointByCoordinates2, 0.25);
            //Set the ReferencePoint
            Action comparison = () => { PointBetween.SetRatio(1.5); };
            comparison.Should().Throw<Exception>();
        }
    }
}