コード例 #1
0
    protected void OnShapeCircleMouse(Event guiEvent)
    {
        Vector2 shapePosition = MouseToShapePoint(mousePosition);

        if (guiEvent.type == EventType.MouseDown)
        {
            CircleShape newCircle = CircleShape.Create(shapePosition, 0f);
            newCircle.penToMeshScale = mouseToShapeScale;
            newCircle.colorOutline   = shapeOutlineColor;
            newCircle.colorFill      = shapeFillColor;
            SetFocusedShape(newCircle);

            Repaint();
        }
        else if (guiEvent.type == EventType.MouseDrag)
        {
            CircleShape activeCircle = focusedShape as CircleShape;
            if (activeCircle != null)
            {
                Vector2 shapeDownPosition = MouseToShapePoint(mouseDownPosition);
                activeCircle.Radius = Vector2.Distance(shapeDownPosition, shapePosition);

                Repaint();
            }
        }
    }
コード例 #2
0
    private static VectorShape TryParseShapeToCircle(Shape shape, Matrix2D transform)
    {
        if (shape.Contours.Length > 1)
        {
            return(null);
        }

        BezierContour contour = shape.Contours[0];

        if (contour.Segments.Length < 5)
        {
            return(null);
        }
        if (!contour.Closed)
        {
            return(null);
        }

        BezierSegment[] segments = new BezierSegment[contour.Segments.Length - 1];
        for (int i = 0; i < segments.Length; i++)
        {
            segments[i].P0 = transform.MultiplyPoint(contour.Segments[i].P0);
            segments[i].P1 = transform.MultiplyPoint(contour.Segments[i].P1);
            segments[i].P2 = transform.MultiplyPoint(contour.Segments[i].P2);
            segments[i].P3 = transform.MultiplyPoint(contour.Segments[(i + 1)].P0);
        }

        Rect    shapeBounds = VectorUtils.Bounds(VectorUtils.BezierSegmentsToPath(segments));
        Vector2 center      = shapeBounds.center;
        float   radius      = (shapeBounds.width + shapeBounds.height) / 4f;
        float   error       = radius / 200f;

        for (int i = 0; i < segments.Length; i++)
        {
            if (Mathf.Abs(Vector2.Distance(center, segments[i].P0) - radius) > error)
            {
                return(null);
            }

            Vector2 midpoint = VectorUtils.Eval(segments[i], 0.5f);
            if (Mathf.Abs(Vector2.Distance(center, midpoint) - radius) > error)
            {
                return(null);
            }
        }

        CircleShape circle = CircleShape.Create(center, radius);

        circle.colorOutline = Color.red;
        return(circle);
    }