Two dimensional point.
Example #1
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="centerPoint"></param>
 /// <param name="angleInDegrees"></param>
 /// <returns></returns>
 public Point2 RotateAt(Point2 centerPoint, double angleInDegrees)
 {
     double angleInRadians = angleInDegrees * (PI / 180.0);
     double cosTheta = Cos(angleInRadians);
     double sinTheta = Sin(angleInRadians);
     return new Point2
     {
         X = (cosTheta * (this.X - centerPoint.X) - sinTheta * (this.Y - centerPoint.Y) + centerPoint.X),
         Y = (sinTheta * (this.X - centerPoint.X) + cosTheta * (this.Y - centerPoint.Y) + centerPoint.Y)
     };
 }
Example #2
0
 /// <summary>
 /// Creates a new <see cref="XPoint"/> instance.
 /// </summary>
 /// <param name="point">The source point.</param>
 /// <returns>The new instance of the <see cref="XPoint"/> class.</returns>
 public static XPoint FromPoint2(Point2 point)
 {
     return new XPoint()
     {
         Name = "",
         Style = default(ShapeStyle),
         X = point.X,
         Y = point.Y,
         Alignment = PointAlignment.None,
         Shape = null
     };
 }
        /// <summary>
        /// Parse a SVG path geometry string.
        /// </summary>
        /// <param name="context">The geometry context.</param>
        /// <param name="pathString">The path geometry string</param>
        /// <param name="startIndex">The string start index.</param>
        public void Parse(XGeometryContext context, string pathString, int startIndex)
        {
            _context = context;
            _pathString = pathString;
            _pathLength = pathString.Length;
            _curIndex = startIndex;
            _secondLastPoint = Point2.Create(0, 0);
            _lastPoint = Point2.Create(0, 0);
            _lastStart = Point2.Create(0, 0);
            _figureStarted = false;
            bool first = true;
            char last_cmd = ' ';

            while (ReadToken())
            {
                char cmd = _token;

                if (first)
                {
                    if ((cmd != 'M') && (cmd != 'm'))
                    {
                        InvalidToken();
                    }

                    first = false;
                }

                switch (cmd)
                {
                    case 'm':
                    case 'M':
                        _lastPoint = ReadPoint(cmd, !_allowComma);

                        _context.BeginFigure(XPoint.FromPoint2(_lastPoint), _isFilled, !_isClosed);
                        _figureStarted = true;
                        _lastStart = _lastPoint;
                        last_cmd = 'M';

                        while (IsNumber(_allowComma))
                        {
                            _lastPoint = ReadPoint(cmd, !_allowComma);
                            _context.LineTo(XPoint.FromPoint2(_lastPoint), _isStroked, !_isSmoothJoin);
                            last_cmd = 'L';
                        }
                        break;

                    case 'l':
                    case 'L':
                    case 'h':
                    case 'H':
                    case 'v':
                    case 'V':
                        EnsureFigure();

                        do
                        {
                            switch (cmd)
                            {
                                case 'l':
                                    _lastPoint = ReadPoint(cmd, !_allowComma);
                                    break;
                                case 'L':
                                    _lastPoint = ReadPoint(cmd, !_allowComma);
                                    break;
                                case 'h':
                                    _lastPoint.X += ReadNumber(!_allowComma);
                                    break;
                                case 'H':
                                    _lastPoint.X = ReadNumber(!_allowComma);
                                    break;
                                case 'v':
                                    _lastPoint.Y += ReadNumber(!_allowComma);
                                    break;
                                case 'V':
                                    _lastPoint.Y = ReadNumber(!_allowComma);
                                    break;
                            }

                            _context.LineTo(XPoint.FromPoint2(_lastPoint), _isStroked, !_isSmoothJoin);
                        }
                        while (IsNumber(_allowComma));

                        last_cmd = 'L';
                        break;

                    case 'c':
                    case 'C':
                    case 's':
                    case 'S':
                        EnsureFigure();

                        do
                        {
                            Point2 p;

                            if ((cmd == 's') || (cmd == 'S'))
                            {
                                if (last_cmd == 'C')
                                {
                                    p = Reflect();
                                }
                                else
                                {
                                    p = _lastPoint;
                                }

                                _secondLastPoint = ReadPoint(cmd, !_allowComma);
                            }
                            else
                            {
                                p = ReadPoint(cmd, !_allowComma);

                                _secondLastPoint = ReadPoint(cmd, _allowComma);
                            }

                            _lastPoint = ReadPoint(cmd, _allowComma);
                            _context.CubicBezierTo(
                                XPoint.FromPoint2(p),
                                XPoint.FromPoint2(_secondLastPoint),
                                XPoint.FromPoint2(_lastPoint),
                                _isStroked,
                                !_isSmoothJoin);

                            last_cmd = 'C';
                        }
                        while (IsNumber(_allowComma));

                        break;

                    case 'q':
                    case 'Q':
                    case 't':
                    case 'T':
                        EnsureFigure();

                        do
                        {
                            if ((cmd == 't') || (cmd == 'T'))
                            {
                                if (last_cmd == 'Q')
                                {
                                    _secondLastPoint = Reflect();
                                }
                                else
                                {
                                    _secondLastPoint = _lastPoint;
                                }

                                _lastPoint = ReadPoint(cmd, !_allowComma);
                            }
                            else
                            {
                                _secondLastPoint = ReadPoint(cmd, !_allowComma);
                                _lastPoint = ReadPoint(cmd, _allowComma);
                            }

                            _context.QuadraticBezierTo(
                                XPoint.FromPoint2(_secondLastPoint),
                                XPoint.FromPoint2(_lastPoint),
                                _isStroked,
                                !_isSmoothJoin);

                            last_cmd = 'Q';
                        }
                        while (IsNumber(_allowComma));

                        break;

                    case 'a':
                    case 'A':
                        EnsureFigure();

                        do
                        {
                            double w = ReadNumber(!_allowComma);
                            double h = ReadNumber(_allowComma);
                            double rotation = ReadNumber(_allowComma);
                            bool large = ReadBool();
                            bool sweep = ReadBool();

                            _lastPoint = ReadPoint(cmd, _allowComma);

                            _context.ArcTo(
                                XPoint.FromPoint2(_lastPoint),
                                XPathSize.Create(w, h),
                                rotation,
                                large,
                                sweep ? XSweepDirection.Clockwise : XSweepDirection.Counterclockwise,
                                _isStroked,
                                !_isSmoothJoin);
                        }
                        while (IsNumber(_allowComma));

                        last_cmd = 'A';
                        break;

                    case 'z':
                    case 'Z':
                        EnsureFigure();
                        _context.SetClosedState(_isClosed);

                        _figureStarted = false;
                        last_cmd = 'Z';
                        _lastPoint = _lastStart;
                        break;

                    default:
                        InvalidToken();
                        break;
                }
            }
        }
Example #4
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="point"></param>
 /// <returns></returns>
 public double Distance(Point2 point)
 {
     double dx = this.X - point.X;
     double dy = this.Y - point.Y;
     return Sqrt(dx * dx + dy * dy);
 }
Example #5
0
 /// <summary>
 /// Creates a new <see cref="Rect2"/> instance.
 /// </summary>
 /// <param name="tl"></param>
 /// <param name="br"></param>
 /// <param name="dx"></param>
 /// <param name="dy"></param>
 /// <returns></returns>
 public static Rect2 Create(Point2 tl, Point2 br, double dx = 0.0, double dy = 0.0)
 {
     return Rect2.Create(tl.X, tl.Y, br.X, br.Y, dx, dy);
 }