• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            戰(zhàn)魂小筑

            討論群:309800774 知乎關(guān)注:http://zhihu.com/people/sunicdavy 開源項(xiàng)目:https://github.com/davyxu

               :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              257 隨筆 :: 0 文章 :: 506 評(píng)論 :: 0 Trackbacks

            前幾天需要做一個(gè)鼠標(biāo)點(diǎn)擊判定,具體是判斷一個(gè)點(diǎn)是否在某個(gè)凸四邊形中。

            最簡(jiǎn)單的方法莫過(guò)于判斷鼠標(biāo)點(diǎn)是否在2個(gè)三角形中。但是很多判定方法都是有問(wèn)題的,比如說(shuō)

             

            copy自IndieLib

            bool Triangle2D::Inside2( const Vector2& p )
            {
                Vector2 v0 = mP3 - mP1;
                Vector2 v1 = mP2 - mP1;
                Vector2 v2 = p - mP1; 
            
                // Compute dot products
                float dot00 =  Vector2::DotProduct( v0, v0 );
                float dot01 =  Vector2::DotProduct( v0, v1 );
                float dot02 =  Vector2::DotProduct( v0, v2 );
                float dot11 =  Vector2::DotProduct( v1, v1 );
                float dot12 =  Vector2::DotProduct( v1, v2 ); 
            
                // Compute barycentric coordinates
                float invDenom = 1 / (dot00 * dot11 - dot01 * dot01);
                float u = (dot11 * dot02 - dot01 * dot12) * invDenom;
                float v = (dot00 * dot12 - dot01 * dot02) * invDenom; 
            
                // Check if point is in triangle
                return (u > 0) && (v > 0) && (u + v < 1);
            } 
            
              
            

            Google出的某人代碼

            float Triangle2D::CrossProduct3(const Vector2& p1,const Vector2& p2, const Vector2& p0 )
            {
                return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
            } 
            
            bool Triangle2D::Inside( const Vector2& p )
            {
                return (CrossProduct3(mP1,p,mP2)*CrossProduct3(mP3,p,mP2)<0) &&
                       (CrossProduct3(mP2,p,mP1)*CrossProduct3(mP3,p,mP1)<0) &&
                       (CrossProduct3(mP1,p,mP3)*CrossProduct3(mP2,p,mP3)<0);
            } 
            

             

            這2個(gè)方法都有缺陷,當(dāng)點(diǎn)在三角形邊上時(shí),就無(wú)法得出。當(dāng)用在一個(gè)正方形判斷時(shí),正方形中心點(diǎn)就判定為沒(méi)有在其內(nèi)部,顯然是一個(gè)錯(cuò)誤。

             

            之后,又Google出某幾個(gè)大俠的算法和思想,考慮了下,判定點(diǎn)與四邊形重心點(diǎn)的線段是否與四邊形4條邊相交,相交時(shí),其在四邊形外部,反之亦然。

            bool Quadrangle::Inside2( const Vector2& p )
            {
                Vector2 c = Segement2D::GetCrossPoint( mP1, mP3, mP2, mP4 ); 
            
                return !(Segement2D::Intersect( mP1, mP2, c, p) || 
                       Segement2D::Intersect( mP2, mP3, c, p) ||
                       Segement2D::Intersect( mP3, mP4, c, p) ||
                       Segement2D::Intersect( mP4, mP1, c, p) );
            } 
            
            bool Segement2D::Intersect( const Vector2& p1, const Vector2& p2,const Vector2& p3, const Vector2& p4 )
            {
                float gradab, gradcd, ycptab, ycptcd, interceptX, intercepty; 
            
                // In order to avoid divisions by zero
                //if (mP1.y == mP2.y)
                //    mP2.y += 0.0001f; 
            
                //if (mP1.x == mP2.x)
                //    mP2.x += 0.0001f; 
            
                //if (seg.mP1.y == seg.mP2.y)
                //    seg.mP2.y += 0.0001f; 
            
                //if (seg.mP1.x == seg.mP2.x)
                //    seg.mP2.x += 0.0001f; 
            
                // Calculates the intersection between the two lines
                gradab = (p1.y - p2.y) / (p1.x - p2.x);
                gradcd = (p3.y - p4.y) / (p3.x - p4.x); 
            
                ycptab = p1.y - p1.x * gradab;
                ycptcd = p3.y - p3.x * gradcd;
                interceptX = (ycptab - ycptcd) / (gradcd - gradab);
                intercepty = (ycptab - (gradab * ycptcd) / gradcd) / (1 - gradab / gradcd); 
            
                // Checking in the intersection is inside the segment
                if (!((interceptX >= p1.x && interceptX <= p2.x) || (interceptX >= p2.x && interceptX <= p1.x)))
                    return 0; 
            
                if (!((intercepty >= p1.y && intercepty <= p2.y) || (intercepty >= p2.y && intercepty <= p1.y)))
                    return 0; 
            
                if (!((interceptX >= p3.x && interceptX <= p4.x) || (interceptX >= p4.x && interceptX <= p3.x)))
                    return 0; 
            
                if (!((intercepty >= p3.y && intercepty <= p4.y) || (intercepty >= p4.y && intercepty <= p3.y)))
                    return 0; 
            
                return 1;
            } 
            
            Vector2 Segement2D::GetCrossPoint(const Vector2& p1, const Vector2& p2, const Vector2& q1, const Vector2& q2)
            {
                //必須相交求出的才是線段的交點(diǎn),但是下面的程序段是通用的 
            
                /*根據(jù)兩點(diǎn)式化為標(biāo)準(zhǔn)式,進(jìn)而求線性方程組*/
                Vector2 crossPoint;
                //求x坐標(biāo)
                float tempLeft = (q2.x - q1.x) * (p1.y - p2.y) - (p2.x - p1.x) * (q1.y - q2.y);
                float tempRight = (p1.y - q1.y) * (p2.x - p1.x) * (q2.x - q1.x) + q1.x * (q2.y - q1.y) * (p2.x - p1.x) - p1.x * (p2.y - p1.y) * (q2.x - q1.x);
                crossPoint.x = tempRight / tempLeft;
                //求y坐標(biāo)
                tempLeft = (p1.x - p2.x) * (q2.y - q1.y) - (p2.y - p1.y) * (q1.x - q2.x);
                tempRight = p2.y * (p1.x - p2.x) * (q2.y - q1.y) + (q2.x- p2.x) * (q2.y - q1.y) * (p1.y - p2.y) - q2.y * (q1.x - q2.x) * (p2.y - p1.y);
                crossPoint.y = tempRight / tempLeft; 
            
                return crossPoint;
            }
            

            這個(gè)算法效率并不是很高,但對(duì)于設(shè)計(jì)器來(lái)說(shuō)無(wú)所謂了,如果有好的準(zhǔn)確算法,可以討論


            評(píng)論

            # re: 判斷點(diǎn)在凸四邊形中 2010-01-08 10:33 forestkeeper
            這題coding沒(méi)那么麻煩吧。。。直接用向量分析很容易啊。  回復(fù)  更多評(píng)論
              

            # re: 判斷點(diǎn)在凸四邊形中 2010-01-08 11:12 陳梓瀚(vczh)
            過(guò)判斷的點(diǎn)來(lái)一橫線,求出所有焦點(diǎn)(最多兩個(gè)),然后看看左邊如果有一個(gè)交點(diǎn)那就是在里面了,當(dāng)你求交到頂點(diǎn)的時(shí)候,如果頂點(diǎn)的兩條邊在你的橫線的同一側(cè),算兩個(gè)交點(diǎn)。

            何必呢  回復(fù)  更多評(píng)論
              

            # re: 判斷點(diǎn)在凸四邊形中 2010-01-08 20:57 陳昱(CY)
            凸n邊型知道各頂點(diǎn)圍繞順序的話,向量叉乘最容易  回復(fù)  更多評(píng)論
              

            # re: 判斷點(diǎn)在凸四邊形中[未登錄](méi) 2010-01-12 17:39 feng
            凸四邊形構(gòu)造出來(lái)四條直線,設(shè)解析式是
            f_1(x,y), f_2(x,y), f_3(x,y), f_4(x,y)
            將點(diǎn)坐標(biāo)帶進(jìn)去,計(jì)算一下這些解析式的數(shù)值即可判斷  回復(fù)  更多評(píng)論
              

            # re: 判斷點(diǎn)在凸四邊形中 2010-03-25 17:28 hoodlum1980
            (1)是通用的射線法,即判斷改點(diǎn)出發(fā)的射線和多邊形相交次數(shù),那么無(wú)所謂幾邊形也無(wú)所謂凸否。
            (2)是構(gòu)建這樣一個(gè)多邊形區(qū)域,然后用PtInRgn判斷好了。  回復(fù)  更多評(píng)論
              

            久久精品国产清自在天天线| 麻豆久久| 久久国产精品-国产精品| 国产综合精品久久亚洲| 久久综合精品国产一区二区三区| 欧美一级久久久久久久大| 区亚洲欧美一级久久精品亚洲精品成人网久久久久 | 久久香蕉国产线看观看99| 久久AⅤ人妻少妇嫩草影院| 无遮挡粉嫩小泬久久久久久久| 国内精品人妻无码久久久影院| 久久婷婷五月综合色99啪ak| 浪潮AV色综合久久天堂| 精品久久久久久国产三级| 久久精品国产亚洲av日韩| 伊人久久一区二区三区无码| 久久精品国产91久久综合麻豆自制| 久久久久香蕉视频| 国产成人久久777777| 国产亚洲婷婷香蕉久久精品| 久久夜色精品国产亚洲| 久久毛片免费看一区二区三区| 久久精品人人做人人爽电影蜜月| 亚洲精品无码久久久| 亚洲中文字幕伊人久久无码 | 成人国内精品久久久久一区| 伊人久久大香线蕉无码麻豆| 国产精品VIDEOSSEX久久发布| 国产精品久久久久久吹潮| 精品久久久无码人妻中文字幕| 亚洲七七久久精品中文国产| 久久精品国产99久久香蕉| 久久精品女人天堂AV麻| 狠狠人妻久久久久久综合蜜桃 | 超级97碰碰碰碰久久久久最新| 久久99精品久久久久久齐齐| 精品视频久久久久| 久久av免费天堂小草播放| 久久久久人妻一区精品| 中文成人无码精品久久久不卡| 日韩人妻无码一区二区三区久久99 |