Exemple #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="shape"></param>
        /// <param name="rect"></param>
        /// <param name="selection"></param>
        /// <param name="builder"></param>
        /// <param name="treshold"></param>
        /// <param name="dx"></param>
        /// <param name="dy"></param>
        /// <returns></returns>
        private static bool HitTest(
            BaseShape shape,
            Rect2 rect,
            Vector2[] selection,
            ImmutableHashSet <BaseShape> .Builder builder,
            double treshold,
            double dx,
            double dy)
        {
            if (shape is XPoint)
            {
                if (GetPointBounds(shape as XPoint, treshold, dx, dy).IntersectsWith(rect))
                {
                    if (builder != null)
                    {
                        builder.Add(shape);
                    }
                    else
                    {
                        return(true);
                    }
                }
                return(false);
            }
            else if (shape is XLine)
            {
                var line = shape as XLine;
                if (GetPointBounds(line.Start, treshold, dx, dy).IntersectsWith(rect) ||
                    GetPointBounds(line.End, treshold, dx, dy).IntersectsWith(rect) ||
                    MathHelpers.LineIntersectsWithRect(rect, new Point2(line.Start.X, line.Start.Y), new Point2(line.End.X, line.End.Y)))
                {
                    if (builder != null)
                    {
                        builder.Add(line);
                        return(false);
                    }
                    else
                    {
                        return(true);
                    }
                }
                return(false);
            }
            else if (shape is XEllipse)
            {
                if (GetEllipseBounds(shape as XEllipse, dx, dy).IntersectsWith(rect))
                {
                    if (builder != null)
                    {
                        builder.Add(shape);
                        return(false);
                    }
                    else
                    {
                        return(true);
                    }
                }
                return(false);
            }
            else if (shape is XRectangle)
            {
                if (GetRectangleBounds(shape as XRectangle, dx, dy).IntersectsWith(rect))
                {
                    if (builder != null)
                    {
                        builder.Add(shape);
                        return(false);
                    }
                    else
                    {
                        return(true);
                    }
                }
                return(false);
            }
            else if (shape is XArc)
            {
                if (GetArcBounds(shape as XArc, dx, dy).IntersectsWith(rect))
                {
                    if (builder != null)
                    {
                        builder.Add(shape);
                        return(false);
                    }
                    else
                    {
                        return(true);
                    }
                }
                return(false);
            }
            else if (shape is XBezier)
            {
                if (ConvexHullBounds.Overlap(selection, ConvexHullBounds.GetVertices(shape as XBezier, dx, dy)))
                {
                    if (builder != null)
                    {
                        builder.Add(shape);
                        return(false);
                    }
                    else
                    {
                        return(true);
                    }
                }
                return(false);
            }
            else if (shape is XQBezier)
            {
                if (ConvexHullBounds.Overlap(selection, ConvexHullBounds.GetVertices(shape as XQBezier, dx, dy)))
                {
                    if (builder != null)
                    {
                        builder.Add(shape);
                        return(false);
                    }
                    else
                    {
                        return(true);
                    }
                }
                return(false);
            }
            else if (shape is XText)
            {
                if (GetTextBounds(shape as XText, dx, dy).IntersectsWith(rect))
                {
                    if (builder != null)
                    {
                        builder.Add(shape);
                        return(false);
                    }
                    else
                    {
                        return(true);
                    }
                }
                return(false);
            }
            else if (shape is XImage)
            {
                if (GetImageBounds(shape as XImage, dx, dy).IntersectsWith(rect))
                {
                    if (builder != null)
                    {
                        builder.Add(shape);
                        return(false);
                    }
                    else
                    {
                        return(true);
                    }
                }
                return(false);
            }
            else if (shape is XPath)
            {
                if ((shape as XPath).Geometry != null)
                {
                    var points = (shape as XPath).GetAllPoints();
                    if (ConvexHullBounds.Overlap(selection, ConvexHullBounds.GetVertices(points, dx, dy)))
                    {
                        if (builder != null)
                        {
                            builder.Add(shape);
                            return(false);
                        }
                        else
                        {
                            return(true);
                        }
                    }
                }
                return(false);
            }
            else if (shape is XGroup)
            {
                if (HitTest((shape as XGroup).Shapes.Reverse(), rect, selection, null, treshold, dx, dy) == true)
                {
                    if (builder != null)
                    {
                        builder.Add(shape);
                        return(false);
                    }
                    else
                    {
                        return(true);
                    }
                }
                return(false);
            }

            return(false);
        }
Exemple #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="shape"></param>
        /// <param name="p"></param>
        /// <param name="treshold"></param>
        /// <param name="dx"></param>
        /// <param name="dy"></param>
        /// <returns></returns>
        public static BaseShape HitTest(BaseShape shape, Vector2 p, double treshold, double dx, double dy)
        {
            if (shape is XPoint)
            {
                if (GetPointBounds(shape as XPoint, treshold, dx, dy).Contains(p))
                {
                    return(shape);
                }
                return(null);
            }
            else if (shape is XLine)
            {
                var line = shape as XLine;

                if (GetPointBounds(line.Start, treshold, dx, dy).Contains(p))
                {
                    return(line.Start);
                }

                if (GetPointBounds(line.End, treshold, dx, dy).Contains(p))
                {
                    return(line.End);
                }

                if (HitTestLine(line, p, treshold, dx, dy))
                {
                    return(line);
                }

                return(null);
            }
            else if (shape is XRectangle)
            {
                var rectangle = shape as XRectangle;

                if (GetPointBounds(rectangle.TopLeft, treshold, dx, dy).Contains(p))
                {
                    return(rectangle.TopLeft);
                }

                if (GetPointBounds(rectangle.BottomRight, treshold, dx, dy).Contains(p))
                {
                    return(rectangle.BottomRight);
                }

                if (GetRectangleBounds(rectangle, dx, dy).Contains(p))
                {
                    return(rectangle);
                }
                return(null);
            }
            else if (shape is XEllipse)
            {
                var ellipse = shape as XEllipse;

                if (GetPointBounds(ellipse.TopLeft, treshold, dx, dy).Contains(p))
                {
                    return(ellipse.TopLeft);
                }

                if (GetPointBounds(ellipse.BottomRight, treshold, dx, dy).Contains(p))
                {
                    return(ellipse.BottomRight);
                }

                if (GetEllipseBounds(ellipse, dx, dy).Contains(p))
                {
                    return(ellipse);
                }
                return(null);
            }
            else if (shape is XArc)
            {
                var arc = shape as XArc;

                if (GetPointBounds(arc.Point1, treshold, dx, dy).Contains(p))
                {
                    return(arc.Point1);
                }

                if (GetPointBounds(arc.Point2, treshold, dx, dy).Contains(p))
                {
                    return(arc.Point2);
                }

                if (GetPointBounds(arc.Point3, treshold, dx, dy).Contains(p))
                {
                    return(arc.Point3);
                }

                if (GetPointBounds(arc.Point4, treshold, dx, dy).Contains(p))
                {
                    return(arc.Point4);
                }

                if (GetArcBounds(arc, dx, dy).Contains(p))
                {
                    return(arc);
                }
                return(null);
            }
            else if (shape is XBezier)
            {
                var bezier = shape as XBezier;

                if (GetPointBounds(bezier.Point1, treshold, dx, dy).Contains(p))
                {
                    return(bezier.Point1);
                }

                if (GetPointBounds(bezier.Point2, treshold, dx, dy).Contains(p))
                {
                    return(bezier.Point2);
                }

                if (GetPointBounds(bezier.Point3, treshold, dx, dy).Contains(p))
                {
                    return(bezier.Point3);
                }

                if (GetPointBounds(bezier.Point4, treshold, dx, dy).Contains(p))
                {
                    return(bezier.Point4);
                }

                if (ConvexHullBounds.Contains(bezier, p, dx, dy))
                {
                    return(bezier);
                }
                return(null);
            }
            else if (shape is XQBezier)
            {
                var qbezier = shape as XQBezier;

                if (GetPointBounds(qbezier.Point1, treshold, dx, dy).Contains(p))
                {
                    return(qbezier.Point1);
                }

                if (GetPointBounds(qbezier.Point2, treshold, dx, dy).Contains(p))
                {
                    return(qbezier.Point2);
                }

                if (GetPointBounds(qbezier.Point3, treshold, dx, dy).Contains(p))
                {
                    return(qbezier.Point3);
                }

                if (ConvexHullBounds.Contains(qbezier, p, dx, dy))
                {
                    return(qbezier);
                }
                return(null);
            }
            else if (shape is XText)
            {
                var text = shape as XText;

                if (GetPointBounds(text.TopLeft, treshold, dx, dy).Contains(p))
                {
                    return(text.TopLeft);
                }

                if (GetPointBounds(text.BottomRight, treshold, dx, dy).Contains(p))
                {
                    return(text.BottomRight);
                }

                if (GetTextBounds(text, dx, dy).Contains(p))
                {
                    return(text);
                }
                return(null);
            }
            else if (shape is XImage)
            {
                var image = shape as XImage;

                if (GetPointBounds(image.TopLeft, treshold, dx, dy).Contains(p))
                {
                    return(image.TopLeft);
                }

                if (GetPointBounds(image.BottomRight, treshold, dx, dy).Contains(p))
                {
                    return(image.BottomRight);
                }

                if (GetImageBounds(image, dx, dy).Contains(p))
                {
                    return(image);
                }
                return(null);
            }
            else if (shape is XPath)
            {
                var path = shape as XPath;

                if (path.Geometry != null)
                {
                    var points = path.GetAllPoints();
                    foreach (var point in points)
                    {
                        if (GetPointBounds(point, treshold, dx, dy).Contains(p))
                        {
                            return(point);
                        }
                    }

                    if (ConvexHullBounds.Contains(points, p, dx, dy))
                    {
                        return(path);
                    }
                }
                return(null);
            }
            else if (shape is XGroup)
            {
                var group = shape as XGroup;

                foreach (var connector in group.Connectors.Reverse())
                {
                    if (GetPointBounds(connector, treshold, dx, dy).Contains(p))
                    {
                        return(connector);
                    }
                }

                var result = HitTest(group.Shapes.Reverse(), p, treshold, dx, dy);
                if (result != null)
                {
                    return(shape);
                }
                return(null);
            }

            return(null);
        }