public static void DrawPolygon(SpriteBatch batch, Texture2D blank, float width, Color color, Polygon p) { foreach (Edge e in p.Edges) { DrawLine(batch, blank, width, color, e); } }
public static Polygon RelativeToCamera(Polygon p) { Polygon tmpPol = new Polygon(); foreach (Edge e in p.Edges) { tmpPol.Edges.Add(Camera.RelativeToCamera(e)); } return tmpPol; }
public static bool PointInPolygon(Point point, Polygon polygon) { bool inside = false; foreach (var side in polygon.Edges) { if (point.Y > Math.Min(side.StartVertex.Y, side.EndVertex.Y)) if (point.Y <= Math.Max(side.StartVertex.Y, side.EndVertex.Y)) if (point.X <= Math.Max(side.StartVertex.X, side.EndVertex.X)) { float xIntersection = side.StartVertex.X + ((point.Y - side.StartVertex.Y) / (side.EndVertex.Y - side.StartVertex.Y)) * (side.EndVertex.X - side.StartVertex.X); if (point.X <= xIntersection) inside = !inside; } } return inside; }
/// <summary> /// Só serve se for um losango /// </summary> /// <param name="p">Losango</param> /// <returns>O ponto central do losango</returns> public static Point CalculateCenter(Polygon p) { Edge e1 = p.Edges[0]; Edge e2 = p.Edges[1]; Edge e3 = p.Edges[2]; //int x = Convert.ToInt32(Math.Ceiling(e1.StartVertex.X + (e2.EndVertex.X / 2))); //int y = Convert.ToInt32(Math.Ceiling(e3.EndVertex.Y - (e2.StartVertex.Y / 2))); int x = Convert.ToInt32(e1.StartVertex.X + (e2.EndVertex.X / 2)); int y1 = Convert.ToInt32(e1.EndVertex.Y > 0 ? e1.EndVertex.Y : e1.EndVertex.Y * -1); int y2 = Convert.ToInt32(e3.EndVertex.Y > 0 ? e3.EndVertex.Y : e3.EndVertex.Y * -1); int y = Convert.ToInt32((y1 + y2) / 2); //Vector2.dis return new Point(x, y); }
private void CalculateEdges() { TopPolygon = new Polygon(); //primeira aresta "de cima" /* * Start * / * / * / * End * */ TopPolygon.Edges.Add(new Edge() { StartVertex = new Vector2 ( Position.Left + BaseWidth / 2, //posição X + largura / 2 Position.Top //Y do topo do retangulo ), EndVertex = new Vector2 ( Position.Left, //X do retangulo Position.Top + BaseHeight / 2 //positção Y + altura / 4 ) }); //segunda aresta "de cima" /* * End * \ * \ * \ * Start * */ TopPolygon.Edges.Add(new Edge() { StartVertex = new Vector2 ( Position.Left + BaseWidth, //posição X + largura / 2 Position.Top + BaseHeight / 2 //posição Y + altura / 2 ), EndVertex = new Vector2 ( Position.Left + BaseWidth / 2, //posição X + largura / 2 Position.Top //Y do topo do retangulo ) }); //primeira aresta "de baixo" /* * Start * \ * \ * \ * End * */ TopPolygon.Edges.Add(new Edge() { StartVertex = new Vector2 ( Position.Left, //X do retangulo Position.Top + BaseHeight / 2 //positção Y + BaseHeight / 2 ), EndVertex = new Vector2 ( Position.Left + BaseWidth / 2, //largura / 2 Position.Y + BaseHeight //ja faço coment ) }); //segunda aresta "de baixo" /* * End * / * / * / * Start * */ TopPolygon.Edges.Add(new Edge() { StartVertex = new Vector2 ( Position.Left + BaseWidth / 2, //largura / 2 Position.Y + BaseHeight //ja faço coment ), EndVertex = new Vector2 ( Position.Left + BaseWidth, //largura / 2 Position.Y + BaseHeight / 2 //ja faço coment ) }); }
private void CalculateCityEdges() { MapPolygon = new Polygon(); //primeira aresta "de cima" /* * Start * / * / * / * End * */ Edge e1 = new Edge { StartVertex = Blocks[0, 0].CityBlock.TopPolygon.Edges[0].StartVertex, EndVertex = Blocks[0, Height - 1].CityBlock.TopPolygon.Edges[0].EndVertex }; ////segunda aresta "de cima" ///* // * End // * \ // * \ // * \ // * Start // * */ Edge e2 = new Edge { StartVertex = Blocks[Width - 1, 0].CityBlock.TopPolygon.Edges[3].EndVertex, EndVertex = e1.StartVertex }; ////primeira aresta "de baixo" ///* // * Start // * \ // * \ // * \ // * End // * */ Edge e3 = new Edge { StartVertex = e1.EndVertex, EndVertex = Blocks[Width - 1, Height - 1].CityBlock.TopPolygon.Edges[3].StartVertex }; ////segunda aresta "de baixo" ///* // * End // * / // * / // * / // * Start // * */ Edge e4 = new Edge { StartVertex = e3.EndVertex, EndVertex = e2.StartVertex }; MapPolygon.Edges.Add(e1); MapPolygon.Edges.Add(e2); MapPolygon.Edges.Add(e3); MapPolygon.Edges.Add(e4); }