コード例 #1
0
ファイル: Circle.cs プロジェクト: RasterCode/OtterUI
        /// <summary>
        /// Draws the Circle.
        /// </summary>
        /// <param name="graphics"></param>
        public override void Draw(Otter.Interface.Graphics graphics)
        {
            // Do not draw if there are less than 3 segments in the circle
            if(Segments <= 2)
                return;
            
            Triangle[] triangles = new Triangle[Segments * 2];

            double x = 1.0f;
            double y = 0.0f;

            float radius = ((CircleLayout)Layout).Radius;
            uint halfWidth = ((CircleLayout)Layout).Width / 2;
            Color color = ((CircleLayout)Layout).Color;

            PointF center = new PointF(Layout.Size.Width / 2.0f, Layout.Size.Height / 2.0f);

            // For each segment create two triangles
            for (int i = 0; i < Segments; i++)
            {
                double angle1 = (i / (double)Segments) * Math.PI * 2;
                double angle2 = ((i + 1) / (double)Segments) * Math.PI * 2;

                float rx1 = (float)(x * Math.Cos(angle1) - y * Math.Sin(angle1));
                float ry1 = (float)(x * Math.Sin(angle1) + y * Math.Cos(angle1));

                float rx2 = (float)(x * Math.Cos(angle2) - y * Math.Sin(angle2));
                float ry2 = (float)(x * Math.Sin(angle2) + y * Math.Cos(angle2));

                triangles[i * 2 + 0] = new Triangle();
                triangles[i * 2 + 0].SetVertex(0, center.X + rx1 * (radius + halfWidth), center.Y + ry1 * (radius + halfWidth), 0.0f, 0.0f, 0.0f, color.ToArgb());
                triangles[i * 2 + 0].SetVertex(1, center.X + rx2 * (radius + halfWidth), center.Y + ry2 * (radius + halfWidth), 0.0f, 0.0f, 0.0f, color.ToArgb());
                triangles[i * 2 + 0].SetVertex(2, center.X + rx1 * (radius - halfWidth), center.Y + ry1 * (radius - halfWidth), 0.0f, 0.0f, 0.0f, color.ToArgb());

                triangles[i * 2 + 1] = new Triangle();
                triangles[i * 2 + 1].SetVertex(0, center.X + rx1 * (radius - halfWidth), center.Y + ry1 * (radius - halfWidth), 0.0f, 0.0f, 0.0f, color.ToArgb());
                triangles[i * 2 + 1].SetVertex(1, center.X + rx2 * (radius + halfWidth), center.Y + ry2 * (radius + halfWidth), 0.0f, 0.0f, 0.0f, color.ToArgb());
                triangles[i * 2 + 1].SetVertex(2, center.X + rx2 * (radius - halfWidth), center.Y + ry2 * (radius - halfWidth), 0.0f, 0.0f, 0.0f, color.ToArgb());
            }

            graphics.DrawTriangles(-1, triangles);
        }