Exemplo n.º 1
0
        public void Reverse()
        {
            var p2 = new Dir2d(1, 0);

            Assert.AreEqual(new Dir2d(-1, 0), p2.Reversed());
            p2.Reverse();
            Assert.AreEqual(new Dir2d(-1, 0), p2);
        }
        public void ValueType_Dir2d()
        {
            Assert.AreEqual(new Dir2d(1, 0), Dir2d.DX);
            Assert.AreEqual(new Dir2d(0, 1), Dir2d.DY);

            var p1 = new Dir2d(1, 0);
            var p2 = new Dir2d(0, 1);

            Assert.IsFalse(p1.IsEqual(p2, 0.1));
            Assert.IsTrue(p1.IsEqual(p2, Math.PI / 2));
            Assert.IsTrue(p1.IsNormal(p2, 0.1));
            Assert.IsFalse(p1.IsOpposite(p2, 0.1));
            Assert.IsTrue(p1.IsOpposite(p2, Math.PI / 2));
            Assert.IsFalse(p1.IsParallel(p2, 0.1));
            Assert.IsTrue(p1.IsParallel(p2, Math.PI / 2));

            Assert.AreEqual(Math.PI / 2, p1.Angle(p2));

            p2 = new Dir2d(1, 0);
            Assert.AreEqual(1, p1.Crossed(new Dir2d(0, 1)));

            Assert.AreEqual(Math.Sqrt(2) / 2, p1.Dot(new Dir2d(1, 1)), 0.0000001);

            p2 = new Dir2d(1, 0);
            p2.Reverse();
            Assert.AreEqual(new Dir2d(-1, 0), p2);
            Assert.AreEqual(new Dir2d(-1, 0), p1.Reversed());

            p2 = new Dir2d(1, 0);
            p2.Mirror(new Dir2d(0, 1));
            Assert.AreEqual(new Dir2d(-1, 0), p2);
            Assert.AreEqual(new Dir2d(-1, 0), p1.Mirrored(new Dir2d(0, 1)));

            var m2 = new Ax2d(new Pnt2d(-1, 2), new Dir2d(-1, 0));

            p2 = new Dir2d(1, 0);
            p2.Mirror(m2);
            Assert.AreEqual(new Dir2d(1, 0), p2);
            Assert.AreEqual(new Dir2d(1, 0), p1.Mirrored(m2));

            p2 = new Dir2d(1, 0);
            Assert.IsTrue(new Dir2d(0, 1).IsEqual(p2.Rotated(Math.PI / 2), 0.00001));
            p2.Rotate(Math.PI / 2);
            Assert.IsTrue(new Dir2d(0, 1).IsEqual(p2, 0.0001));

            //TestContext.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0},{1},{2}", gp2.x, gp2.y, gp2.z));

            Trsf2d t1 = new Trsf2d();

            t1.SetRotation(new Pnt2d(1, 2), Math.PI / 2);
            p2 = new Dir2d(0, 1);
            Assert.IsTrue(new Dir2d(-1, 0).IsEqual(p2.Transformed(t1), 0.0001));
            p2.Transform(t1);
            Assert.IsTrue(new Dir2d(-1, 0).IsEqual(p2, 0.0001));
        }
Exemplo n.º 3
0
        //--------------------------------------------------------------------------------------------------

        public bool ComputeParameters(out Parameters parameters)
        {
            if (FirstPoint.IsEqual(SecondPoint, 0.01))
            {
                parameters = new();
                return(false);
            }

            double scale = 1.0;

            // Dimension line direction
            var dimDir   = new Vec2d(_FirstPoint, _SecondPoint).ToDir();
            var dimMid   = _FirstPoint.Lerped(_SecondPoint, 0.5);
            var dimWidth = _FirstPoint.Distance(_SecondPoint);

            scale = Math.Min(scale, dimWidth / (DrawingRenderHelper.GetArrowSize().Length * 3));
            if (_AutoText)
            {
                Text = dimWidth.ToInvariantString("F1");
            }

            var  rotation       = dimDir.Angle(Dir2d.DX);
            bool dimRightToLeft = false;

            if (rotation < -Maths.HalfPI)
            {
                rotation      += Maths.PI;
                dimRightToLeft = true;
            }
            else if (rotation > Maths.HalfPI)
            {
                rotation      -= Maths.PI;
                dimRightToLeft = true;
            }

            // Extension line
            var extDir = new Dir2d(dimDir.Y, -dimDir.X);

            if (dimDir.Angle(new Vec2d(dimMid, Position).ToDir()) > 0)
            {
                extDir.Reverse();
            }

            var dimToPos  = new gp_Lin2d(_FirstPoint, dimDir).Distance(Position);
            var extVector = extDir.ToVec(dimToPos + _ExtensionOverlength);
            var dimOffset = extDir.ToVec(dimToPos);

            // Text
            var textOrigin = _FirstPoint.Lerped(_SecondPoint, 0.5)
                             .Translated(dimOffset);
            var textWidth  = 1.0;
            var textHeight = 1.0;
            var textScale  = scale;

            if (!Text.IsNullOrWhiteSpace())
            {
                var fontStyle = DrawingRenderHelper.GetDefaultFontStyle();
                var textSize  = DrawingRenderHelper.MeasureText(Text, fontStyle);
                textScale = Math.Min(1.0, dimWidth * 0.90 / textSize.X);
                scale     = Math.Min(scale, textScale);

                // Exact positioning on above dim line
                var textOffset = dimDir.Angle(extDir) > 0 ? 1.0 : -1.0;
                if (dimRightToLeft)
                {
                    textOffset *= -1;
                }
                textOrigin.Translate(extDir.ToVec(textOffset));

                // Center
                textOrigin.Translate(dimDir.ToVec((dimRightToLeft ? 1.0 : -1.0) * textSize.X / 2 * textScale));

                textWidth  = textSize.X * textScale;
                textHeight = textSize.Y * textScale;
            }

            parameters = new Parameters()
            {
                ExtensionVector    = extVector,
                DimensionDirection = dimDir,
                DimensionOffset    = dimOffset,
                DimensionRotation  = rotation,
                TextOrigin         = textOrigin,
                TextWidth          = textWidth,
                TextHeight         = textHeight,
                Scale     = scale,
                TextScale = textScale
            };

            return(true);
        }