public void Vector_FlipAboutTail()
        {
            Vector v = new Vector(Point.MakePointWithInches(2, 2), Point.MakePointWithInches(4, 3));

            v.FlipAboutTail().Should().Be(new Vector(Point.MakePointWithInches(2, 2), Point.MakePointWithInches(0, 1)));
            v.FlipAboutTail().Should().NotBe(new Vector(Point.MakePointWithInches(3, 3), Point.MakePointWithInches(1, 2)));
        }
 public void Vector_FlipAboutHead()
 {
     Vector v = new Vector(Point.MakePointWithInches(2, 2), Point.MakePointWithInches(4, 3));
     Vector flipped = v.FlipAboutHead();
     flipped.Should().Be(new Vector(Point.MakePointWithInches(6,4), Point.MakePointWithInches(4, 3)));
     flipped.Should().NotBe(new Vector(Point.MakePointWithInches(5, 3), Point.MakePointWithInches(3, 2)));
 }
 private static Polygon _makeRectangle(Distance length, Distance width, Point basePoint = null)
 {
     var vector1 = new Vector(Direction.Right, length);
     var vector2 = new Vector(Direction.Up, width);
     
     var rectangle = Parallelogram(vector1, vector2, basePoint);
     return rectangle;
 }
        public void Vector_DoesNotPointInOppositeDirectionOf()
        {
            Vector vector1 = new Vector(Point.MakePointWithInches(1, 1), Point.MakePointWithInches(4, 1));

            Vector vector2 = new Vector(Point.MakePointWithInches(4, 1), Point.MakePointWithInches(3, 3));

            vector1.HasOppositeDirectionOf(vector2).Should().BeFalse();
        }
        public void Vector_DoesNotPointInSameDirectionAs()
        {
            Vector vector1 = new Vector(Point.MakePointWithInches(1, 1), Point.MakePointWithInches(4, 1));

            Vector vector2 = new Vector(Point.MakePointWithInches(2, 2), Point.MakePointWithInches(4, 4));

            vector1.HasSameDirectionAs(vector2).Should().BeFalse();
        }
        public void Vector_Reverse()
        {
            Point point1 = Point.MakePointWithInches(2, 2);
            Point point2 = Point.MakePointWithInches(4, 3);
            Vector v = new Vector(point1, point2);

            v.Reverse().Should().Be(new Vector(point2, point1));
            v.Reverse().Should().NotBe(new Vector(Point.MakePointWithInches(5, 3), Point.MakePointWithInches(3, 2)));
        }
        private static Polygon _makeRectangle(Vector baseSegment, Distance height, Direction referencePlaneNormal)
        {
            if (referencePlaneNormal == null)
            {
                referencePlaneNormal = Direction.Out;
            }

            Direction heightDirection = referencePlaneNormal.CrossProduct(baseSegment.Direction);
            var heightVector = new Vector(heightDirection, height);
            
            Polygon polygon = Parallelogram(baseSegment, heightVector);

            return polygon;
        }
        private static Polyhedron _makeSolid(Distance length, Distance width, Distance height, Point basePoint = null)
        {
            if (basePoint == null)
            {
                basePoint = Point.Origin;
            }
            Distance zero = Distance.ZeroDistance;

            Vector vector1 = new Vector(new Point(zero, width, zero));
            Vector vector2 = new Vector(new Point(length, zero, zero));
            Vector vector3 = new Vector(new Point(zero, zero, height));

            var solid = MakeParallelepiped(vector1, vector2, vector3, basePoint);
            return solid;
        }
        public void Rotation_Constructor_Matrix()
        {
            Vector vector1 = new Vector(Point.MakePointWithInches(2, 3, -6), Direction.Out, new Distance(1, Inches));
            Vector vector2 = new Vector(Point.MakePointWithInches(-5, 3, 7));

            //A very randomish axis
            Line testAxis = vector1.CrossProduct(vector2).Translate(Point.MakePointWithInches(3, -9, 10));

            Angle angle = new Angle(new Radian(), -173);

            Rotation rotation = new Rotation(testAxis, angle);
            Rotation fromMatrixConstructor = Rotation.RotationFromMatrix(rotation.Matrix);

            (fromMatrixConstructor.RotationAngle == angle.ProperAngle).Should().BeTrue();
            (fromMatrixConstructor.AxisOfRotation == testAxis).Should().BeTrue();
        }
示例#10
0
        public Circle(Point center, Distance radius, Direction normalDirection = null)    
        {
            if (normalDirection == null)
            {
                normalDirection = Direction.Out;
            }
            var vector1 = new Vector(Point.MakePointWithInches(1, 0, 0)).CrossProduct(normalDirection * new Distance(1, Inches));
            var vector2 = new Vector(Point.MakePointWithInches(0, 1, 0)).CrossProduct(normalDirection * new Distance(1, Inches));
            var vector3 = new Vector(Point.MakePointWithInches(0, 0, 1)).CrossProduct(normalDirection * new Distance(1, Inches));
            var chosen = new List<Vector>() { vector1, vector2, vector3 }.MaxBy(v => v.Magnitude);

            var basePoint = center.Translate(chosen.Direction * radius);
            var arc = new Arc(basePoint, basePoint, new Line(center, normalDirection));

            this._Edges = new List<IEdge>() { arc };
            this.NormalLine = new Line(arc.CenterPoint, arc.NormalDirection);
        }
        public void Plane_RotateTest()
        {
            Point testBasePoint = Point.MakePointWithInches(1, 1, -1);            
            Vector testNormalVector = new Vector(Point.MakePointWithInches(0, 2, 3), Point.MakePointWithInches(-3, -2, 0));

            Plane testPlane = new Plane(testNormalVector.Direction, testBasePoint);

            Line rotationAxis = new Line(new Direction(Point.MakePointWithInches(1, 1, 1)), Point.MakePointWithInches(1, -1, -1));
            Angle rotationAngle = new Angle(new Degree(), 212);

            Plane actualResult = testPlane.Rotate(new Rotation(rotationAxis, rotationAngle));

            Point expectedPoint = Point.MakePointWithInches(2.8439301238119032, -1.4640641282085687, -0.37986599560333495);
            Vector expectedVector = new Vector(Point.MakePointWithInches(5.23819525861547, 1.681697053112619, -1.91989231172809), Point.MakePointWithInches(1.3162301967095191, -1.0862708827830958, -5.2299593139264218));
            Plane expectedResult = new Plane(expectedVector.Direction, expectedPoint);

            bool test = expectedPoint == actualResult.BasePoint;
            bool test2 = expectedVector == actualResult.NormalVector;

            (actualResult == expectedResult).Should().BeTrue();
        }
 public static bool AreAllCoplanar(this List<Point> points)
 {
     Vector whatTheNormalShouldBe = null;
     for (int i = 0; i < points.Count; i++)
     {
         for (int j = i + 1; j < points.Count(); j++)
         {
             Vector vector1 = new Vector(points[i], points[j]);
             for (int k = j + 1; k < points.Count(); k++)
             {
                 Vector vector2 = new Vector(points[i], points[k]);
                 if (whatTheNormalShouldBe == null || whatTheNormalShouldBe.Magnitude == Distance.ZeroDistance)
                 {
                     whatTheNormalShouldBe = vector1.CrossProduct(vector2);
                 }
                 if (!whatTheNormalShouldBe.IsPerpendicularTo(vector2))
                 {
                     return false;
                 }
             }
         }
     }
     return true;
 }
 public virtual Solid Extrude(Vector directionVector)
 {
     throw new NotImplementedException();
 }
 public static void Translate(this List<PlaneRegion> passedPlaneRegions, Vector passedVector)
 {
     throw new NotImplementedException();
 }
        public void Vector_AngleBetween()
        {
            Vector vector1 = new Vector(Point.MakePointWithInches(1, 2, 3));
            Vector vector2 = new Vector(Point.MakePointWithInches(-2, 1, 0));
            Vector vector3 = new Vector(Point.MakePointWithInches(-2, 1, 1));
            Vector vector4 = new Vector(Point.MakePointWithInches(1, 0));
            Vector vector5 = new Vector(Point.MakePointWithInches(1, Math.Sqrt(3)));
           
            Angle result1 = vector1.AngleBetween(vector2);
            Angle result2 = vector1.AngleBetween(vector3);
            Angle result3 = vector4.AngleBetween(vector5);

            Angle expectedAngle1 = new Angle(1.57079632679, Radians);
            Angle expectedAngle2 = new Angle(1.23732315, Radians);
            Angle expectedAngle3 = new Angle(60, Degrees);

            (result1 == expectedAngle1).Should().BeTrue();
            (result2 == expectedAngle2).Should().BeTrue();
            (result3 == expectedAngle3).Should().BeTrue();
        }
        public void Vector_ContainsPoint()
        {
            Vector testVector = new Vector(Point.MakePointWithInches(4, 4, -4));
            Point testPoint = Point.MakePointWithInches(2, 2, -2);
            Point pointNotOnVector = Point.MakePointWithInches(2, 2, -3);

            bool resultOn = testVector.Contains(testPoint);
            bool resultNotOn = testVector.Contains(pointNotOnVector);

            resultOn.Should().BeTrue();
            resultNotOn.Should().BeFalse();
        }
        public void Vector_ProjectOntoPlane()
        {
            Vector testSegment = new Vector(Point.MakePointWithInches(2, 5, 3));
            Plane projectOnto = new Plane(Line.XAxis, Line.YAxis);

            Vector result = testSegment.ProjectOntoPlane(projectOnto);

            Vector expected = new Vector(Point.MakePointWithInches(2, 5));

            result.Should().Be(expected);
        }
        public void LineSegment_ConstructorVector_ShouldThrowException_IfVectorLengthIsZero()
        {
            Vector vector = new Vector(Point.Origin);

            Action construct = () => new LineSegment(vector);
            construct.ShouldThrow<Exception>();
        }
        private static void _removeInteriorPoints(Point initial, List<Point> pointsInOrder)
        {
            //First we compute what the normal actually is
            Vector last = new Vector(pointsInOrder[pointsInOrder.Count - 1], initial);
            Vector first = new Vector(initial, pointsInOrder[0]);
            Vector normal = last.CrossProduct(first);
            
            //Now we use a nested loop to allow us to actually edit the list we're looping through
            //The top level allows us to loop atleast as many times as we need to edit.
            //The inner loop restarts whenever we need to remove a point, but we keep track of where are index was
            //the flag tracks when we exited the inner loop normally or because we needed to remove an item
            //if we exited normally we're done
            int numberOfIterations = pointsInOrder.Count;
            int startingIndex = -1;
            for (int j = 0; j < numberOfIterations; j++)
            {
                bool flag = false;
                Point point1, point3, point2 = null;
                for (int i = startingIndex; i + 1 < pointsInOrder.Count; i++)
                {
                    if (i == -1)
                    {
                        point1 = initial;
                    }
                    point1 = pointsInOrder[i];
                    point2 = pointsInOrder[i + 1];
                    point3 = pointsInOrder[i + 2];

                    Vector vector1 = new Vector(point1, point2);
                    Vector vector2 = new Vector(point2, point3);
                    Vector shouldBeNormal = vector1.CrossProduct(vector2);

                    if (shouldBeNormal.Magnitude == Distance.ZeroDistance || !shouldBeNormal.HasSameDirectionAs(normal))
                    {
                        flag = true;
                        startingIndex = i;
                        break;
                    }
                }
                if (flag)
                {
                    pointsInOrder.Remove(point2);
                }
                else
                {
                    break;
                }
            }
        }
        public void LineSegment_ProjectOntoLine_ShouldReturnVectorProjection_IfVectorLengthIsNotZero()
        {
            LineSegment lineSegment = new LineSegment(Point.MakePointWithInches(1, 1));
            Line line = new Line(Point.MakePointWithInches(1, 0), Point.MakePointWithInches(2, 0));

            Vector vector = new Vector(lineSegment.BasePoint, lineSegment.EndPoint);

            lineSegment.ProjectOntoLine(line).Should().Be(new LineSegment(vector.ProjectOntoLine(line)));
        }
        public void LineSegment_GetHashCode_ShouldEqualVectorHashCode()
        {
            Point endPoint = Point.MakePointWithInches(1, 1, 1);

            LineSegment lineSegment = new LineSegment(endPoint);

            Vector vector = new Vector(lineSegment.BasePoint, lineSegment.EndPoint);

            lineSegment.GetHashCode().Should().Be(vector.GetHashCode());
        }
        public void LineSegment_ConstructorPointVector_ShouldCreateLineSegmentFromOrigin_IfPointIsNull()
        {
            Point point = null;
            Vector vector = new Vector(Point.MakePointWithInches(1, 1, 1));

            LineSegment lineSegment = new LineSegment(point, vector);

            lineSegment.Should().Be(new LineSegment(vector));
        }
        public void Plane_PointOnSameSideAs()
        {
            Point testPoint = Point.MakePointWithInches(1, 3, -1);
            Point testPoint2 = Point.MakePointWithInches(-1, -2, 5);
            Point testPoint3 = Point.MakePointWithInches(0, 1, 0);
            Point referencePoint = Point.MakePointWithInches(1, 2, 1);
            Point referencePoint2 = Point.MakePointWithInches(0, 2, 1);
            Vector testNormalVector = new Vector(Point.MakePointWithInches(1, 0, 0));

            Plane testPlane = new Plane(testNormalVector);

            testPlane.PointIsOnSameSideAs(testPoint, referencePoint).Should().BeTrue(); //test one on the same side
            testPlane.PointIsOnSameSideAs(testPoint2, referencePoint).Should().BeFalse(); //test one on the opposite side
            testPlane.PointIsOnSameSideAs(testPoint3, referencePoint).Should().BeFalse(); //test one on the plane
            testPlane.PointIsOnSameSideAs(testPoint, referencePoint2).Should().BeFalse(); //test a reference point on the plane


            Point testPointOffOrigin = Point.MakePointWithInches(5, 4, 0);
            Point testPointOffOrigin2 = Point.MakePointWithInches(5, -2, 0);
            Point referencePointOffOrigin = Point.MakePointWithInches(1, 2, 3);
            Point planeBase = Point.MakePointWithInches(1, -4, 2);
            Vector testNormalVectorOffOrigin = new Vector(Point.MakePointWithInches(-1, 2, 1));

            Plane testPlaneOffOrigin = new Plane(testNormalVectorOffOrigin.Direction, planeBase);

            testPlaneOffOrigin.PointIsOnSameSideAs(testPointOffOrigin, referencePointOffOrigin).Should().BeTrue();
            testPlaneOffOrigin.PointIsOnSameSideAs(testPointOffOrigin2, referencePointOffOrigin).Should().BeFalse();
            testPlaneOffOrigin.PointIsOnSameSideAs(planeBase, referencePointOffOrigin).Should().BeFalse();

        }
        public static Polygon ExteriorProfileFromSegments(this List<LineSegment> segments2D, Vector referenceNormal = null)
        {
            Point firstPoint = segments2D.GetAllPoints().OrderBy(p => p.X).ThenBy(p => p.Y).First();
            List<LineSegment> profileSegments = new List<LineSegment>();

            Point currentPoint = null;
            Vector referenceVector = new Vector(Point.MakePointWithInches(0, -1));
            while (currentPoint != firstPoint)
            {
                if (currentPoint == null)
                {
                    currentPoint = firstPoint;
                }
                var segmentsExtendingFromThisPoint = new List<LineSegment>();
                foreach (var segment in segments2D)
                {
                    if (segment.EndPoint == currentPoint)
                    {
                        segmentsExtendingFromThisPoint.Add(segment.Reverse());
                    }
                    else if (segment.BasePoint == currentPoint)
                    {
                        segmentsExtendingFromThisPoint.Add(segment);
                    }
                }
                var nextSegment = segmentsExtendingFromThisPoint.OrderBy(s => new Angle(s.SignedAngleBetween(referenceVector, referenceNormal))).First();
                profileSegments.Add(nextSegment);
                segments2D.Remove(nextSegment);
                referenceVector = nextSegment.Reverse();
                currentPoint = nextSegment.EndPoint;
            }
            return new Polygon(profileSegments, false);
        }
        // Returns a normalVector of the planeregion.
        // or the zero vector, if the planereg has no sides.
        protected Line _getNormalLine()
        {
            if (this.Edges.Count() > 2)
            {
                Vector vector1 = new Vector(_Edges.OrderBy(s => s.BasePoint.X).ThenBy(s => s.BasePoint.Y).ThenBy(s => s.BasePoint.Z).First());
               // Vector vector2 = new Vector(_Edges.First(s => s.BasePoint == vector1.EndPoint));
                Vector vector2 = null;
                for (int i = 0; i < Edges.Count; i++)
                {
                    if (Edges[i].BasePoint == vector1.EndPoint)
                    {
                        vector2=new Vector(Edges[i]);
                    }
                }

                var normal = vector1.CrossProduct(vector2);
                
                return new Line(_Edges[0].BasePoint, normal.Direction);
            }
            else
            {
                IEdge nonSegment;
                if (!(Edges[0] is LineSegment))
                {
                    nonSegment = Edges[0];
                }
                else if (!(Edges[1] is LineSegment))
                {
                    nonSegment = Edges[0];
                }
                else throw new Exception("Not Allowed.");
                var otherVertex = this.Vertices.First(v => v != nonSegment.BasePoint);
                var normalDirection = nonSegment.InitialDirection.CrossProduct(new LineSegment(nonSegment.BasePoint, otherVertex).Direction);
               return new Line(nonSegment.BasePoint, normalDirection);
            }
        }
        public void LineSegment_ProjectOntoPlane_ShouldReturnVectorProjection_IfVectorLengthIsNotZero()
        {
            LineSegment lineSegment = new LineSegment(Point.MakePointWithInches(1, 1, 1));
            Plane plane = new Plane(Direction.Right);

            Vector vector = new Vector(lineSegment.BasePoint, lineSegment.EndPoint);

            lineSegment.ProjectOntoPlane(plane).Should().Be(new LineSegment(vector.ProjectOntoPlane(plane)));
        }
        public void LineSegment_ConstructorPointVector_ShouldCreateLineSegmentFromPointAndVector()
        {
            Vector vector = new Vector(Point.MakePointWithInches(1, 1, 1));

            LineSegment lineSegment = new LineSegment(Point.Origin, vector);

            lineSegment.Should().Be(new LineSegment(Point.MakePointWithInches(1, 1, 1)));
        }
 /// <summary>
 /// Creates a Translation in the direction of the given vector.
 /// </summary>
 /// <param name="vector"></param>
 public Translation(Vector vector)
     : this(vector.EndPoint - vector.BasePoint) { }
        public void Vector_CrossProduct()
        {
            Vector xAxis = new Vector(Point.Origin, Direction.Right, new Distance(1, Inches));
            Vector yAxis = new Vector(Point.Origin, new Direction(new Angle(90, Degrees)), new Distance(1, Inches));

            Vector result = xAxis.CrossProduct(yAxis);
            Vector expected = new Vector(Point.Origin, new Direction(Angle.ZeroAngle, Angle.ZeroAngle), new Distance(1, Inches));

            result.Should().Be(expected);

            Vector resultParallel = xAxis.CrossProduct(xAxis);
            (resultParallel.Magnitude == ZeroDistance).Should().BeTrue();
        }
        public void LineSegment_ConstructorPointVector_ShouldThrowException_IfTheVectorHasZeroLength()
        {
            Vector vector = new Vector(Point.Origin);

            Action construct = () => new LineSegment(Point.Origin, vector);
            construct.ShouldThrow<Exception>();
        }