是从另一个角度的两个角度之间的点?

时间:2012-12-29 19:23:19

标签: math opengl-es

我试图确定一个点是否在来自原始点的两个角度之间(为了确定是否用OpenGL绘制它,尽管这是无关紧要的)。这样做最简单的方法是什么? enter image description here

5 个答案:

答案 0 :(得分:2)

如果角度CAB + BAD的绝对值= 45,则点在内部。如果CAB + BAD> 45,然后点在外面。

答案 1 :(得分:2)

2个向量u = (ux, uy),v = (vx, vy)的二维交叉积,

u x v = ux * vy - uy * vx = |u| * |v| * sin(phi)

其中phiuv之间的角度(从uv衡量)。如果角度在0到180度之间,则交叉积为正。

因此

(B - A) x (D - A) > 0

如果B位于从AD的向量“左侧”的半平面中,那么

(B - A) x (D - A) > 0 and (B - A) x (C - A) < 0

确切地说B位于该扇区中。如果您还想了解B位于扇区边界的情况,请使用>= resp。 <=

注意:只要A处扇区的角度小于180度,就可以使用,并且可以推广到更大的角度。因为你的角度是45度,可以使用这些公式。)

答案 2 :(得分:0)

如果您有点坐标且没有角度,则使用极坐标转换[X,Y] - &gt; [R,Theta](半径和角度)相对于中心(图中的A),然后比较角度(thetas)。

此代码将Point转换为相对于中心点的PolarPoint:

/// <summary>
/// Converts Point to polar coordinate point
/// </summary>
public static PolarPoint PointToPolarPoint(Point center, Point point)
{
  double dist = Distance(center, point);

  double theta = Math.Atan2(point.Y - center.Y, point.X - center.X);

  if (theta < 0)  // provide 0 - 2Pi "experience"
    theta = 2 * Math.PI + theta;

  return new PolarPoint(dist, theta);
}

/// <summary>
/// Calculates distance between two points
/// </summary>
public static int Distance(Point p1, Point p2)  
{
  return (int) Math.Sqrt
         (
           Math.Pow(p1.X - p2.X, 2) +
           Math.Pow(p1.Y - p2.Y, 2)
         );
}

C#中的Polar Point类(包括转换回Point):

    /* NFX by ITAdapter
     * Originated: 2006.01
     * Revision: NFX 0.2  2009.02.10
     */
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Text;

    namespace NFX.Geometry
    {

      /// <summary>
      /// Represents a point with polar coordinates
      /// </summary>
      public struct PolarPoint
      {

        #region .ctor

          /// <summary>
          /// Initializes polar coordinates
          /// </summary>
          public PolarPoint(double r, double theta)
          {
            m_R = r;
            m_Theta = 0;
            Theta = theta;
          }

          /// <summary>
          /// Initializes polar coordinates from 2-d cartesian coordinates
          /// </summary>
          public PolarPoint(Point center, Point point)
          {
            this = CartesianUtils.PointToPolarPoint(center, point);
          }
        #endregion

        #region Private Fields 
          private double m_R;
          private double m_Theta;

        #endregion


        #region Properties
          /// <summary>
          /// R coordinate component which is coordinate distance from point of coordinates origin
          /// </summary>
          public double R
          {
            get { return m_R; }
            set { m_R = value; }
          }


          /// <summary>
          /// Angular azimuth coordinate component. An angle must be between 0 and 2Pi.
          /// Note: Due to screen Y coordinate going from top to bottom (in usual orientation)
          ///  Theta angle may be reversed, that is - be positive in the lower half coordinate plane.
          /// Please refer to:
          ///  http://en.wikipedia.org/wiki/Polar_coordinates
          /// </summary>
          public double Theta
          {
            get { return m_Theta; }
            set
            {
              if ((value < 0) || (value > Math.PI * 2))
                throw new NFXException("Invalid polar coordinates angle");
              m_Theta = value;
            }
          }


          /// <summary>
          /// Returns polar coordinate converted to 2-d cartesian coordinates.
          /// Coordinates are relative to 0,0 of the angle base vertex
          /// </summary>
          public Point Point
          {
            get
            {
              int x = (int)(m_R * Math.Cos(m_Theta));
              int y = (int)(m_R * Math.Sin(m_Theta));
              return new Point(x, y);
            }
          }
        #endregion



        #region Operators  
          public static bool operator ==(PolarPoint left, PolarPoint right)
          {
            return (left.m_R == right.m_R) && (left.m_Theta == right.m_Theta);
          }

          public static bool operator !=(PolarPoint left, PolarPoint right)
          {
            return (left.m_R != right.m_R) || (left.m_Theta != right.m_Theta);
          }
        #endregion


        #region Object overrides
          public override bool Equals(object obj)
          {
            if (obj is PolarPoint)
             return this==((PolarPoint)obj);
            else
             return false; 
          }

          public override int GetHashCode()
          {
            return m_R.GetHashCode() + m_Theta.GetHashCode();
          }

          public override string ToString()
          {
            return string.Format("Distance: {0}; Angle: {1} rad.", m_R, m_Theta);
          }


        #endregion

      }


    }

答案 3 :(得分:0)

我最终得到了这个函数(其中cameraYR是A点旋转的角度,cameraX是A.x,cameraY是A.y,x是B.x,y是B.y):

float cameraAngle = PI + cameraYR;
float angle = PI / 2 + atan2f(cameraY - y, cameraX - x);
float anglediff = fmodf(angle - cameraAngle + PI, PI * 2) - PI;
return (anglediff <= visibleAngle && anglediff >= -visibleAngle) || (anglediff <= -PI * 2 + visibleAngle && angleDiff >= -PI * 2 - visibleAngle);

答案 4 :(得分:0)

我刚刚回答了类似的问题。查找角度是否在两个角度之间(这也解决了您的问题)。请查看以下

Is angle in between two angles

希望有所帮助