0

我首先对我的英语感到抱歉,我将尝试解释我想要做什么我需要用代表光环的 wpf 绘制一个椭圆,它的“变形”代表其中有问题的区域,简而言之,一个椭圆可以是在特定点的运行时间变形

我正在尝试绘制几条形成椭圆的贝塞尔曲线,但在该椭圆中制作可以拖动形成凸面或空心区域的点并不是很困难(而且我不知道如何)。

¿我用西班牙语表达清楚了吗?¿有没有简单的方法可以做到这一点?

提前致谢

4

1 回答 1

0

我不知道您到底要做什么,但我建议制作椭圆的高分辨率版本并自己跟踪变形。这是一个示例程序,可帮助您入门。

对于这个演示,XAML 很简单:

<Canvas Name="canvas" Focusable="True" KeyDown="canvas_KeyDown" MouseDown="canvas_MouseDown" MouseMove="canvas_MouseMove" MouseUp="canvas_MouseUp"/>

和一个代码隐藏:

public partial class EllipseDemo : Window
{
    const int resolution = 1000;
    const double major = 150;
    const double minor = 100;
    const double xOrigin = 200;
    const double yOrigin = 200;
    const double radius = 10;
    const double scale = 0.1;
    const double spread = 10;
    const double magnitude = 10;

    Path path;
    Ellipse controlPoint;
    LineSegment[] segments;
    double[] deformation;
    double[] perturbation;
    int controlPointIndex;

    public EllipseDemo()
    {
        InitializeComponent();

        segments = new LineSegment[resolution];
        deformation = new double[resolution];
        perturbation = new double[resolution];
        for (int i = 0; i < resolution; i++)
        {
            var x = i >= resolution / 2 ? i - resolution : i;
            perturbation[i] = magnitude * Math.Exp(-Math.Pow(scale * x, 2) / spread);
        }
        path = new Path();
        path.Stroke = new SolidColorBrush(Colors.Black);
        path.StrokeThickness = 5;
        CalculateEllipse();
        canvas.Children.Add(path);

        controlPoint = new Ellipse();
        controlPoint.Stroke = new SolidColorBrush(Colors.Red);
        controlPoint.Fill = new SolidColorBrush(Colors.Transparent);
        controlPoint.Width = 2 * radius;
        controlPoint.Height = 2 * radius;
        MoveControlPoint(0);
        canvas.Children.Add(controlPoint);

        canvas.Focus();
    }

    void CalculateEllipse()
    {
        for (int i = 0; i < resolution; i++)
        {
            double angle = 2 * Math.PI * i / resolution;
            double x = xOrigin + Math.Cos(angle) * (major + deformation[i]);
            double y = yOrigin + Math.Sin(angle) * (minor + deformation[i]);
            segments[i] = new LineSegment(new Point(x, y), true);
        }
        var figure = new PathFigure(segments[0].Point, segments, true);
        var figures = new PathFigureCollection();
        figures.Add(figure);
        var geometry = new PathGeometry();
        geometry.Figures = figures;
        path.Data = geometry;
    }

    void MoveControlPoint(int index)
    {
        controlPointIndex = index;
        Canvas.SetLeft(controlPoint, segments[index].Point.X - radius);
        Canvas.SetTop(controlPoint, segments[index].Point.Y - radius);
    }

    bool mouseDown;

    void canvas_MouseDown(object sender, MouseButtonEventArgs e)
    {
        if (Mouse.DirectlyOver != controlPoint)
            return;
        mouseDown = true;
        controlPoint.CaptureMouse();
    }

    void canvas_MouseMove(object sender, MouseEventArgs e)
    {
        if (!mouseDown)
            return;
        int index = FindNearestIndex(e.GetPosition(canvas));
        MoveControlPoint(index);
    }

    void canvas_MouseUp(object sender, MouseButtonEventArgs e)
    {
        if (!mouseDown)
            return;
        controlPoint.ReleaseMouseCapture();
        mouseDown = false;
    }

    private void canvas_KeyDown(object sender, KeyEventArgs e)
    {
        int delta = 0;
        switch (e.Key)
        {
            case Key.Up:
                delta = 1;
                break;
            case Key.Down:
                delta = -1;
                break;
        }
        if (delta == 0)
            return;
        int index = controlPointIndex;
        for (int i = 0; i < resolution; i++)
            deformation[(i + index) % resolution] += delta * perturbation[i];
        CalculateEllipse();
        MoveControlPoint(index);
    }

    int FindNearestIndex(Point point)
    {
        var min = double.PositiveInfinity;
        var index = -1;
        for (int i = 0; i < segments.Length; i++)
        {
            var vector = point - segments[i].Point;
            var distance = vector.LengthSquared;
            if (distance < min)
            {
                index = i;
                min = distance;
            }
        }
        return index;
    }
}

这主要适用于Path由线段表示的 a 和Ellipse作为控制点的 a。鼠标可以在椭圆周围移动控制点,然后箭头键添加或删除一个固定扰动。一切都是硬编码的,但是如果您对数学没问题,那么它应该可以帮助您入门。

这是正在运行的程序:

椭圆演示

于 2011-01-09T22:32:39.703 回答