private static Polygon CreateShape(double width, double[] heights, Vector[] vertexes) { int n = heights.Length; IndexTriangle[] triangles = new IndexTriangle[(n - 1) * 2]; Int16[] outlineIndices = new Int16[n * 2]; for (int i = 0; i < n * 2; i++) { outlineIndices[i] = (Int16)i; } for (int i = 0; i < n - 1; i++) { triangles[2 * i] = new IndexTriangle(i, 2 * n - i - 2, 2 * n - i - 1); triangles[2 * i + 1] = new IndexTriangle(i, i + 1, 2 * n - i - 2); } Vector[] outlineVertices = new Vector[outlineIndices.Length]; for (int i = 0; i < outlineIndices.Length; i++) { outlineVertices[i] = vertexes[outlineIndices[i]]; } return(new Polygon(new ShapeCache(vertexes, triangles, outlineIndices), false)); }
internal static ShapeCache CreateRegularPolygonCache(int vertexCount) { double angleStep = 2 * Math.PI / vertexCount; Int16 centerIndex = (Int16)vertexCount; Vector[] vertices = new Vector[vertexCount + 1]; IndexTriangle[] triangles = new IndexTriangle[vertexCount]; Int16[] outlineIndices = new Int16[vertexCount]; for (int i = 0; i < vertexCount; i++) { double a = i * angleStep; vertices[i] = new Vector(0.5 * Math.Cos(a), 0.5 * Math.Sin(a)); outlineIndices[i] = (Int16)i; } vertices[centerIndex] = Vector.Zero; for (int i = 0; i < vertexCount - 1; i++) { triangles[i] = new IndexTriangle((Int16)i, centerIndex, (Int16)(i + 1)); } triangles[vertexCount - 1] = new IndexTriangle((Int16)(vertexCount - 1), centerIndex, (Int16)0); return(new ShapeCache(vertices, triangles, outlineIndices)); }
/// <summary> /// Luo tien kentälle. /// </summary> public void Insert() { if (wayPoints.Length < 2) { throw new ArgumentException("Must have at least 2 points"); } angles = new Angle[wayPoints.Length]; Vector first = wayPoints[0]; Vector second = wayPoints[1]; Vector toBeginning = (first - second).Normalize(); Vector beginning = first + toBeginning; Vector previousLeft, previousRight; CalculatePoints(beginning, first, second, out previousLeft, out previousRight); angles[0] = (second - first).Angle; Vector secondToLast = wayPoints[wayPoints.Length - 2]; Vector last = wayPoints[wayPoints.Length - 1]; Vector toVeryLast = (last - secondToLast).Normalize(); Vector veryLast = last + toVeryLast; for (int i = 1; i < wayPoints.Length; i++) { Vector previous = wayPoints[i - 1]; Vector current = wayPoints[i]; Vector next; Vector toPrevious = (previous - current).Normalize(); Vector toNext; if (i == wayPoints.Length - 1) { next = veryLast; toNext = toVeryLast; } else { next = wayPoints[i + 1]; toNext = (next - current).Normalize(); } Vector left, right; CalculatePoints(previous, current, next, out left, out right); angles[i] = (next - previous).Angle; Vector center = previous + toNext / 2; Vector[] vertices = new Vector[4]; vertices[0] = previousLeft - center; vertices[1] = previousRight - center; vertices[2] = right - center; vertices[3] = left - center; IndexTriangle[] triangles = new IndexTriangle[] { new IndexTriangle(0, 3, 1), new IndexTriangle(1, 3, 2) }; ShapeCache cache = new ShapeCache(vertices, triangles); Polygon shape = new Polygon(cache, false); PhysicsObject segment = CreateSegmentFunction(100, 100, shape); segment.Position = center; Segments[i - 1] = segment; previousLeft = left; previousRight = right; } }