如何约束椭圆内物体的运动?

时间:2016-08-02 17:13:32

标签: c# uwp-xaml rendertransform

场景

我在网格中有几个对象。我在他们的操纵delta事件中翻译它们。对象显示在用户指针下方。它有效,看起来像这样:

ellipse.ManipulationDelta += (s, e) =>
{
    var transform = ellipse.RenderTransform as CompositeTransform;
    transform.TranslateX = e.Cumulative.Translation.X;
    transform.TranslateY = e.Cumulative.Translation.Y;
}

每个可移动物体都有一个椭圆边界,在该边界外不应拖动物体。该值易于计算。像这样:

ellipse.ManipulationDelta += (s, e) =>
{
    var boundary = new Ellipse { Height = 50, Width = 100 };

    var transform = ellipse.RenderTransform as CompositeTransform;
    transform.TranslateX = e.Cumulative.Translation.X;
    transform.TranslateY = e.Cumulative.Translation.Y;
};

要求

如果请求的点在椭圆边界之外,我想实现一些不会转换元素的逻辑。

enter image description here

东西,也许是这样的:

ellipse.ManipulationDelta += (s, e) =>
{
    var boundary = new Ellipse { Height = 50, Width = 100 };
    var point = DeterminePoint(boundary, e.Cumulative.Translation);

    var transform = ellipse.RenderTransform as CompositeTransform;
    transform.TranslateX = point.X;
    transform.TranslateY = point.Y;
};

Point DeterminePoint(Ellipse boundary, Point target) { /* TODO */ }

我可以轻松编写代码来将逻辑约束为矩形。但是矩形不是必需的,椭圆是。上面代码中的TODO部分就是这个问题的核心。

提前谢谢你。

0 个答案:

没有答案