public void DrawPolygon(CCPoint[] verts, int count, CCColor4F fillColor, float borderWidth, CCColor4F borderColor) { var extrude = new ExtrudeVerts[count]; for (int i = 0; i < count; i++) { var v0 = verts[(i - 1 + count) % count]; var v1 = verts[i]; var v2 = verts[(i + 1) % count]; var n1 = CCPoint.Normalize(CCPoint.PerpendicularCCW(v1 - v0)); var n2 = CCPoint.Normalize(CCPoint.PerpendicularCCW(v2 - v1)); var offset = (n1 + n2) * (1.0f / (CCPoint.Dot(n1, n2) + 1.0f)); extrude[i] = new ExtrudeVerts() { offset = offset, n = n2 }; } bool outline = (borderColor.A > 0.0f && borderWidth > 0.0f); var colorFill = new CCColor4B(fillColor); var borderFill = new CCColor4B(borderColor); float inset = (!outline ? 0.5f : 0.0f); for (int i = 0; i < count - 2; i++) { var v0 = verts[0] - (extrude[0].offset * inset); var v1 = verts[i + 1] - (extrude[i + 1].offset * inset); var v2 = verts[i + 2] - (extrude[i + 2].offset * inset); AddTriangleVertex(new CCV3F_C4B(v0, colorFill)); //__t(v2fzero) AddTriangleVertex(new CCV3F_C4B(v1, colorFill)); //__t(v2fzero) AddTriangleVertex(new CCV3F_C4B(v2, colorFill)); //__t(v2fzero) } for (int i = 0; i < count; i++) { int j = (i + 1) % count; var v0 = verts[i]; var v1 = verts[j]; var n0 = extrude[i].n; var offset0 = extrude[i].offset; var offset1 = extrude[j].offset; if (outline) { var inner0 = (v0 - (offset0 * borderWidth)); var inner1 = (v1 - (offset1 * borderWidth)); var outer0 = (v0 + (offset0 * borderWidth)); var outer1 = (v1 + (offset1 * borderWidth)); AddTriangleVertex(new CCV3F_C4B(inner0, borderFill)); //__t(v2fneg(n0)) AddTriangleVertex(new CCV3F_C4B(inner1, borderFill)); //__t(v2fneg(n0)) AddTriangleVertex(new CCV3F_C4B(outer1, borderFill)); //__t(n0) AddTriangleVertex(new CCV3F_C4B(inner0, borderFill)); //__t(v2fneg(n0)) AddTriangleVertex(new CCV3F_C4B(outer0, borderFill)); //__t(n0) AddTriangleVertex(new CCV3F_C4B(outer1, borderFill)); //__t(n0) } else { var inner0 = (v0 - (offset0 * 0.5f)); var inner1 = (v1 - (offset1 * 0.5f)); var outer0 = (v0 + (offset0 * 0.5f)); var outer1 = (v1 + (offset1 * 0.5f)); AddTriangleVertex(new CCV3F_C4B(inner0, colorFill)); //__t(v2fzero) AddTriangleVertex(new CCV3F_C4B(inner1, colorFill)); //__t(v2fzero) AddTriangleVertex(new CCV3F_C4B(outer1, colorFill)); //__t(n0) AddTriangleVertex(new CCV3F_C4B(inner0, colorFill)); //__t(v2fzero) AddTriangleVertex(new CCV3F_C4B(outer0, colorFill)); //__t(n0) AddTriangleVertex(new CCV3F_C4B(outer1, colorFill)); //__t(n0) } } dirty = true; }
/// Clamp CCPoint p to length len. public static CCPoint Clamp(CCPoint p, float len) { return((CCPoint.Dot(p, p) > len * len) ? CCPoint.Normalize(p) * len : p); }
public void DrawPolygon(CCPoint[] verts, int count, CCColor4F fillColor, float borderWidth, CCColor4F borderColor, bool closePolygon = true) { var polycount = count; var colorFill = new CCColor4B(fillColor); var borderFill = new CCColor4B(borderColor); bool outline = (borderColor.A > 0.0f && borderWidth > 0.0f); bool fill = fillColor.A > 0.0f; var numberOfTriangles = outline ? (3 * polycount - 2) : (polycount - 2); if (numberOfTriangles > 0 && fill && closePolygon) { for (int i = 1; i < polycount - 1; i++) { AddTriangleVertex(new CCV3F_C4B(verts[0], colorFill)); AddTriangleVertex(new CCV3F_C4B(verts[i], colorFill)); AddTriangleVertex(new CCV3F_C4B(verts[i + 1], colorFill)); } } else { for (int i = 0; i < polycount - 1; i++) { DrawLine(verts[i], verts[i + 1], borderWidth, colorFill); } } if (outline) { var extrude = new ExtrudeVerts[polycount]; for (int i = 0; i < polycount; i++) { var v0 = verts[(i - 1 + polycount) % polycount]; var v1 = verts[i]; var v2 = verts[(i + 1) % polycount]; var n1 = CCPoint.Normalize(CCPoint.PerpendicularCCW(v1 - v0)); var n2 = CCPoint.Normalize(CCPoint.PerpendicularCCW(v2 - v1)); var offset = (n1 + n2) * (1.0f / (CCPoint.Dot(n1, n2) + 1.0f)); extrude[i] = new ExtrudeVerts() { offset = offset, n = n2 }; } float inset = (!outline ? 0.5f : 0.0f); for (int i = 0; i < polycount - 2; i++) { var v0 = verts[0] - (extrude[0].offset * inset); var v1 = verts[i + 1] - (extrude[i + 1].offset * inset); var v2 = verts[i + 2] - (extrude[i + 2].offset * inset); AddTriangleVertex(new CCV3F_C4B(v0, colorFill)); //__t(v2fzero) AddTriangleVertex(new CCV3F_C4B(v1, colorFill)); //__t(v2fzero) AddTriangleVertex(new CCV3F_C4B(v2, colorFill)); //__t(v2fzero) } for (int i = 0; i < polycount - 1; i++) { int j = (i + 1) % polycount; var v0 = verts[i]; var v1 = verts[j]; var offset0 = extrude[i].offset; var offset1 = extrude[j].offset; var inner0 = (v0 - (offset0 * borderWidth)); var inner1 = (v1 - (offset1 * borderWidth)); var outer0 = (v0 + (offset0 * borderWidth)); var outer1 = (v1 + (offset1 * borderWidth)); AddTriangleVertex(new CCV3F_C4B(inner0, borderFill)); AddTriangleVertex(new CCV3F_C4B(inner1, borderFill)); AddTriangleVertex(new CCV3F_C4B(outer1, borderFill)); AddTriangleVertex(new CCV3F_C4B(inner0, borderFill)); AddTriangleVertex(new CCV3F_C4B(outer0, borderFill)); AddTriangleVertex(new CCV3F_C4B(outer1, borderFill)); } if (closePolygon) { for (int i = polycount - 1; i < polycount; i++) { int j = (i + 1) % polycount; var v0 = verts[i]; var v1 = verts[j]; var offset0 = extrude[i].offset; var offset1 = extrude[j].offset; var inner0 = (v0 - (offset0 * borderWidth)); var inner1 = (v1 - (offset1 * borderWidth)); var outer0 = (v0 + (offset0 * borderWidth)); var outer1 = (v1 + (offset1 * borderWidth)); AddTriangleVertex(new CCV3F_C4B(inner0, borderFill)); AddTriangleVertex(new CCV3F_C4B(inner1, borderFill)); AddTriangleVertex(new CCV3F_C4B(outer1, borderFill)); AddTriangleVertex(new CCV3F_C4B(inner0, borderFill)); AddTriangleVertex(new CCV3F_C4B(outer0, borderFill)); AddTriangleVertex(new CCV3F_C4B(outer1, borderFill)); } } } dirty = true; }
static void VertexLineToPolygon(CCPoint[] points, float stroke, CCV3F_C4B_T2F[] vertices, int offset, int nuPoints) { nuPoints += offset; if (nuPoints <= 1) { return; } stroke *= 0.5f; int idx; int nuPointsMinus = nuPoints - 1; float rad70 = MathHelper.ToRadians(70); float rad170 = MathHelper.ToRadians(170); for (int i = offset; i < nuPoints; i++) { idx = i * 2; CCPoint p1 = points[i]; CCPoint perpVector; if (i == 0) { perpVector = CCPoint.PerpendicularCCW(CCPoint.Normalize(p1 - points[i + 1])); } else if (i == nuPointsMinus) { perpVector = CCPoint.PerpendicularCCW(CCPoint.Normalize(points[i - 1] - p1)); } else { CCPoint p2 = points[i + 1]; CCPoint p0 = points[i - 1]; CCPoint p2p1 = CCPoint.Normalize(p2 - p1); CCPoint p0p1 = CCPoint.Normalize(p0 - p1); // Calculate angle between vectors var angle = (float)Math.Acos(CCPoint.Dot(p2p1, p0p1)); if (angle < rad70) { perpVector = CCPoint.PerpendicularCCW(CCPoint.Normalize(CCPoint.Midpoint(p2p1, p0p1))); } else if (angle < rad170) { perpVector = CCPoint.Normalize(CCPoint.Midpoint(p2p1, p0p1)); } else { perpVector = CCPoint.PerpendicularCCW(CCPoint.Normalize(p2 - p0)); } } perpVector = perpVector * stroke; vertices[idx].Vertices = new CCVertex3F(p1.X + perpVector.X, p1.Y + perpVector.Y, 0); vertices[idx + 1].Vertices = new CCVertex3F(p1.X - perpVector.X, p1.Y - perpVector.Y, 0); } // Validate vertexes offset = (offset == 0) ? 0 : offset - 1; for (int i = offset; i < nuPointsMinus; i++) { idx = i * 2; int idx1 = idx + 2; CCVertex3F p1 = vertices[idx].Vertices; CCVertex3F p2 = vertices[idx + 1].Vertices; CCVertex3F p3 = vertices[idx1].Vertices; CCVertex3F p4 = vertices[idx1 + 1].Vertices; float s; bool fixVertex = !VertexLineIntersect(p1.X, p1.Y, p4.X, p4.Y, p2.X, p2.Y, p3.X, p3.Y, out s); if (!fixVertex) { if (s < 0.0f || s > 1.0f) { fixVertex = true; } } if (fixVertex) { vertices[idx1].Vertices = p4; vertices[idx1 + 1].Vertices = p3; } } }