コード例 #1
0
        public void KtLine2DTest1()
        {
            var start = new KtPoint2D(1, 2);
            var end   = new KtPoint2D(3, 4);
            var line  = new KtLine2D(1, 2, 3, 4);

            Assert.AreEqual(line.Start, start);
            Assert.AreEqual(line.End, end);
        }
コード例 #2
0
        public bool HasThePoint(KtPoint2D point)
        {
            var parameter = (point - Start).ToKtVector2D() / Vector;

            if (parameter is null)
            {
                return(false);
            }
            return(ValidParametricValue(parameter));
        }
コード例 #3
0
        public KtPoint2D Centroid()
        {
            var Atotal = 0d;
            var Ad     = new KtPoint2D();

            foreach (var r in _regions)
            {
                Atotal += r.Area();
                Ad     += r.AreaCentroid();
            }
            return(Ad / Atotal);
        }
コード例 #4
0
        public void LineIntersectionTests()
        {
            var watch        = new Stopwatch();
            var intersection = new KtPoint2D(5, 7);
            var line1        = new KtLine2D((1, 1), (3, 4));
            var line2        = new KtLine2D((9, 3), (7, 5));
            var line3        = new KtLine2D((3, 1), (5, 4));
            var ray1         = new KtRay2D((9, 3), (7, 5));
            var ray2         = new KtRay2D((7, 5), (9, 3));
            var ray3         = new KtRay2D((1, 1), (3, 4));
            var ray4         = new KtRay2D((3, 4), (1, 1));
            var segment1     = new KtSegment2D((7, 5), (9, 3));
            var segment2     = new KtSegment2D((4, 8), (9, 3));
            var segment3     = new KtSegment2D((1, 1), (3, 4));
            var segment4     = new KtSegment2D((1, 1), (7, 10));

            watch.Start();
            for (int i = 0; i < 100001; i++)
            {
                // line to line with intersection
#pragma warning disable CS0618 // Type or member is obsolete
                Assert.AreEqual(line1.IntersectWith3(line2), intersection);
                //line to line parallel
                Assert.IsNull(line1.IntersectWith3(line3));
                // line to ray with intersection
                Assert.AreEqual(line1.IntersectWith3(ray1), new KtPoint2D(5, 7));
                // line to ray no Intersection
                Assert.AreEqual(line1.IntersectWith3(ray2), null);
                //line to segment no intersection
                Assert.AreEqual(line1.IntersectWith3(segment1), null);
                //line to segment with intersection
                Assert.AreEqual(line1.IntersectWith3(segment2), new KtPoint2D(5, 7));
                //ray to ray with intersection
                Assert.AreEqual(ray3.IntersectWith3(ray1), new KtPoint2D(5, 7));
                //ray to ray no intersection
                Assert.AreEqual(ray3.IntersectWith3(ray2), null);
                Assert.AreEqual(ray4.IntersectWith3(ray2), null);
                Assert.AreEqual(ray4.IntersectWith3(ray1), null);
                //ray to segment with intersection
                Assert.AreEqual(ray3.IntersectWith3(segment2), new KtPoint2D(5, 7));
                //ray to segment no intersection
                Assert.AreEqual(ray3.IntersectWith3(segment1), null);
                Assert.AreEqual(ray4.IntersectWith3(segment2), null);
                //segment to segment no intersection
                Assert.AreEqual(segment3.IntersectWith3(segment1), null);
                //segement to segment with intersection
                Assert.AreEqual(segment4.IntersectWith3(segment2), new KtPoint2D(5, 7));
#pragma warning restore CS0618 // Type or member is obsolete
            }
            watch.Stop();
            System.Console.WriteLine(((double)watch.ElapsedMilliseconds).ToString());
            //System.Windows.Forms.MessageBox.Show(watch.ElapsedMilliseconds.ToString());
        }
コード例 #5
0
        public KtPoint2D IntersectWith3(KtLine2DBase line2)
        {
            var determinant = Vector.Cross(line2.Vector);

            if (determinant.NearZero(5))
            {
                return(null);
            }
            var(a1, a2, b1, b2, c1, c2) = (A, line2.A, B, line2.B, C, line2.C);
            var p = new KtPoint2D((b2 * c1) - (b1 * c2), (c2 * a1) - (c1 * a2)) / determinant;

            return(Bounds(p) && line2.Bounds(p) ? p : null);
        }
コード例 #6
0
        internal override bool Bounds(KtPoint2D p)
        {
            if (p is null)
            {
                return(false);
            }
            if (p == Start)
            {
                return(true);
            }
            var directionOfPoint = (p - Start).ToKtVector2D().Direction;
            var directionOfLine  = Vector.Direction;

            return(directionOfPoint == directionOfLine);
        }
コード例 #7
0
        public static KtRegion Rectangle(KtPoint2D center, double width, double height, bool solid = true)
        {
            if (center == null)
            {
                center = new KtPoint2D();
            }
            var corners = new List <KtPoint2D>
            {
                center + new KtPoint2D(-width / 2.0, -height / 2.0),
                center + new KtPoint2D(-width / 2.0, height / 2.0),
                center + new KtPoint2D(width / 2.0, height / 2.0),
                center + new KtPoint2D(width / 2.0, -height / 2.0)
            };

            return(solid ? (KtRegion) new KtSolidRegion(corners) : new KtHollowRegion(corners));
        }
コード例 #8
0
 protected KtLine2DBase(KtPoint2D start, KtPoint2D end)
 {
     if (start == end)
     {
         throw new ArgumentException("Cannot create zero length line where start and end are equal");
     }
     if (start == null || end == null)
     {
         throw new ArgumentNullException($"Cannot create a 2D line with points ({start}) and  ({end}) where one is null");
     }
     Start  = start;
     End    = end;
     Vector = (end - start).ToKtVector2D();
     A      = Vector.Y;
     B      = -Vector.X;
     C      = Start.ToKtVector2D().Cross(Vector);
 }
コード例 #9
0
        public static KtRegion RegularPolygon(KtPoint2D center, int cornerCount, double circumRadius, bool solid)
        {
            if (circumRadius.NearZero(5) || cornerCount < 2)
            {
                return(null);
            }
            if (center == null)
            {
                center = new KtPoint2D();
            }
            var corners  = new List <KtPoint2D>();
            var intAngle = Cycle.Create(1) / cornerCount;

            for (var i = 0; i < cornerCount; i++)
            {
                var corner = center + (new KtVector2D(intAngle * i) * circumRadius).ToKtPoint2D();
                corners.Add(corner);
            }
            return(solid ? (KtRegion) new KtSolidRegion(corners) : new KtHollowRegion(corners));
        }
コード例 #10
0
 public KtSegment2D(KtPoint2D start, KtPoint2D end) : base(start, end)
 {
 }
コード例 #11
0
 public KtLine2D(KtPoint2D start, KtPoint2D end) : base(start, end)
 {
 }
コード例 #12
0
 internal static bool LiesOnSegments(this KtPoint2D point, IEnumerable <KtSegment2D> segments) =>
 segments.Any(segment => segment.HasThePoint(point));
コード例 #13
0
 internal abstract bool Bounds(KtPoint2D p);
コード例 #14
0
 internal static bool Inscribes(this CircularStack <KtPoint2D> points, KtPoint2D point) =>
 !points.EdgesHasThePoint(point) && !points.InteriorAngle(point).IsZero;
コード例 #15
0
 public KtPolygon2D Translate(KtPoint2D point) => new(_regions.Select(reg => reg.Translate(point)).ToArray());
コード例 #16
0
 private static bool EdgesHasThePoint(this CircularStack <KtPoint2D> points, KtPoint2D point) =>
 points.ToEdges().Any(edge => edge.HasThePoint(point));
コード例 #17
0
 public KtRay2D(KtPoint2D start, KtVector2D direction) : this(start, start + direction)
 {
 }
コード例 #18
0
 public double SubstitutePoint(KtPoint2D p) => (A * p.X) + (B * p.Y) - C;
コード例 #19
0
 public double Distance(KtPoint2D p) => SubstitutePoint(p) / Vector.Magnitude;
コード例 #20
0
 internal override bool Bounds(KtPoint2D p) => p.X.IsWithin(Start.X, End.X) && p.Y.IsWithin(Start.Y, End.Y);
コード例 #21
0
 public static Angle TotalInternalAngle(this CircularStack <KtPoint2D> points, KtPoint2D point) =>
 points.Aggregate(Degrees.Create(0), (total, c) => total + (c?.Next?.Value - point).ToKtVector2D().AngleBetween((c?.Value - point).ToKtVector2D()));
コード例 #22
0
 internal static bool Inscribes(this IEnumerable <KtPoint2D> points, KtPoint2D point) =>
 points.ToCircularStack().Inscribes(point);
コード例 #23
0
 public static Angle TotalInternalAngle(this IEnumerable <KtPoint2D> points, KtPoint2D point) =>
 points.ToCircularStack().TotalInternalAngle(point);
コード例 #24
0
 private static Angle InteriorAngle(this CircularStack <KtPoint2D> points, KtPoint2D point) =>
 points.Aggregate(Degrees.Create(0.0), (total, c) => total + c.InteriorAngle(point));
コード例 #25
0
 public bool Inline(KtPoint2D p) => p != null && SubstitutePoint(p).NearZero() && Bounds(p);
コード例 #26
0
 private static Angle InteriorAngle(this CircularStackElement <KtPoint2D> c, KtPoint2D point) =>
 (c?.Next?.Value - point).ToKtVector2D().AngleBetween((c?.Value - point).ToKtVector2D());
コード例 #27
0
 public KtRay2D(KtPoint2D start, KtPoint2D end) : base(start, end)
 {
 }