0

ポイントが元のポイントからの2つの角度の間にあるかどうかを判断しようとしています(OpenGLで描画するかどうかを判断するためですが、それは関係ありません)。これを行う最も簡単な方法は何ですか? ここに画像の説明を入力してください

4

5 に答える 5

2

角度の絶対値 CAB + BAD = 45 の場合、点は内側です。CAB + BAD > 45 の場合、ポイントは外側です。

于 2012-12-29T19:26:14.290 に答える
2

u = (ux, uy2 つのベクトル)、の 2 次元外積v = (vx, vy)は、

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

ここでは からまでphiの角度( からまで測定) です。角度が 0 ~ 180 度の場合、外積は正です。uvuv

したがって

(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 度であるため、これらの式を使用できます。)

于 2012-12-29T19:46:46.880 に答える
0

最終的にこの関数にたどり着きました (cameraYR は点 A が回転する角度、cameraX は Ax、cameraY は Ay、x は Bx、y は By):

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);
于 2012-12-30T09:15:44.800 に答える
0

ポイント座標があり、角度がない場合は、極座標を使用して [X,Y] -> [R,Theta] (半径と角度) を中心 (図の A) に対して変換し、角度 (シータ) を比較します。

このコードは、中心点を基準にして 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

      }


    }
于 2012-12-29T19:31:12.820 に答える
0

私はちょうど同様の質問に答えました。角度が 2 つの角度の間にあるかどうかを調べます (問題も解決します)。以下をチェック

2 つの角度の間の角度です

それが役に立てば幸い

于 2013-02-25T15:49:52.453 に答える