Ejemplo n.º 1
0
        public override bool Snap(Vector2 input, out Vector2 output)
        {
            float angle = GeomUtil.TwoPointAngle(point, input);
            //最も近い放射角度を求める
            float nearAngle = 0f;
            float halfDelta = deltaAngle * 0.5f;

            for (int i = 0; i < angles.Length; ++i)
            {
                if ((angles[i] - halfDelta) < angle && angle < (angles[i] + halfDelta))
                {
                    nearAngle = angles[i];
                    break;
                }
            }
            //線上の射影からスナップ座標を求める
            Vector2 p, q;

            p = GeomUtil.DegToVector2(nearAngle);
            q = input - point;
            float dot        = GeomUtil.Dot(p, q);
            float projection = dot / p.magnitude;               //射影距離

            output = p.normalized * projection + point;

            //スナップ座標との距離を測る
            if ((output - input).magnitude <= snapForce)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Ejemplo n.º 2
0
        public override bool Snap(Vector2 input, out Vector2 output)
        {
            //スナップ座標を求める
            Vector2 p, q;
            Vector2 snapPoint;

            //a側
            p = b - a;
            q = input - a;
            float aDot = GeomUtil.Dot(p, q);

            if (aDot <= 0)
            {
                snapPoint = a;
            }
            else
            {
                //b側
                p = a - b;
                q = input - b;
                float bDot = GeomUtil.Dot(p, q);
                if (bDot <= 0)
                {
                    snapPoint = b;
                }
                else
                {
                    //垂線の直交座標
                    float projection = bDot / p.magnitude;                      //射影距離
                    snapPoint = p.normalized * projection + b;
                }
            }

            output = snapPoint;
            //スナップ座標との距離を測る
            if ((snapPoint - input).magnitude <= snapForce)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }