Exemple #1
0
        public static bool Intersects(Ray2LR a, Segment2LR b)
        {
            if (a.direction.IsZero)
            {
                return(b.Contains(a.origin));
            }
            Vector2LR v = new Vector2LR(-a.Direction.Y, a.Direction.X);

            if (Vector2LR.DotProduct(b.Start - a.Origin, v).IsPositive == Vector2LR.DotProduct(b.End - a.Origin, v).IsPositive)
            {
                return(false);
            }
            if (b.Start == b.End)
            {
                return(a.Contains(b.Start));
            }
            Vector2LR    delta         = b.GetDelta();
            LongRational longRational1 = a.Direction.X * delta.Y - a.Direction.Y * delta.X;

            if (longRational1.IsZero)
            {
                return(false);
            }
            Vector2LR    u             = b.Start - a.Origin;
            LongRational longRational2 = Vector2LR.CrossProduct(u, delta);

            if (longRational1.IsPositive)
            {
                if (longRational2.IsNegative)
                {
                    return(false);
                }
            }
            else if (longRational2.IsPositive)
            {
                return(false);
            }
            LongRational longRational3 = Vector2LR.CrossProduct(u, a.Direction);

            if (longRational1.IsPositive)
            {
                if (longRational3.IsNegative || longRational3 > longRational1)
                {
                    return(false);
                }
            }
            else if (longRational3.IsPositive || longRational3 < longRational1)
            {
                return(false);
            }
            LongRational longRational4 = longRational3 / longRational1;
            LongRational longRational5 = longRational2 / longRational1;

            return(true);
        }
Exemple #2
0
        public static bool GetIntersectionCoefficients(
            Ray2LR a,
            Segment2LR b,
            out LongRational?p,
            out LongRational?q,
            out bool areParallel)
        {
            if (a.direction.IsZero)
            {
                areParallel = false;
                p           = new LongRational?();
                bool flag;
                if (flag = b.Contains(a.origin))
                {
                    Vector2LR delta = b.GetDelta();
                    q = new LongRational?(Vector2LR.DotProduct(a.origin - b.Start, delta) / delta.GetLengthSquared());
                }
                else
                {
                    q = new LongRational?();
                }
                return(flag);
            }
            Vector2LR    v             = new Vector2LR(-a.direction.Y, a.direction.X);
            Vector2LR    u             = b.Start - a.Origin;
            LongRational longRational1 = Vector2LR.DotProduct(u, v);
            LongRational longRational2 = Vector2LR.DotProduct(b.End - a.Origin, v);

            if (longRational1.IsPositive && longRational2.IsPositive || longRational1.IsNegative && longRational2.IsNegative)
            {
                p           = new LongRational?();
                q           = new LongRational?();
                areParallel = false;
                return(false);
            }
            Vector2LR delta1 = b.GetDelta();

            if (delta1.IsZero)
            {
                areParallel = false;
                p           = new LongRational?(Vector2LR.DotProduct(u, a.direction) / a.direction.GetLengthSquared());
                q           = new LongRational?(LongRational.Zero);
                return(true);
            }
            LongRational longRational3 = a.Direction.X * delta1.Y - a.Direction.Y * delta1.X;

            if (longRational3.IsZero)
            {
                p           = new LongRational?();
                q           = new LongRational?();
                areParallel = true;
                return(true);
            }
            areParallel = false;
            LongRational longRational4 = Vector2LR.CrossProduct(u, delta1);

            if (longRational3.IsPositive)
            {
                if (longRational4.IsNegative)
                {
                    p = new LongRational?();
                    q = new LongRational?();
                    return(false);
                }
            }
            else if (longRational4.IsPositive)
            {
                p = new LongRational?();
                q = new LongRational?();
                return(false);
            }
            LongRational longRational5 = Vector2LR.CrossProduct(u, a.Direction);

            if (longRational3.IsPositive)
            {
                if (longRational5.IsNegative || longRational5 > longRational3)
                {
                    p = new LongRational?();
                    q = new LongRational?();
                    return(false);
                }
            }
            else if (longRational5.IsPositive || longRational5 < longRational3)
            {
                p = new LongRational?();
                q = new LongRational?();
                return(false);
            }
            p = new LongRational?(longRational4 / longRational3);
            q = new LongRational?(longRational5 / longRational3);
            return(true);
        }
Exemple #3
0
        public static bool GetIntersectionParameters(
            Segment2LR a,
            Segment2LR b,
            out LongRational[] pArray,
            out LongRational[] qArray)
        {
            Vector2LR    delta1        = a.GetDelta();
            Vector2LR    delta2        = b.GetDelta();
            LongRational longRational1 = delta1.X * delta2.Y - delta1.Y * delta2.X;

            if (longRational1.IsZero)
            {
                Segment2LR.Struct9 struct9_1 = new Segment2LR.Struct9(a.start, a.end, delta1);
                Segment2LR.Struct9 struct9_2 = new Segment2LR.Struct9(b.start, b.end, delta2);
                if ((struct9_1.longRational_0 > struct9_2.longRational_0 ? (!struct9_1.method_0(b.start) ? 0 : (struct9_1.method_0(b.End) ? 1 : 0)) : (struct9_2.longRational_0.IsZero || !struct9_2.method_0(a.start) ? 0 : (struct9_2.method_0(a.end) ? 1 : 0))) != 0)
                {
                    if (struct9_1.method_1(b.start))
                    {
                        if (struct9_1.method_1(b.end))
                        {
                            pArray = new LongRational[2]
                            {
                                a.GetNormalizedProjection(b.start),
                                a.GetNormalizedProjection(b.end)
                            };
                            qArray = new LongRational[2]
                            {
                                LongRational.Zero,
                                LongRational.One
                            };
                            return(true);
                        }
                        if (struct9_2.method_1(a.start))
                        {
                            pArray = new LongRational[2]
                            {
                                LongRational.Zero,
                                a.GetNormalizedProjection(b.start)
                            };
                            qArray = new LongRational[2]
                            {
                                b.GetNormalizedProjection(a.start),
                                LongRational.Zero
                            };
                            return(true);
                        }
                        if (struct9_2.method_1(a.End))
                        {
                            pArray = new LongRational[2]
                            {
                                a.GetNormalizedProjection(b.start),
                                LongRational.One
                            };
                            qArray = new LongRational[2]
                            {
                                LongRational.Zero,
                                b.GetNormalizedProjection(a.end)
                            };
                            return(true);
                        }
                    }
                    else if (struct9_1.method_1(b.end))
                    {
                        if (struct9_2.method_1(a.start))
                        {
                            if (struct9_2.method_1(a.End))
                            {
                                pArray = new LongRational[2]
                                {
                                    LongRational.Zero,
                                    LongRational.One
                                };
                                qArray = new LongRational[2]
                                {
                                    b.GetNormalizedProjection(a.start),
                                    b.GetNormalizedProjection(a.end)
                                };
                            }
                            else
                            {
                                pArray = new LongRational[2]
                                {
                                    LongRational.Zero,
                                    a.GetNormalizedProjection(b.end)
                                };
                                qArray = new LongRational[2]
                                {
                                    b.GetNormalizedProjection(a.start),
                                    LongRational.One
                                };
                            }
                            return(true);
                        }
                        if (struct9_2.method_1(a.End))
                        {
                            pArray = new LongRational[2]
                            {
                                LongRational.One,
                                a.GetNormalizedProjection(b.end)
                            };
                            qArray = new LongRational[2]
                            {
                                b.GetNormalizedProjection(a.end),
                                LongRational.One
                            };
                            return(true);
                        }
                    }
                    else if (struct9_2.method_1(a.start))
                    {
                        pArray = new LongRational[2]
                        {
                            LongRational.Zero,
                            LongRational.One
                        };
                        qArray = new LongRational[2]
                        {
                            b.GetNormalizedProjection(a.start),
                            b.GetNormalizedProjection(a.end)
                        };
                        return(true);
                    }
                }
                pArray = (LongRational[])null;
                qArray = (LongRational[])null;
                return(false);
            }
            LongRational longRational2 = (b.Start.X - a.Start.X) * delta2.Y - (b.Start.Y - a.Start.Y) * delta2.X;

            if (longRational1.IsPositive)
            {
                if (longRational2.IsNegative || longRational2 > longRational1)
                {
                    pArray = (LongRational[])null;
                    qArray = (LongRational[])null;
                    return(false);
                }
            }
            else if (longRational2.IsPositive || longRational2 < longRational1)
            {
                pArray = (LongRational[])null;
                qArray = (LongRational[])null;
                return(false);
            }
            LongRational longRational3 = (b.Start.X - a.Start.X) * delta1.Y - (b.Start.Y - a.Start.Y) * delta1.X;

            if (longRational1.IsPositive)
            {
                if (longRational3.IsNegative || longRational3 > longRational1)
                {
                    pArray = (LongRational[])null;
                    qArray = (LongRational[])null;
                    return(false);
                }
            }
            else if (longRational3.IsPositive || longRational3 < longRational1)
            {
                pArray = (LongRational[])null;
                qArray = (LongRational[])null;
                return(false);
            }
            LongRational longRational4 = longRational2 / longRational1;
            LongRational longRational5 = longRational3 / longRational1;

            pArray = new LongRational[1] {
                longRational4
            };
            qArray = new LongRational[1] {
                longRational5
            };
            return(true);
        }
Exemple #4
0
        public static bool Intersects(Segment2LR a, Segment2LR b)
        {
            Vector2LR    delta1        = a.GetDelta();
            Vector2LR    delta2        = b.GetDelta();
            LongRational longRational1 = delta1.X * delta2.Y - delta1.Y * delta2.X;

            if (longRational1.IsZero)
            {
                Segment2LR.Struct9 struct9_1 = new Segment2LR.Struct9(a.start, a.end, delta1);
                Segment2LR.Struct9 struct9_2 = new Segment2LR.Struct9(b.start, b.end, delta2);
                if ((struct9_1.longRational_0 > struct9_2.longRational_0 ? (!struct9_1.method_0(b.start) ? 0 : (struct9_1.method_0(b.End) ? 1 : 0)) : (struct9_2.longRational_0.IsZero || !struct9_2.method_0(a.start) ? 0 : (struct9_2.method_0(a.end) ? 1 : 0))) != 0)
                {
                    if (struct9_1.method_1(b.start))
                    {
                        if (struct9_1.method_1(b.end) || struct9_2.method_1(a.start) || struct9_2.method_1(a.End))
                        {
                            return(true);
                        }
                    }
                    else if (struct9_1.method_1(b.end))
                    {
                        if (struct9_2.method_1(a.start) || struct9_2.method_1(a.End))
                        {
                            return(true);
                        }
                    }
                    else if (struct9_2.method_1(a.start))
                    {
                        return(true);
                    }
                }
                return(false);
            }
            LongRational longRational2 = (b.Start.X - a.Start.X) * delta2.Y - (b.Start.Y - a.Start.Y) * delta2.X;

            if (longRational1.IsPositive)
            {
                if (longRational2.IsNegative || longRational2 > longRational1)
                {
                    return(false);
                }
            }
            else if (longRational2.IsPositive || longRational2 < longRational1)
            {
                return(false);
            }
            LongRational longRational3 = (b.Start.X - a.Start.X) * delta1.Y - (b.Start.Y - a.Start.Y) * delta1.X;

            if (longRational1.IsPositive)
            {
                if (longRational3.IsNegative || longRational3 > longRational1)
                {
                    return(false);
                }
            }
            else if (longRational3.IsPositive || longRational3 < longRational1)
            {
                return(false);
            }
            return(true);
        }