public TriangleResult GetTriangleInfo(Vector3 point, int triangle)
            {
                var result = new TriangleResult();

                result.triangle        = triangle;
                result.distanceSquared = float.PositiveInfinity;

                if (triangle >= Length)
                {
                    return(result);
                }


                //Get the vertices of the triangle
                var p1 = Vertices[Triangles[0 + triangle * 3]];
                var p2 = Vertices[Triangles[1 + triangle * 3]];
                var p3 = Vertices[Triangles[2 + triangle * 3]];

                result.normal = Vector3.Cross((p2 - p1).normalized, (p3 - p1).normalized);

                //Project our point onto the plane
                var projected = point + Vector3.Dot((p1 - point), result.normal) * result.normal;

                //Calculate the barycentric coordinates
                var u = ((projected.x * p2.y) - (projected.x * p3.y) - (p2.x * projected.y) + (p2.x * p3.y) + (p3.x * projected.y) - (p3.x * p2.y)) /
                        ((p1.x * p2.y) - (p1.x * p3.y) - (p2.x * p1.y) + (p2.x * p3.y) + (p3.x * p1.y) - (p3.x * p2.y));
                var v = ((p1.x * projected.y) - (p1.x * p3.y) - (projected.x * p1.y) + (projected.x * p3.y) + (p3.x * p1.y) - (p3.x * projected.y)) /
                        ((p1.x * p2.y) - (p1.x * p3.y) - (p2.x * p1.y) + (p2.x * p3.y) + (p3.x * p1.y) - (p3.x * p2.y));
                var w = ((p1.x * p2.y) - (p1.x * projected.y) - (p2.x * p1.y) + (p2.x * projected.y) + (projected.x * p1.y) - (projected.x * p2.y)) /
                        ((p1.x * p2.y) - (p1.x * p3.y) - (p2.x * p1.y) + (p2.x * p3.y) + (p3.x * p1.y) - (p3.x * p2.y));

                result.centre = p1 * 0.3333f + p2 * 0.3333f + p3 * 0.3333f;

                //Find the nearest point
                var vector = (new Vector3(u, v, w)).normalized;

                //work out where that point is
                var nearest = p1 * vector.x + p2 * vector.y + p3 * vector.z;

                result.closestPoint    = nearest;
                result.distanceSquared = (nearest - point).sqrMagnitude;

                if (float.IsNaN(result.distanceSquared))
                {
                    result.distanceSquared = float.PositiveInfinity;
                }

                return(result);
            }
示例#2
0
        /// <summary>
        /// Given a row (A-F) and a column (1-12), return the vertices of the triangle.
        /// </summary>
        public TriangleResult GetVertices(string rowText, int column)
        {
            // Turn the row variable into a number:
            int row = Convert.ToInt32(rowText[0]) - Convert.ToInt32('A');

            // The square that we're inside of is shared by A1/A2, A3/A4, so we can get the column of the box by dividing by 2.
            // "col" refers to the 6 rectangular columns now.  This is useful because A1/A2 share their upper left and lower right coordinates
            int col = (column - 1) / 2;

            if (row < 0 || row > 5 || col < 0 || col > 5)
            {
                // Maybe log something here if needed.  This is kind of a lazy way of keeping the view model from being polluted w/ success/fail flags and handling them.
                throw new Exception();
            }

            // So now we have enough to get the upper left and lower right coords.  We'll put those in (x2, y2) and (x3, y3) in order to match
            // the naming convention established on page 3 of the pdf:

            var result = new TriangleResult
            {
                x2 = col * 10,
                y2 = row * 10,

                x3 = (col + 1) * 10,
                y3 = (row + 1) * 10
            };

            // The last thing to do is establish if we're looking at a triangle that occupies the lower left half of the square (A1, A3, A5, A7, A9, A11, B1...)
            // or if we're looking at the triangle in the upper right (A2, A4, A6, A8, A10, A12, B2...).  If it's odd, we want x1 to be set to the same value as x2 and y1
            // to be equal to y3.  If it's even, we want x1 to equal x3 and y1 to equal y2.

            result.x1 = column % 2 == 1 ? result.x2 : result.x3;
            result.y1 = column % 2 == 1 ? result.y3 : result.y2;

            return(result);
        }