public GridWorld(float gridSize, int xExtend, int yExtend, Vector2 gridOrigin) { this.GridSize = gridSize; this.X_Extend = xExtend; this.Y_Extend = yExtend; this.Diagonal = gridSize * 1.414f; int meshCount = xExtend * yExtend; MeshRenders = new MeshRenderer[meshCount]; Centers = new Vector2[meshCount]; States = new State[meshCount]; //make each grids mesh int idx = 0; for (int i = 0; i < yExtend; i++) { for (int j = 0; j < xExtend; j++) { Vector2 center = new Vector2((gridSize * j) + (float)(gridSize / 2.0), (gridSize * i) + (float)(gridSize / 2.0)); center += gridOrigin; Centers[idx] = center; MeshRenders[idx] = MakeMesh(center, gridSize, idx.ToString()).GetComponent <MeshRenderer>(); States[idx] = State.Alive; idx++; } } CentersMin = Centers.First(); CentersMax = Centers.Last(); }
public static Vector2 PointInPolygonClosestToPoint(List <Vector2> orderedVertices, Vector2 point) { if (orderedVertices.Count == 0) { return(point); } if (orderedVertices.Count == 1) { return(orderedVertices[0]); } if (PointIsInPolygonByRaycasting(orderedVertices, point)) { return(point); } float[] sqrDists = new float[orderedVertices.Count]; Vector2[] closestPoints = new Vector2[orderedVertices.Count]; for (int i = 0; i < orderedVertices.Count; i++) { closestPoints[i] = ClosestPointOnLineSegment(point, orderedVertices[i], orderedVertices[(i + 1) % orderedVertices.Count]); sqrDists[i] = Vector2.SqrMagnitude(point - closestPoints.Last()); } int index = System.Array.IndexOf <float>(sqrDists, Mathf.Min(sqrDists)); return(closestPoints[index]); }
/// <summary> /// Builds a new truncated cone buffer. /// The cone is aligned to the x axis. /// </summary> /// <param name="description">The description used to build the cone</param> /// <param name="tesselation">The tesselation, must be greater than 2</param> public VertexPosition[] Build(TruncatedConeDescription description, int tesselation = 32) { var sections = description.Sections; if (sections.Length < 2) { throw new ArgumentException("sections must contain more than 1 element."); } if (tesselation < 3) { throw new ArgumentException("tesselation must be greater than 2."); } // build tesselation angles var tessInverse = (float)1 / tesselation; var tesselationAngles = new Vector2[tesselation]; for (var i = 0; i < tesselationAngles.Length; i++) { var quat = Quaternion.RotationAxis(Vector3.BackwardRH, tessInverse * i * MathUtil.TwoPi); var transformed = Vector3.Transform(Vector3.Up, quat); tesselationAngles[i] = new Vector2(transformed.X, transformed.Y);//discard x value } var vertCount = (sections.Length - 1) * tesselation * 6; var buffer = new VertexPosition[vertCount]; // the first time this gets used it increments to 0 int bufferIndex = -1; var prevSection = sections[0]; for (var i = 1; i < description.Sections.Length; i++) { var section = description.Sections[i]; // start at the last item var prevAngle = tesselationAngles.Last(); for (var j = 0; j < tesselation; j++) { var angle = tesselationAngles[j]; buffer[++bufferIndex] = GetVertex(prevSection, angle); buffer[++bufferIndex] = GetVertex(section, angle); buffer[++bufferIndex] = GetVertex(prevSection, prevAngle); buffer[++bufferIndex] = GetVertex(section, angle); buffer[++bufferIndex] = GetVertex(section, prevAngle); buffer[++bufferIndex] = GetVertex(prevSection, prevAngle); prevAngle = tesselationAngles[j]; } prevSection = sections[i]; } Debug.Assert(bufferIndex + 1 == buffer.Length); return(buffer); }
private void DrawGraph() { UltiDraw.Begin(); Color[] colors = UltiDraw.GetRainbowColors(Values.Length); Vector2 pivot = Rect.GetPosition(); float radius = 0.2f * Rect.W; UltiDraw.DrawGUICircle(pivot, Rect.W * 1.05f, UltiDraw.Gold); UltiDraw.DrawGUICircle(pivot, Rect.W, UltiDraw.White); Vector2[] anchors = new Vector2[Values.Length]; for (int i = 0; i < Values.Length; i++) { float step = (float)i / (float)Values.Length; anchors[i] = new Vector2((Rect.W - radius / 2f) * Screen.height / Screen.width * Mathf.Cos(step * 2f * Mathf.PI), (Rect.W - radius / 2f) * Mathf.Sin(step * 2f * Mathf.PI)); } Vector2[] positions = new Vector2[Frames]; for (int i = 0; i < Values.Length; i++) { int _index = 0; foreach (float value in Values[i]) { positions[_index] += value * anchors[i]; _index += 1; } } for (int i = 1; i < positions.Length; i++) { UltiDraw.DrawGUILine(pivot + positions[i - 1], pivot + positions[i], 0.1f * radius, UltiDraw.Black.Transparent((float)(i + 1) / (float)positions.Length)); } for (int i = 0; i < anchors.Length; i++) { UltiDraw.DrawGUILine(pivot + positions.Last(), pivot + anchors[i], 0.1f * radius, colors[i].Transparent(Weights[i])); UltiDraw.DrawGUICircle(pivot + anchors[i], Mathf.Max(0.5f * radius, Utility.Normalise(Weights[i], 0f, 1f, 0.5f, 1f) * radius), Color.Lerp(UltiDraw.Black, colors[i], Weights[i])); } UltiDraw.DrawGUICircle(pivot + positions.Last(), 0.5f * radius, UltiDraw.Purple); UltiDraw.End(); }
/// <summary> /// Determines if a CCW defined polygon is convex. /// </summary> /// <param name="verts">The vertices of the polygon.</param> /// <returns>True if polygon is convex.</returns> public static bool PolyIsConvex( Vector2[] verts ) { int nVerts = verts.Length; Vector2 last = verts.Last(); for ( int i = 0; i <= nVerts; ++i ) { Vector2 vert = verts[( i + 1 ) % nVerts]; Vector2 next = verts[( i + 2 ) % nVerts]; Vector2 u = vert - last; Vector2 v = next - vert; if ( u.X * v.Y - u.Y * v.X < 0f ) return false; } return true; }
public Vector2[] CleanPath(Vector2[] path) { if (path == null || path.Length == 0) { return path; } var newPath = new List<Vector2>(); var current = path[0]; for (var i = 1; i < path.Length; i++) { if (Evade.ClippedPolygons.Any(p => p.IsIntersectingWithLineSegment(current, path[i]))) { newPath.Add(path[i]); current = path[i]; } } newPath.Add(current); newPath.Add(path.Last()); return newPath.ToArray(); }