青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

Point in triangle test

LINK: http://www.blackpawn.com/texts/pointinpoly/default.html

Same Side Technique

A common way to check if a point is in a triangle is to find the vectors connecting the point to each of the triangle's three vertices and sum the angles between those vectors. If the sum of the angles is 2*pi then the point is inside the triangle, otherwise it is not. It works, but it is very slow. This text explains a faster and much easier method.

First off, forgive the nasty coloring. I'm really sorry about it. Honest.

Okay, A B C forms a triangle and all the points inside it are yellow. Lines AB, BC, and CA each split space in half and one of those halves is entirely outside the triangle. This is what we'll take advantage of.

For a point to be inside the traingle A B C it must be below AB and left of BC and right of AC. If any one of these tests fails we can return early.

But, how do we tell if a point is on the correct side of a line? I'm glad you asked.


If you take the cross product of [B-A] and [p-A], you'll get a vector pointing out of the screen. On the other hand, if you take the cross product of [B-A] and [p'-A] you'll get a vector pointing into the screen. Ah ha! In fact if you cross [B-A] with the vector from A to any point above the line AB, the resulting vector points out of the screen while using any point below AB yields a vector pointing into the screen. So all we need to do to distinguish which side of a line a point lies on is take a cross product.

The only question remaining is: how do we know what direction the cross product should point in? Because the triangle can be oriented in any way in 3d-space, there isn't some set value we can compare with. Instead what we need is a reference point - a point that we know is on a certain side of the line. For our triangle, this is just the third point C.

So, any point p where [B-A] cross [p-A] does not point in the same direction as [B-A] cross [C-A] isn't inside the triangle. If the cross products do point in the same direction, then we need to test p with the other lines as well. If the point was on the same side of AB as C and is also on the same side of BC as A and on the same side of CA as B, then it is in the triangle.

Implementing this is a breeze. We'll make a function that tells us if two points are on the same side of a line and have the actual point-in-triangle function call this for each edge.

function SameSide(p1,p2, a,b)
            cp1 = CrossProduct(b-a, p1-a)
            cp2 = CrossProduct(b-a, p2-a)
            if DotProduct(cp1, cp2) >= 0 then return true
            else return false
            function PointInTriangle(p, a,b,c)
            if SameSide(p,a, b,c) and SameSide(p,b, a,c)
            and SameSide(p,c, a,b) then return true
            else return false
            

It's simple, effective and has no square roots, arc cosines, or strange projection axis determination nastiness.



Barycentric Technique

The advantage of the method above is that it's very simple to understand so that once you read it you should be able to remember it forever and code it up at any time without having to refer back to anything. It's just - hey the point has to be on the same side of each line as the triangle point that's not in the line. Cake.

Well, there's another method that is also as easy conceptually but executes faster. The downside is there's a little more math involved, but once you see it worked out it should be no problem.

So remember that the three points of the triangle define a plane in space. Pick one of the points and we can consider all other locations on the plane as relative to that point. Let's go with A -- it'll be our origin on the plane. Now what we need are basis vectors so we can give coordinate values to all the locations on the plane. We'll pick the two edges of the triangle that touch A, (C - A) and (B - A). Now we can get to any point on the plane just by starting at A and walking some distance along (C - A) and then from there walking some more in the direction (B - A).

With that in mind we can now describe any point on the plane as

    P = A + u * (C - A) + v * (B - A)

Notice now that if u or v < 0 then we've walked in the wrong direction and must be outside the triangle. Also if u or v > 1 then we've walked too far in a direction and are outside the triangle. Finally if u + v > 1 then we've crossed the edge BC again leaving the triangle.

Given u and v we can easily calculate the point P with the above equation, but how can we go in the reverse direction and calculate u and v from a given point P? Time for some math!

    P = A + u * (C - A) + v * (B - A)       // Original equation
(P - A) = u * (C - A) + v * (B - A)     // Subtract A from both sides
v2 = u * v0 + v * v1                    // Substitute v0, v1, v2 for less writing
// We have two unknowns (u and v) so we need two equations to solve
// for them.  Dot both sides by v0 to get one and dot both sides by
// v1 to get a second.
(v2) . v0 = (u * v0 + v * v1) . v0
(v2) . v1 = (u * v0 + v * v1) . v1
// Distribute v0 and v1
v2 . v0 = u * (v0 . v0) + v * (v1 . v0)
v2 . v1 = u * (v0 . v1) + v * (v1 . v1)
// Now we have two equations and two unknowns and can solve one
// equation for one variable and substitute into the other.  Or
// if you're lazy like me, fire up Mathematica and save yourself
// some handwriting.
Solve[v2.v0 == {u(v0.v0) + v(v1.v0), v2.v1 == u(v0.v1) + v(v1.v1)}, {u, v}]
u = ((v1.v1)(v2.v0)-(v1.v0)(v2.v1)) / ((v0.v0)(v1.v1) - (v0.v1)(v1.v0))
v = ((v0.v0)(v2.v1)-(v0.v1)(v2.v0)) / ((v0.v0)(v1.v1) - (v0.v1)(v1.v0))

Here's an implementation in Flash that you can play with. :)

// Compute vectors
                v0 = C - A
                v1 = B - A
                v2 = P - A
                // Compute dot products
                dot00 = dot(v0, v0)
                dot01 = dot(v0, v1)
                dot02 = dot(v0, v2)
                dot11 = dot(v1, v1)
                dot12 = dot(v1, v2)
                // Compute barycentric coordinates
                invDenom = 1 / (dot00 * dot11 - dot01 * dot01)
                u = (dot11 * dot02 - dot01 * dot12) * invDenom
                v = (dot00 * dot12 - dot01 * dot02) * invDenom
                // Check if point is in triangle
                return (u > 0) && (v > 0) && (u + v < 1)
                

The algorithm outlined here follows one of the techniques described in Realtime Collision Detection. You can also find more information about Barycentric Coordinates at Wikipedia and MathWorld.

posted on 2009-12-01 16:58 zmj 閱讀(1421) 評論(0)  編輯 收藏 引用

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            麻豆91精品| 久久综合九色综合久99| 亚洲精品国精品久久99热一| 噜噜噜在线观看免费视频日韩| 在线成人av| 亚洲国产日韩精品| 欧美午夜影院| 久久久欧美精品| 农村妇女精品| 亚洲男女毛片无遮挡| 香蕉久久a毛片| 亚洲激情成人在线| 99pao成人国产永久免费视频| 国产伦精品一区二区三区在线观看| 久久久综合精品| 欧美日本一道本| 久久精品91久久久久久再现| 美国三级日本三级久久99| 一区二区三区欧美视频| 亚洲一区二区三区精品在线| 伊人成综合网伊人222| 亚洲人成久久| 国产一区二区久久久| 亚洲第一福利社区| 国产精品亚洲综合天堂夜夜| 女主播福利一区| 国产精品成人一区二区三区夜夜夜 | 亚洲国产欧美精品| 国产精品入口麻豆原神| 欧美国产日韩一区二区在线观看| 欧美日韩一区二区三区在线视频 | 久热精品视频在线| 欧美视频在线免费| 亚洲丁香婷深爱综合| 国产精品一区二区三区四区| 亚洲国产另类精品专区| 国内精品久久久久影院薰衣草| 亚洲免费av片| 亚洲区一区二| 久久久久久久97| 欧美在线高清| 国产精品国产三级国产aⅴ9色| 免费观看成人网| 国产一区二区福利| 亚洲男人的天堂在线| 在线一区观看| 欧美精品亚洲一区二区在线播放| 欧美jjzz| 怡红院av一区二区三区| 校园春色综合网| 欧美一区二区三区四区在线观看| 欧美午夜在线| 亚洲美女毛片| 在线视频中文亚洲| 欧美激情一区二区三区在线视频观看 | 亚洲福利一区| 久久久另类综合| 老司机午夜精品| 亚洲高清一二三区| 巨胸喷奶水www久久久免费动漫| 久久久久久久91| 国内精品亚洲| 久久午夜电影| 欧美激情影院| 日韩一区二区福利| 欧美—级高清免费播放| 亚洲日本精品国产第一区| 99在线视频精品| 欧美视频在线免费| 亚洲制服欧美中文字幕中文字幕| 亚洲免费网站| 国产亚洲综合在线| 久久伊伊香蕉| 亚洲欧洲精品一区二区三区不卡| 亚洲精品一区二区三| 欧美日韩国产一级| 亚洲深夜福利| 久久裸体艺术| 最新精品在线| 国产精品极品美女粉嫩高清在线 | 艳妇臀荡乳欲伦亚洲一区| 性欧美超级视频| 激情六月婷婷久久| 欧美黑人在线观看| 亚洲一区二区三区四区在线观看| 久久国产精品免费一区| 国产综合色产| 欧美肥婆在线| 亚洲专区一区二区三区| 欧美**字幕| 亚洲免费在线视频| 亚洲福利视频二区| 国产精品黄色| 免费91麻豆精品国产自产在线观看| 亚洲精品久久久久久下一站| 欧美一区二区三区免费视频| 在线观看一区二区精品视频| 欧美99在线视频观看| 亚洲一区二区在线免费观看视频| 麻豆乱码国产一区二区三区| 在线一区二区视频| 好吊视频一区二区三区四区 | 欧美激情视频一区二区三区免费 | 亚洲激情在线观看| 欧美在线视频观看| 日韩亚洲精品视频| 国外视频精品毛片| 国产精品高潮呻吟久久| 免费久久99精品国产| 午夜国产不卡在线观看视频| 亚洲日本在线观看| 久久精品在线视频| 亚洲综合三区| 日韩视频精品| 亚洲国产精品久久精品怡红院| 国产精品网站视频| 欧美日韩成人综合天天影院| 久久精品免费| 午夜精品国产更新| 99亚洲一区二区| 91久久精品国产91久久| 久久综合影音| 久久精品人人做人人爽| 亚洲欧美制服另类日韩| 一本色道久久综合亚洲精品小说| 亚洲黄色成人| 樱桃视频在线观看一区| 国产亚洲欧美一区二区三区| 国产精品尤物| 国产精品裸体一区二区三区| 欧美精品在线一区二区三区| 免费在线观看精品| 久久综合一区二区| 久久婷婷综合激情| 美女尤物久久精品| 美女视频网站黄色亚洲| 久久蜜桃精品| 久久夜色精品国产亚洲aⅴ| 久久不射网站| 久久久久久久综合色一本| 久久福利视频导航| 久久久久久久久久码影片| 久久不见久久见免费视频1| 欧美中文字幕视频在线观看| 欧美影院视频| 久久这里只有| 欧美国产先锋| 欧美日本乱大交xxxxx| 欧美日韩一区二区在线播放| 欧美视频一区二区| 国产精品久久久亚洲一区 | 久久综合狠狠综合久久综青草| 久久久av水蜜桃| 欧美11—12娇小xxxx| 欧美激情成人在线视频| 欧美日韩免费在线观看| 国产精品免费看久久久香蕉| 国产精品一二一区| 国产一级精品aaaaa看| 一区在线视频| 99成人在线| 久久www免费人成看片高清| 久久精品综合一区| 欧美激情免费在线| 夜夜精品视频| 久久福利影视| 欧美精品videossex性护士| 国产精品高清在线| 影音先锋日韩资源| 在线午夜精品自拍| 久久久蜜桃精品| 亚洲精品之草原avav久久| 亚洲无线视频| 裸体一区二区| 国产精品私拍pans大尺度在线| 国产一区二区三区最好精华液| 亚洲精品欧美在线| 欧美一区二区福利在线| 欧美成人免费网| 亚洲小视频在线| 另类成人小视频在线| 国产精品久久久久久模特 | 在线观看一区| 欧美一区激情视频在线观看| 亚洲国产一区二区三区高清| 亚洲一区二区三区四区五区黄| 欧美sm视频| 黄色日韩精品| 午夜精品视频在线观看| 欧美激情影音先锋| 欧美在线黄色| 国产精品欧美日韩一区| 亚洲老司机av| 欧美a级一区二区| 新狼窝色av性久久久久久| 欧美日韩成人| 亚洲人成在线影院| 久久综合狠狠综合久久综青草| 亚洲无吗在线| 国产精品啊啊啊|