【问题标题】:Point inside Pie Piece点在 Pie Pie 里面
【发布时间】:2011-11-25 00:10:13
【问题描述】:

我正在用 WPF 编写一个应用程序,但遇到了一个问题。如下图所示,我需要一个算法来确定指定点 P 是否在圆的阴影区域内。阴影区域只是圆的一部分,具有方向(阴影区域正在查看的位置)和角度。

【问题讨论】:

  • 最初,我想出了一个与答案类似的解决方案,但是我在理解如何解决角度时遇到了问题,因为它们从 0 到 360 然后环绕。它现在正在工作。

标签: c# .net wpf algorithm math


【解决方案1】:

数学第一:

让 v = p - c let u = (1,0) : 使用上面显示的几何图形

检查 |v|

角度 = acos(v.u/|v|)

检查角度是否在范围内。

在 WPF 中:

Vector v = p - (new Point(0,0));
if(v.Length > radius)
    return false;
double angle = -Vector.AngleBetween(v, new Vector(1,0));
...

这是一个未经测试的类

class Pie
{
    public Point Center { get; set; }
    public double Radius { get; set; }
    public Vector ZeroDegrees { get; set; }
    public bool ClockwisePositive { get; set; }

    public double GetAngle(Point p)
    {
        if (ClockwisePositive)
            return (Vector.AngleBetween(p - Center, ZeroDegrees) + 360) % 360;
        else
            return (Vector.AngleBetween(ZeroDegrees, p - Center) + 360) % 360;
    }

    public bool Contains(Point p)
    {
        return (p - Center).Length <= Radius;
    }

    public class Slice
    {
        public Pie Parent { get; set; }
        public double DirectionDegrees { get; set; }
        public double SizeDegrees { get; set; }

        public bool Contains(Point p)
        {
            if (!Parent.Contains(p))
                return false;

            double angle = Parent.GetAngle(p);
            double minAngle = (DirectionDegrees - SizeDegrees / 2 + 360) % 360;
            double maxAngle = (DirectionDegrees + SizeDegrees / 2 + 360) % 360;

            if (minAngle < maxAngle)
                return minAngle <= angle && angle <= maxAngle;
            else
                return angle >= minAngle || angle <= maxAngle;
        }
    }
}

【讨论】:

    【解决方案2】:

    假设您首先测试了 P 在圆内,并且您知道饼图的起始角度和结束角度

    一个。找到线段 CP (CPa) 的方向 - 这应该是相当基本的触发

    b.检查 CPa 是否在饼图的起始角和结束角之间

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-09
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      • 1970-01-01
      • 1970-01-01
      • 2019-03-25
      • 2022-12-27
      相关资源
      最近更新 更多