Пример #1
0
        public string Text2Path(String strText, string strCulture, bool LtoR, string strTypeFace, int nSize, Thickness masks)
        {
            // Set up the Culture
            if (strCulture == "")
                strCulture = "en-us";
            System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo(strCulture);

            // Set up the flow direction
            System.Windows.FlowDirection fd;
            if (LtoR)
                fd = FlowDirection.LeftToRight;
            else
                fd = FlowDirection.RightToLeft;

            // Set up the font family from the parameter
            FontFamily ff = new FontFamily(strTypeFace);

            // Create the new typeface
            System.Windows.Media.Typeface tf = new System.Windows.Media.Typeface(ff,

                FontStyles.Normal, FontWeights.Normal, FontStretches.Normal);

            // Create a formatted text object from the text,

            // culture, flowdirection, typeface, size and black

            FormattedText t = new FormattedText(strText, ci, fd, tf, nSize,

                System.Windows.Media.Brushes.Black);

            // Build a Geometry out of this
            Geometry g = t.BuildGeometry(new Point(0, 0));

            // Get the Path info from the geometry
            PathGeometry p = g.GetFlattenedPathGeometry();
            var x = nSize - masks.Left - masks.Right;
            var y = nSize - masks.Top - masks.Bottom;
            var size = new Size(x < 0 ? 0 : x, y < 0 ? 0 : y);
            var rectv = new Rect(new Point(masks.Left, masks.Top), size);

            RectangleGeometry rg = new RectangleGeometry(rectv);
            var cg = new CombinedGeometry(p, rg);
            cg.GeometryCombineMode = GeometryCombineMode.Intersect;

            p = cg.GetFlattenedPathGeometry();

            // Return the path info
            return p.ToString();
        }
Пример #2
0
        /// <summary>
        /// test collision between two Geometry object
        /// </summary>
        /// <param name="firstGeometry">first Geometry</param>
        /// <param name="secondGeometry">second Geometry</param>
        /// <returns>intersection Points</returns>
        private Point[] collisionDetect(Geometry firstGeometry, Geometry secondGeometry)
        {
            Geometry firstPathGeometry = firstGeometry.GetWidenedPathGeometry(new Pen(Brushes.Black, 1.0)); //retrieve PathGeometry class, inherit from Geometry Class
            Geometry secondPathGeometry = secondGeometry.GetWidenedPathGeometry(new Pen(Brushes.Black, 1.0));

            CombinedGeometry combGeometry = new CombinedGeometry(GeometryCombineMode.Intersect, firstPathGeometry, secondPathGeometry); //inherit from Geometry
            PathGeometry combPathGeometry = combGeometry.GetFlattenedPathGeometry();

            /*code below retrieve all intersection points of 2 geometry*/
            //combPathGeometry.Figures rappresenta una serie di piccoli segmenti geometrici che compongono la geometria
            Point[] intersectionPoints = new Point[combPathGeometry.Figures.Count];
            //ho inizializzato un'array di punti, però bisogna ancora trovare le coordinate di ognuno di essi
            for (int i = 0; i < combPathGeometry.Figures.Count; i++)
            {
                //crea una piccola geometria a partire dal segmento trovato e ritorna il suo bounding box rettangolare
                Rect fig = new PathGeometry(new PathFigure[] { combPathGeometry.Figures[i] }).Bounds;
                //trova il punto centrale a partire da questo rettangolo
                intersectionPoints[i] = new Point(fig.Left + fig.Width / 2.0, fig.Top + fig.Height / 2.0);
            }
            return intersectionPoints;
        }