public void DrawSegment(CCPoint from, CCPoint to, float radius, CCColor4F color) { var cl = color.ToColor(); var a = from; var b = to; var n = CCPoint.Normalize(CCPoint.PerpendicularCCW(a - b)); var lww = radius; var nw = n * lww; var v0 = b - nw; var v1 = b + nw; var v2 = a - nw; var v3 = a + nw; // Triangles from beginning to end triangleVertices.Add(new VertexPositionColor(v1.ToVector3(), cl)); triangleVertices.Add(new VertexPositionColor(v2.ToVector3(), cl)); triangleVertices.Add(new VertexPositionColor(v0.ToVector3(), cl)); triangleVertices.Add(new VertexPositionColor(v1.ToVector3(), cl)); triangleVertices.Add(new VertexPositionColor(v2.ToVector3(), cl)); triangleVertices.Add(new VertexPositionColor(v3.ToVector3(), cl)); var mb = (float)Math.Atan2(v1.Y - b.Y, v1.X - b.X); var ma = (float)Math.Atan2(v2.Y - a.Y, v2.X - a.X); // Draw rounded line caps DrawSolidArc(a, radius, ma, MathHelper.Pi, color); DrawSolidArc(b, radius, mb, MathHelper.Pi, color); dirty = true; }
// See http://slabode.exofire.net/circle_draw.shtml // An Efficient Way to Draw Approximate Circles in OpenGL // Try to keep from calculating Cos and Sin of values everytime and just use // add and subtract where possible to calculate the values. public void DrawDot(CCPoint pos, float radius, CCColor4F color) { var cl = color.ToColor(); var segments = 10 * (float)Math.Sqrt(radius); //<- Let's try to guess at # segments for a reasonable smoothness float theta = MathHelper.Pi * 2.0f / segments; float tangetial_factor = (float)Math.Tan(theta); //calculate the tangential factor float radial_factor = (float)Math.Cos(theta); //calculate the radial factor float x = radius; //we start at angle = 0 float y = 0; var verticeCenter = new VertexPositionColor(new Vector3(pos.X, pos.Y, 0), cl); var vert1 = new VertexPositionColor(Vector3.Zero, cl); float tx = 0; float ty = 0; for (int i = 0; i < segments; i++) { vert1.Position.X = x + pos.X; vert1.Position.Y = y + pos.Y; triangleVertices.Add(vert1); // output vertex //calculate the tangential vector //remember, the radial vector is (x, y) //to get the tangential vector we flip those coordinates and negate one of them tx = -y; ty = x; //add the tangential vector x += tx * tangetial_factor; y += ty * tangetial_factor; //correct using the radial factor x *= radial_factor; y *= radial_factor; vert1.Position.X = x + pos.X; vert1.Position.Y = y + pos.Y; triangleVertices.Add(vert1); // output vertex triangleVertices.Add(verticeCenter); } dirty = true; }
// See http://slabode.exofire.net/circle_draw.shtml // An Efficient Way to Draw Approximate Circles in OpenGL // Try to keep from calculating Cos and Sin of values everytime and just use // add and subtract where possible to calculate the values. public void DrawDot(CCPoint pos, float radius, CCColor4F color) { var cl = color.ToColor(); var segments = 10 * (float)Math.Sqrt(radius); //<- Let's try to guess at # segments for a reasonable smoothness float theta = MathHelper.Pi * 2.0f / segments; float tangetial_factor = (float)Math.Tan(theta); //calculate the tangential factor float radial_factor = (float)Math.Cos(theta); //calculate the radial factor float x = radius; //we start at angle = 0 float y = 0; var verticeCenter = new VertexPositionColor(new Vector3(pos.X, pos.Y, 0), cl); var vert1 = new VertexPositionColor(Vector3.Zero, cl); float tx = 0; float ty = 0; for (int i = 0; i < segments; i++) { vert1.Position.X = x + pos.X; vert1.Position.Y = y + pos.Y; triangleVertices.Add(vert1); // output vertex //calculate the tangential vector //remember, the radial vector is (x, y) //to get the tangential vector we flip those coordinates and negate one of them tx = -y; ty = x; //add the tangential vector x += tx * tangetial_factor; y += ty * tangetial_factor; //correct using the radial factor x *= radial_factor; y *= radial_factor; vert1.Position.X = x + pos.X; vert1.Position.Y = y + pos.Y; triangleVertices.Add(vert1); // output vertex triangleVertices.Add(verticeCenter); } dirty = true; }
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 = fillColor.ToColor(); var borderFill = borderColor.ToColor(); 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); triangleVertices.Add(new VertexPositionColor(v0.ToVector3(), colorFill)); //__t(v2fzero) triangleVertices.Add(new VertexPositionColor(v1.ToVector3(), colorFill)); //__t(v2fzero) triangleVertices.Add(new VertexPositionColor(v2.ToVector3(), 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)); triangleVertices.Add(new VertexPositionColor(inner0.ToVector3(), borderFill)); //__t(v2fneg(n0)) triangleVertices.Add(new VertexPositionColor(inner1.ToVector3(), borderFill)); //__t(v2fneg(n0)) triangleVertices.Add(new VertexPositionColor(outer1.ToVector3(), borderFill)); //__t(n0) triangleVertices.Add(new VertexPositionColor(inner0.ToVector3(), borderFill)); //__t(v2fneg(n0)) triangleVertices.Add(new VertexPositionColor(outer0.ToVector3(), borderFill)); //__t(n0) triangleVertices.Add(new VertexPositionColor(outer1.ToVector3(), 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)); triangleVertices.Add(new VertexPositionColor(inner0.ToVector3(), colorFill)); //__t(v2fzero) triangleVertices.Add(new VertexPositionColor(inner1.ToVector3(), colorFill)); //__t(v2fzero) triangleVertices.Add(new VertexPositionColor(outer1.ToVector3(), colorFill)); //__t(n0) triangleVertices.Add(new VertexPositionColor(inner0.ToVector3(), colorFill)); //__t(v2fzero) triangleVertices.Add(new VertexPositionColor(outer0.ToVector3(), colorFill)); //__t(n0) triangleVertices.Add(new VertexPositionColor(outer1.ToVector3(), colorFill)); //__t(n0) } } dirty = true; }
public void DrawSegment(CCPoint from, CCPoint to, float radius, CCColor4F color) { var cl = color.ToColor(); var a = from; var b = to; var n = CCPoint.Normalize(CCPoint.PerpendicularCCW(a - b)); var lww = radius; var nw = n * lww; var v0 = b - nw; var v1 = b + nw; var v2 = a - nw; var v3 = a + nw; // Triangles from beginning to end triangleVertices.Add(new VertexPositionColor(v1.ToVector3(), cl)); triangleVertices.Add(new VertexPositionColor(v2.ToVector3(), cl)); triangleVertices.Add(new VertexPositionColor(v0.ToVector3(), cl)); triangleVertices.Add(new VertexPositionColor(v1.ToVector3(), cl)); triangleVertices.Add(new VertexPositionColor(v2.ToVector3(), cl)); triangleVertices.Add(new VertexPositionColor(v3.ToVector3(), cl)); var mb = (float)Math.Atan2(v1.Y - b.Y, v1.X - b.X); var ma = (float)Math.Atan2(v2.Y - a.Y, v2.X - a.X); // Draw rounded line caps DrawSolidArc(a, radius, ma, MathHelper.Pi, color); DrawSolidArc(b, radius, mb, MathHelper.Pi, color); dirty = true; }
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 = fillColor.ToColor(); var borderFill = borderColor.ToColor(); 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); triangleVertices.Add(new VertexPositionColor(v0.ToVector3(), colorFill)); //__t(v2fzero) triangleVertices.Add(new VertexPositionColor(v1.ToVector3(), colorFill)); //__t(v2fzero) triangleVertices.Add(new VertexPositionColor(v2.ToVector3(), 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)); triangleVertices.Add(new VertexPositionColor(inner0.ToVector3(), borderFill)); //__t(v2fneg(n0)) triangleVertices.Add(new VertexPositionColor(inner1.ToVector3(), borderFill)); //__t(v2fneg(n0)) triangleVertices.Add(new VertexPositionColor(outer1.ToVector3(), borderFill)); //__t(n0) triangleVertices.Add(new VertexPositionColor(inner0.ToVector3(), borderFill)); //__t(v2fneg(n0)) triangleVertices.Add(new VertexPositionColor(outer0.ToVector3(), borderFill)); //__t(n0) triangleVertices.Add(new VertexPositionColor(outer1.ToVector3(), 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)); triangleVertices.Add(new VertexPositionColor(inner0.ToVector3(), colorFill)); //__t(v2fzero) triangleVertices.Add(new VertexPositionColor(inner1.ToVector3(), colorFill)); //__t(v2fzero) triangleVertices.Add(new VertexPositionColor(outer1.ToVector3(), colorFill)); //__t(n0) triangleVertices.Add(new VertexPositionColor(inner0.ToVector3(), colorFill)); //__t(v2fzero) triangleVertices.Add(new VertexPositionColor(outer0.ToVector3(), colorFill)); //__t(n0) triangleVertices.Add(new VertexPositionColor(outer1.ToVector3(), colorFill)); //__t(n0) } } dirty = true; }