Example #1
0
        public static Pnt2d FindEllipseCenterFromEndpoints(Pnt2d startPnt, Pnt2d endPnt, double rX, double rY, double angle, bool sense)
        {
            // https://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
            // F.6.5.1
            var matRotation = new Mat2d();

            matRotation.SetRotation(-angle);
            var v1 = new XY(startPnt.X - endPnt.X, startPnt.Y - endPnt.Y)
                     .Multiplied(0.5)
                     .Multiplied(matRotation);

            // F.6.5.2
            var denom = rX.Sqr() * v1.Y.Sqr() + rY.Sqr() * v1.X.Sqr();
            var numer = rX.Sqr() * rY.Sqr() - denom;

            var root = Math.Sqrt(Math.Abs(numer / denom)) * (sense ? -1 : 1);
            var c1   = new XY(rX * v1.Y / rY, -(rY * v1.X / rX)).Multiplied(root);

            // F.6.5.3
            matRotation.SetRotation(angle);
            var c = new XY(startPnt.X + endPnt.X, startPnt.Y + endPnt.Y)
                    .Multiplied(0.5)
                    .Added(c1.Multiplied(matRotation));

            return(c.ToPnt());
        }
Example #2
0
        public void Rotate()
        {
            Mat2d m1 = new Mat2d();

            m1.SetRotation(Math.PI / 2);
            var p2 = new XY(4, 5);

            Assert.That(new XY(-5, 4).IsEqual(p2.Multiplied(m1), 0.0000001));
            Assert.That(new XY(-5, 4).IsEqual(p2 * m1, 0.0000001));
            p2.Multiply(m1);
            Assert.That(new XY(-5, 4).IsEqual(p2, 0.0000001));
        }
        public static Pnt2d FindEllipseCenterFromEndpoints(Pnt2d startPnt, Pnt2d endPnt, double rX, double rY, double angle, bool sense)
        {
            // https://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
            // F.6.5.1
            var matRotation = new Mat2d();

            matRotation.SetRotation(-angle);
            var v1 = new XY(startPnt.X - endPnt.X, startPnt.Y - endPnt.Y)
                     .Multiplied(0.5)
                     .Multiplied(matRotation);

            // F.6.5.2
            var denom = rX.Sqr() * v1.Y.Sqr() + rY.Sqr() * v1.X.Sqr();
            var numer = rX.Sqr() * rY.Sqr() - denom;

            // This correction is used in SvgNet, no idea what it is doing.
            //double root = 0;
            //if (numer < 0)
            //{
            //    var s = (float)Math.Sqrt(1.0 - numer / (rX.Sqr() * rY.Sqr()));
            //    rX *= s;
            //    rY *= s;
            //    root = 0.0;
            //}
            //else
            //{
            //    root = Math.Sqrt(numer / denom) * (sense ? -1 : 1);
            //}

            var root = Math.Sqrt(Math.Abs(numer / denom)) * (sense ? -1 : 1);
            var c1   = new XY(rX * v1.Y / rY, -(rY * v1.X / rX)).Multiplied(root);

            // F.6.5.3
            matRotation.SetRotation(angle);
            var c = new XY(startPnt.X + endPnt.X, startPnt.Y + endPnt.Y)
                    .Multiplied(0.5)
                    .Added(c1.Multiplied(matRotation));

            return(c.ToPnt());
        }
        public void ValueType_XY()
        {
            var p1 = new XY(1, 2);
            var p2 = new XY(4, 5);

            Assert.AreEqual(5, p1.SquareModulus());
            Assert.AreEqual(Math.Sqrt(5), p1.Modulus());

            Assert.IsTrue(p1.IsEqual(p2, 3));
            Assert.IsFalse(p1.IsEqual(p2, 2.99));

            p2 = p1;
            p2.Add(new XY(1, 2));
            Assert.AreEqual(new XY(2, 4), p2);
            Assert.AreEqual(new XY(2, 4), p1.Added(new XY(1, 2)));
            p2 += new XY(1, 2);
            Assert.AreEqual(new XY(3, 6), p2);

            Assert.AreEqual(-4, p1.Crossed(new XY(3, 2)));

            Assert.AreEqual(Math.Sqrt(16), p1.CrossMagnitude(new XY(3, 2)));
            Assert.AreEqual(16, p1.CrossSquareMagnitude(new XY(3, 2)));

            p2 = new XY(1, 2);
            p2.Divide(2);
            Assert.AreEqual(new XY(0.5, 1), p2);
            Assert.AreEqual(new XY(0.5, 1), p1.Divided(2));

            Assert.AreEqual(5, p1.Dot(new XY(1, 2)));

            p2 = new XY(1, 2);
            p2.Multiply(2);
            Assert.AreEqual(new XY(2, 4), p2);
            Assert.AreEqual(new XY(2, 4), p1.Multiplied(2));
            Assert.AreEqual(new XY(2, 4), p1 * 2);

            p2 = new XY(1, 2);
            p2.Multiply(new XY(1, 2));
            Assert.AreEqual(new XY(1, 4), p2);
            Assert.AreEqual(new XY(1, 4), p1.Multiplied(new XY(1, 2)));
            Assert.AreEqual(new XY(1, 4), p1 * new XY(1, 2));

            Mat2d m1 = new Mat2d();

            m1.SetRotation(Math.PI / 2);
            p2 = new XY(4, 5);
            Assert.AreEqual("-5,4", p2.Multiplied(m1).ToString());
            Assert.AreEqual("-5,4", (p2 * m1).ToString());
            p2.Multiply(m1);
            Assert.AreEqual("-5,4", p2.ToString());

            p2 = new XY(1, 23);
            Assert.AreEqual("0.0434372242763069,0.99905615835506", p2.Normalized().ToString());
            p2.Normalize();
            Assert.AreEqual("0.0434372242763069,0.99905615835506", p2.ToString());

            p2 = new XY(1, 2);
            p2.Reverse();
            Assert.AreEqual(new XY(-1, -2), p2);
            Assert.AreEqual(new XY(-1, -2), p1.Reversed());

            p2 = new XY(1, 2);
            p2.Subtract(new XY(3, 2));
            Assert.AreEqual(new XY(-2, 0), p2);
            Assert.AreEqual(new XY(-2, 0), p1.Subtracted(new XY(3, 2)));
            Assert.AreEqual(new XY(-2, 0), p1 - new XY(3, 2));

            p2.SetLinearForm(new XY(1, 2), new XY(4, 5));
            Assert.AreEqual(new XY(5, 7), p2);
            p2.SetLinearForm(2, new XY(1, 2), new XY(4, 5));
            Assert.AreEqual(new XY(6, 9), p2);
            p2.SetLinearForm(2, new XY(1, 2), 3, new XY(4, 5));
            Assert.AreEqual(new XY(14, 19), p2);
            p2.SetLinearForm(2, new XY(1, 2), 3, new XY(4, 5), new XY(7, 8));
            Assert.AreEqual(new XY(21, 27), p2);
            //TestContext.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0},{1}", gp2.x, gp2.y));
            //TestContext.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0},{1}", new gp_XY(1, 23).Normalized().X(), new gp_XY(1, 23).Normalized().Y()));
        }