public bool TryToMoveVertex(int vertexIndex, FreeVector vector) { var L = _GetMoveVectors(vertexIndex, vector, 1); if (L.Length == 0) { return(false); } var P = _GetMoveVectors(vertexIndex, vector, -1); if (P.Length == 0) { return(false); } P[vertexIndex] = new FreeVector(new PointD(0, 0)); FreeVector[] vectors = new FreeVector[this.NumberOfVertices]; for (int i = 0; i < this.NumberOfVertices; ++i) { if (!L[i].IsEmpty && !P[i].IsEmpty) { return(false); } vectors[i] = L[i] + P[i]; } _MoveVertices(vectors); return(true); }
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(FreeVector.MakeWithInches(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(FreeVector.MakeWithInches(-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 void Direction_Equality_Tests() { Direction direction2 = new Direction(33 * Degrees); Direction direction4 = new Direction(33 * Degrees, RightAngle); (direction2 == direction4).Should().BeTrue(); //Error margin of 0.1 degrees var testVector = FreeVector.MakeWithInches(0.002, 0.001, -1.5); Direction testErrorHandling = new Direction(testVector); Direction expectedErrorDirection = Direction.NegativeZ; (testErrorHandling == expectedErrorDirection).Should().BeTrue(); //now try the reverse Direction too var testVector0 = FreeVector.MakeWithInches(0.002, 0.001, 1.5); Direction testDirection1 = new Direction(testVector0); Direction expected = Direction.PositiveZ; (testDirection1 == expected).Should().BeTrue(); var testVector2 = FreeVector.MakeWithInches(0.003, 0.002, 1.5); Direction testDirection2 = new Direction(testVector2); //Just barely more than a 0.1 degree angle between them (testDirection2 == expected).Should().BeFalse(); }
public PointD[] GetIntersectionPointsWithCircle(Circle other) { FreeVector v = new FreeVector(_middle, other._middle); double d = v.Length; if (d < Math.Abs(_radius - other._radius) || d > _radius + other._radius) { return(new PointD[0]); } PointD[] res = new PointD[2]; double a = (_radius * _radius - other._radius * other._radius + d * d) / (2 * d); FreeVector V0 = new FreeVector(_middle); FreeVector V1 = new FreeVector(other._middle); FreeVector V2 = V0 + (V1 - V0) * a / d; double h = (double)Math.Sqrt(_radius * _radius - a * a); res[0] = new PointD(V2.X - h * (V1.Y - V0.Y) / d, V2.Y + h * (V1.X - V0.X) / d); res[1] = new PointD(V2.X + h * (V1.Y - V0.Y) / d, V2.Y - h * (V1.X - V0.X) / d); return(res); }
/// <summary> /// 提供可读String类型返回 /// </summary> /// <returns></returns> public virtual string ToReadableText(string splitter = ",") { StringBuilder sb = new StringBuilder(); sb.AppendLine(this.GetType().Name); //首先是属性 Geo.Utils.StringUtil.AppendListLine(sb, ParamNames, "ParamNames:"); Geo.Utils.StringUtil.AppendListLine(sb, SecondParamNames, "SecondParamNames:"); if (this.Observation != null) { sb.AppendLine(ObsMatrixType.Observation.ToString()); sb.AppendLine(Observation.ToReadableText(splitter)); } if (this.HasFreeVector) { sb.AppendLine(ObsMatrixType.FreeVector.ToString()); sb.AppendLine(FreeVector.ToReadableText()); } if (this.HasSecondFreeVector) { sb.AppendLine(ObsMatrixType.SecondFreeVector.ToString()); sb.AppendLine(SecondFreeVector.ToReadableText()); } if (this.HasApprox) { sb.AppendLine(ObsMatrixType.ApproxVector.ToString()); sb.AppendLine(this.ApproxVector.ToReadableText()); } if (this.HasSecondApprox) { sb.AppendLine(ObsMatrixType.SecondApproxVector.ToString()); sb.AppendLine(this.SecondApproxVector.ToReadableText()); } if (this.HasApriori) { sb.AppendLine(ObsMatrixType.Apriori.ToString()); sb.AppendLine(this.Apriori.ToReadableText(splitter)); } if (this.Coefficient != null) { sb.AppendLine(ObsMatrixType.Coefficient.ToString()); sb.AppendLine(Matrix.ToReadableText(Coefficient, splitter)); } if (this.SecondCoefficient != null) { sb.AppendLine(ObsMatrixType.SecondCoefficient.ToString()); sb.AppendLine(Matrix.ToReadableText(SecondCoefficient, splitter)); } if (this.HasTransfer) { sb.AppendLine(ObsMatrixType.Transfer.ToString()); sb.AppendLine(this.Transfer.ToReadableText(splitter)); } return(sb.ToString()); }
public bool ArePointsOnTheSameSide(PointD[] points) { FreeVector segmentVector = new FreeVector(_beg, _end); PointD beg = _beg; var r = points.Select(p => (segmentVector.GetDirection(new FreeVector(beg, p)))); return(r.All(x => x == Direction.Anticlockwise) || r.All(x => x == Direction.Clockwise)); }
private void _MoveClipPolygonIfPointTouchesSubject() { FreeVector moveVector = new FreeVector(new Point(6, 0)); while (_clip.HasOnSideAnyPointFrom(_subject) || _subject.HasOnSideAnyPointFrom(_clip)) { _clip._vertexManager.MoveAll(moveVector); } }
protected void _MoveVertices(int[] vertexIndices, FreeVector vector) { foreach (int vertexIndex in vertexIndices) { Vertex v = _vertices[vertexIndex]; v.Location = v.Location + vector; _vertices[vertexIndex] = v; } }
public override bool Move(PointD currentPoint) { FreeVector vector = new FreeVector(_clickedPoint, currentPoint); _currentPolygon._vertexManager.MoveAll(vector); _clickedPoint = currentPoint; return(true); }
public void Vector_ProjectOntoPlane() { Vector testSegment = new Vector(FreeVector.MakeWithInches(2, 5, 3)); Plane projectOnto = new Plane(Line.ZAxis); Vector result = testSegment.ProjectOntoPlane(projectOnto); Vector expected = new Vector(FreeVector.MakeWithInches(2, 5)); result.Should().Be(expected); }
public void Point_Rotate3DTest_AxisNotThroughOrigin() { Point pointToRotate = Point.MakePointWithInches(4, -2, 2); Line axis = new Line(new Direction(FreeVector.MakeWithInches(-1, -5, -3)), Point.MakePointWithInches(2, -2, -3)); Angle rotationAngle = 322 * Degrees; Point newPoint = pointToRotate.Rotate3D(new Rotation(axis, rotationAngle)); newPoint.Should().Be(Point.MakePointWithInches(6.2806322893240427, -1.3811031899761135, 0.20829455351884096)); }
public void Point_IsOnLineWithComponentOfDirectionEqualToZeroTest() { Point testBasePoint = Point.MakePointWithInches(1, 0, 2); Direction testDirection = new Direction(FreeVector.MakeWithInches(0, 3, 1)); Line testLine = new Line(testDirection, testBasePoint); Point pointOnLine = Point.MakePointWithInches(1, 6, 4); pointOnLine.IsOnLine(testLine).Should().BeTrue(); }
public void Shift_ShiftToCoordinateSystem() { Shift system = new Shift(FreeVector.MakeWithInches(1, -2, -4), RightAngle, -45 * Degrees, ZeroAngle); Point testPoint = Point.MakePointWithInches(0, 3, 0); Point shifted = testPoint.Shift(system.Inverse()); Point expected = Point.MakePointWithInches(2.12132034356, 3.53553391, -5); shifted.Should().Be(expected); }
public void Point_TranslateTest() { Point pointToTranslate = Point.MakePointWithInches(1, 2, 3); //Direction directionToTranslate = new Direction(Point.MakePointWithInches(-1, 5, 4)); //Distance displacementOfPoint = 12.9614814 * Unit.Inches; var testDisplacement = FreeVector.MakeWithInches(-2, 10, 8); Point actualResult = pointToTranslate.Translate(testDisplacement); Point expectedResult = Point.MakePointWithInches(-1, 12, 11); actualResult.Should().Be(expectedResult); }
public void Point_MakePerpendicularLineSegmentTest2() { Point destinationLineBasePoint = Point.MakePointWithInches(2, 3, 4); Line destinationLine = new Line(new Direction(FreeVector.MakeWithInches(6, 4, -6)), destinationLineBasePoint); Point testPoint = Point.MakePointWithInches(0, 0, 0); LineSegment actualResult = testPoint.MakePerpendicularLineSegment(destinationLine); LineSegment expectedResult = new LineSegment(testPoint, Point.MakePointWithInches(2, 3, 4)); actualResult.Should().Be(expectedResult); }
public void Plane_IntersectionWithPlane() { Plane testPlane1 = new Plane(new Direction(FreeVector.MakeWithInches(2, -1, 1)), Point.MakePointWithInches(2, 1, 2)); Plane testPlane2 = new Plane(new Direction(FreeVector.MakeWithInches(1, 1, -1)), Point.MakePointWithInches(1, 3, 3)); Line test12Intersect = testPlane1.IntersectWithPlane(testPlane2).As <Line>(); Line test21Intersect = testPlane2.IntersectWithPlane(testPlane1).As <Line>(); Line expectedLine = new Line(new Direction(FreeVector.MakeWithInches(0, 3, 3)), Point.MakePointWithInches(2, -1, 0)); Assert.IsTrue(test12Intersect.Equals(test21Intersect)); Assert.IsTrue(test21Intersect.Equals(expectedLine)); }
public void Point_TranslateTest_OneComponent() { Point pointToTranslate = Point.MakePointWithInches(1, 1, 1); //Direction directionToTranslate = new Direction(Point.MakePointWithInches(1, 0, 0)); //Distance displacementOfPoint = 4 * Unit.Inches; var testDisplacement = FreeVector.MakeWithInches(4, 0, 0); Point actualResult = pointToTranslate.Translate(testDisplacement); Point expectedResult = Point.MakePointWithInches(5, 1, 1); actualResult.Should().Be(expectedResult); }
public void Line_Translate() { var line1 = new Line(Point.MakePointWithInches(1, 2, 3), Point.MakePointWithInches(-3, -2, 0)); //Direction testDirection = new Direction(FreeVector.MakeWithInches(-1, 5, 4)); //Distance testDisplacement = 12.9614814 * Unit.Inches; var testDisplacement = FreeVector.MakeWithInches(-2, 10, 8); var actualLine1 = line1.Translate((testDisplacement)); var expectedLine1 = new Line(Point.MakePointWithInches(-1, 12, 11), Point.MakePointWithInches(-5, 8, 8)); (actualLine1 == expectedLine1).Should().BeTrue(); }
private bool _TryToMoveForSettingRelation(int startIndex, FreeVector vector, int inc) { var L = _GetMoveVectors(startIndex, vector, inc); if (L.Length > 0) { _MoveVertices(L); return(true); } else { return(false); } }
public void Plane_PerpendicularLineTest() { Plane testPlane1 = new Plane(new Direction(FreeVector.MakeWithInches(2, -1, 1)), Point.MakePointWithInches(2, 1, 2)); Plane testPlane2 = new Plane(new Direction(FreeVector.MakeWithInches(1, 1, -1)), Point.MakePointWithInches(1, 3, 3)); Line perpindicular1 = new Line(Point.Origin, Point.MakePointWithInches(2, -1, 1)); Line perpindicular2 = new Line(Point.MakePointWithInches(3, 1, -3), Point.MakePointWithInches(4, 2, -4)); testPlane1.IsPerpendicularTo(perpindicular1).Should().BeTrue(); testPlane1.IsPerpendicularTo(perpindicular2).Should().BeFalse(); testPlane2.IsPerpendicularTo(perpindicular1).Should().BeFalse(); testPlane2.IsPerpendicularTo(perpindicular2).Should().BeTrue(); }
public override bool Move(PointD currentPoint) { PointD oldPoint = _currentPolygon._vertexManager.GetVertex(_vertexIndex).Location; FreeVector vector = new FreeVector(oldPoint, currentPoint); bool res = _currentPolygon._vertexManager.TryToMoveVertex(_vertexIndex, vector); if (res && _relationCheckBox.Checked) // automatic relation { _currentPolygon._vertexManager.AddAutomaticRelations(); } return(res); }
public override void Draw(PaintTools paintTools) { BezierCurve bezierCurve = _polyline.GetBezierCurve(); FreeVector tangentVector = bezierCurve.GetTangentVector(_bezierPointNumber); Point curvePoint = bezierCurve.GetPoint(_bezierPointNumber); float angle = (new FreeVector(new Point(1, 0))).GetAngleWith(tangentVector); PixelSet points = _rotator.GetRotated(angle); FreeVector translateVector = new FreeVector(_imageMiddle, curvePoint); for (int i = 0; i < points.Locations.Length; ++i) { Point translatedPoint = (Point)(points.Locations[i] + translateVector); paintTools.Bitmap.SetPixel(translatedPoint.X, translatedPoint.Y, points.Colors[i]); } }
private bool _ProcessHorizontalRelation(int sideIndex, int vertexIndex, FreeVector[] vector, Func <int, int> next, Func <int, int> prev) { FreeVector sideVector = new FreeVector(_vertices[vertexIndex].Location, _vertices[prev(vertexIndex)].Location + vector[prev(vertexIndex)]); if (sideVector.IsHorizontal) { return(true); } switch (_relations[next(sideIndex)].ToString()) { case "EmptyRelation": vector[vertexIndex] = vector[prev(vertexIndex)]; return(true); case "VerticalRelation": double xw = _vertices[vertexIndex].Location.X; double yw = (_vertices[prev(vertexIndex)].Location + vector[prev(vertexIndex)]).Y; FreeVector v = new FreeVector(_vertices[vertexIndex].Location, new PointD(xw, yw)); vector[vertexIndex] = v; return(true); case "LengthRelation": Circle c = new Circle(_vertices[next(vertexIndex)].Location, (_relations[next(sideIndex)] as LengthRelation).Length); double y = (_vertices[prev(vertexIndex)].Location + vector[prev(vertexIndex)]).Y; PointD[] p = c.GetIntersectionPointsWithHorizontalLine(y); PointD?chosenPoint = _GetTheNearestPoint(p, _vertices[vertexIndex].Location); if (!chosenPoint.HasValue) { vector[vertexIndex] = vector[prev(vertexIndex)]; return(false); } vector[vertexIndex] = new FreeVector(_vertices[vertexIndex].Location, chosenPoint.Value); return(true); } return(true); //? }
public void Shift_CoordinateShiftTheUnShift() { Shift system = new Shift(FreeVector.MakeWithInches(-1, 2, 4), 123 * Degrees, -22 * Degrees, 78 * Degrees); List <LineSegment> bounds = new List <LineSegment>(); bounds.Add(new LineSegment(Point.MakePointWithInches(0, 1, 0), Point.MakePointWithInches(0, 3, 0))); bounds.Add(new LineSegment(Point.MakePointWithInches(0, 1, 0), Point.MakePointWithInches(4, 1, 0))); bounds.Add(new LineSegment(Point.MakePointWithInches(0, 3, 0), Point.MakePointWithInches(4, 3, 0))); bounds.Add(new LineSegment(Point.MakePointWithInches(4, 1, 0), Point.MakePointWithInches(4, 3, 0))); Polygon testPolygon = new Polygon(bounds); Polygon shifted = testPolygon.Shift(system.Inverse()); Polygon shifted2 = shifted.Shift(system); testPolygon.Should().Be(shifted2); }
public void Line_Rotate_AboutZAxis() { var basePointLine1 = Point.MakePointWithInches(2, 1, 0); var otherPointLine1 = Point.MakePointWithInches(3, 3, 3); var line1 = new Line(basePointLine1, otherPointLine1); var axisLine = new Line(Point.Origin, Point.MakePointWithInches(0, 0, 1)); var rotationAngle = 199 * Degrees; var actualResult = line1.Rotate(new Rotation(axisLine, rotationAngle)); var expectedResultBasePoint = Point.MakePointWithInches(-1.5654689967414768, -1.5966548845136304, 0.0); var expectedDirection = new Direction(FreeVector.MakeWithInches(-0.29438226668500322, -2.2166053056557904, 3.0)); var expectedResult = new Line(expectedDirection, expectedResultBasePoint); actualResult.Should().Be(expectedResult); }
public void PolygonList_Shift() { List <Polygon> planes = new List <Polygon>(); List <LineSegment> polygonLines = new List <LineSegment>(); polygonLines.Add(new LineSegment(Point.MakePointWithInches(2, 3, 1))); polygonLines.Add(new LineSegment(Point.MakePointWithInches(0, 2, 5))); polygonLines.Add(new LineSegment(Point.MakePointWithInches(2, 3, 1), Point.MakePointWithInches(0, 2, 5))); Polygon polygon = new Polygon(polygonLines); List <LineSegment> polygon2Lines = new List <LineSegment>(); polygon2Lines.Add(new LineSegment(Point.MakePointWithInches(-1, -5, 7))); polygon2Lines.Add(new LineSegment(Point.MakePointWithInches(2, 3, 2))); polygon2Lines.Add(new LineSegment(Point.MakePointWithInches(2, 3, 2), Point.MakePointWithInches(-1, -5, 7))); Polygon polygon2 = new Polygon(polygon2Lines); //add them to the generic list planes.Add(polygon); planes.Add(polygon2); Shift shift = new Shift(FreeVector.MakeWithInches(2, 0, 0)); List <LineSegment> polygonExpectedLines = new List <LineSegment>(); polygonExpectedLines.Add(new LineSegment(Point.MakePointWithInches(2, 0, 0), Point.MakePointWithInches(4, 3, 1))); polygonExpectedLines.Add(new LineSegment(Point.MakePointWithInches(2, 0, 0), Point.MakePointWithInches(2, 2, 5))); polygonExpectedLines.Add(new LineSegment(Point.MakePointWithInches(4, 3, 1), Point.MakePointWithInches(2, 2, 5))); Polygon polygonExpected = new Polygon(polygonExpectedLines); List <LineSegment> polygon2ExpectedLines = new List <LineSegment>(); polygon2ExpectedLines.Add(new LineSegment(Point.MakePointWithInches(2, 0, 0), Point.MakePointWithInches(1, -5, 7))); polygon2ExpectedLines.Add(new LineSegment(Point.MakePointWithInches(2, 0, 0), Point.MakePointWithInches(4, 3, 2))); polygon2ExpectedLines.Add(new LineSegment(Point.MakePointWithInches(4, 3, 2), Point.MakePointWithInches(1, -5, 7))); Polygon polygon2Expected = new Polygon(polygon2ExpectedLines); List <Polygon> resultPlanes = planes.Shift(shift); (resultPlanes[0] == polygonExpected).Should().BeTrue(); (resultPlanes[1] == polygon2Expected).Should().BeTrue(); }
public void Vector_AngleBetween() { Vector vector1 = new Vector(FreeVector.MakeWithInches(1, 2, 3)); Vector vector2 = new Vector(FreeVector.MakeWithInches(-2, 1, 0)); Vector vector3 = new Vector(FreeVector.MakeWithInches(-2, 1, 1)); Vector vector4 = new Vector(FreeVector.MakeWithInches(1, 0)); Vector vector5 = new Vector(FreeVector.MakeWithInches(1, Math.Sqrt(3))); Angle result1 = vector1.AngleBetween(vector2); Angle result2 = vector1.AngleBetween(vector3); Angle result3 = vector4.AngleBetween(vector5); Angle expectedAngle1 = 1.57079632679 * Unit.Radians; Angle expectedAngle2 = 1.23732315 * Unit.Radians; Angle expectedAngle3 = 60 * Unit.Degrees; (result1 == expectedAngle1).Should().BeTrue(); (result2 == expectedAngle2).Should().BeTrue(); (result3 == expectedAngle3).Should().BeTrue(); }
public override double Area() { double res = 0; for (int i = 0; i < this.NumberOfVertices; ++i) { int a = i; int b = (i + 1) % this.NumberOfVertices; FreeVector v = new FreeVector(_vertexManager.GetVertex(a).Location); FreeVector u = new FreeVector(_vertexManager.GetVertex(b).Location); //res += (_vertexManager.GetVertex(b).Location.X - _vertexManager.GetVertex(a).Location.X) // * (_vertexManager.GetVertex(b).Location.Y + _vertexManager.GetVertex(a).Location.Y); res += (u.CrossProduct(v)); } return(res / 2); }
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(FreeVector.MakeWithInches(1, 1, 1)), Point.MakePointWithInches(1, -1, -1)); Angle rotationAngle = 212 * Degrees; 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 void Shift_ComposeRotationAndTranslation() { var angle = -90 * Degrees; var axis = new Line(Point.MakePointWithInches(1, 0, 0), Point.MakePointWithInches(1, 0, 1)); var rotation = new Rotation(axis, angle); var translation = FreeVector.MakeWithInches(-1, 2, 5); var shift = new Shift(rotation, translation); shift .Matrix .ShouldEqualWithinTolerance ( new double[, ] { { 0, 1, 0, 0 }, { -1, 0, 0, 3 }, { 0, 0, 1, 5 }, { 0, 0, 0, 1 }, } ); }