Ejemplo n.º 1
0
        public bool isHit(LDPointList form, LDPoint p, float hitRange)
        {
            LDLine l = this.toLine(form);

            LDPoint startPt = l.p1();
            LDPoint endPt   = l.p2();

            //端点から一定の距離内だったら当たり
            if (PointUtil.isHit(startPt, p, hitRange))
            {
                return(true);
            }
            if (PointUtil.isHit(endPt, p, hitRange))
            {
                return(true);
            }


            //点が一直線上にないか確認
            if (TriangleUtil.isTriangle(startPt, endPt, p))
            {
                //鈍角三角形なら判定外
                if (TriangleUtil.isObtuseAngle(startPt, endPt, p))
                {
                    return(false);
                }

                //三角形の面積を算出して、その底面で割れば高さ=線と点の距離
                float distance = TriangleUtil.getTriangleHeight(startPt, endPt, p);

                if (distance <= hitRange)
                {
                    return(true);
                }
                return(false);
            }
            //一直線上にあるが線分外にあるか判定
            LDVector2 ab    = new LDVector2(endPt - startPt);
            LDVector2 ap    = new LDVector2(p - startPt);
            LDVector2 bp    = new LDVector2(p - endPt);
            float     omega = 0.0001f;//NOTE:誤差の基準値 かなり適当に指定

            if (ap.length() + bp.length() > ab.length() + omega)
            {
                return(false);
            }
            return(true);
        }