Example #1
0
        // Taken from http://forums.create.msdn.com/forums/t/7414.aspx?PageIndex=1
        public static Shape CreateEllipse(Vector2 position, float semiMajorAxis, float semiMinorAxis, float angleOffset, int sides)
        {
            Shape s = new Shape();

            float max = 2.0f * (float)Math.PI;
            float step = max / (float)sides;
            float h = 0.0f;
            float k = 0.0f;

            for (float t = 0.0f; t < max; t += step)
            {
                // center point: (h,k); add as argument if you want (to circumvent modifying this.Position)
                // x = h + a*cos(t)  -- a is semimajor axis, b is semiminor axis
                // y = k + b*sin(t)
                s._vertices.Add(position + new Vector2((float)(h + semiMajorAxis * Math.Cos(t)),
                                        (float)(k + semiMinorAxis * Math.Sin(t))));
            }

            // then add the first vector again so it's a complete loop
            s._vertices.Add(position + new Vector2((float)(h + semiMajorAxis * Math.Cos(step)),
                                    (float)(k + semiMinorAxis * Math.Sin(step))));

            // now rotate it as necessary
            Matrix m = Matrix.CreateRotationZ(angleOffset);
            for (int i = 0; i < s._vertices.Length; i++)
            {
                s._vertices[i] = Vector2.Transform((Vector2)s._vertices[i], m);
            }

            return s;
        }
Example #2
0
        // Taken from http://forums.create.msdn.com/forums/t/7414.aspx?PageIndex=1
        public static Shape CreateCircle(Vector2 position, float radius, int sides)
        {
            Shape s = new Shape();

            float max = 2 * (float)Math.PI;
            float step = max / (float)sides;

            for (float theta = 0; theta < max; theta += step)
            {
                s._vertices.Add(position + new Vector2(radius * (float)Math.Cos((double)theta),
                    radius * (float)Math.Sin((double)theta)));
            }

            // then add the first vector again so it's a complete loop
            s._vertices.Add(position + new Vector2(radius * (float)Math.Cos(0),
                    radius * (float)Math.Sin(0)));

            return s;
        }
        public void DrawShape(Shape shape, Color color, float thickness)
        {
            if (shape.vertices.vertices == null) { return; }
            Vector2 v1, v2, position, origin, scale;
            float distance, angle;

            for (int i = shape.vertices.Length - 1; i >= 1; --i)
            {
                v1 = shape.vertices[i - 1];
                v2 = shape.vertices[i];
                distance = Vector2.Distance(v1, v2);
                angle = (float)Math.Atan2((double)(v2.Y - v1.Y),
                                           (double)(v2.X - v1.X));
                //origin = Vector2.One * 0.5f;
                //position = v1 + 0.5f * (v2 - v1);
                scale = new Vector2(distance, thickness);

                _batch.Draw(_pixelTexture, v1, null, color, angle, new Vector2(0, 0.5f), scale, SpriteEffects.None, 0);
            }
        }
Example #4
0
        public static Shape CreateLine(Vector2 start, Vector2 end)
        {
            Shape s = new Shape();

            s._vertices.Add(start);
            s._vertices.Add(end);

            return s;
        }
Example #5
0
        public static Shape CreateTriangle(Vector2 a, Vector2 b, Vector2 c)
        {
            Shape s = new Shape();

            s._vertices.Add(a);
            s._vertices.Add(b);
            s._vertices.Add(c);
            s._vertices.Add(a);

            return s;
        }
Example #6
0
        public static Shape CreateRectangle(AABB aabb)
        {
            Shape s = new Shape();

            s._vertices.Add(aabb.lowerBound);
            s._vertices.Add(new Vector2(aabb.upperBound.X, aabb.lowerBound.Y));

            s._vertices.Add(aabb.upperBound);
            s._vertices.Add(new Vector2(aabb.lowerBound.X, aabb.upperBound.Y));

            s._vertices.Add(aabb.lowerBound);

            return s;
        }
Example #7
0
        public static Shape CreateRectangle(Rectangle r)
        {
            Shape s = new Shape();

            s._vertices.Add(new Vector2(r.Left, r.Top));
            s._vertices.Add(new Vector2(r.Right, r.Top));

            s._vertices.Add(new Vector2(r.Right, r.Bottom));
            s._vertices.Add(new Vector2(r.Left, r.Bottom));

            s._vertices.Add(new Vector2(r.Left, r.Top));

            return s;
        }
Example #8
0
        public static Shape CreateLine(LineSegment segment)
        {
            Shape s = new Shape();

            s._vertices.Add(segment.start);
            s._vertices.Add(segment.end);

            return s;
        }
 public void DrawShape(Shape shape, Color color)
 {
     DrawShape(shape, color, 1);
 }