1

I am using following code snippets to draw circle,in code i generate circle points.I want to draw sphere,is their any logic to generate sphere points?

for(double i=0.0;i<360;i++)
{
double angle=i*system.Math.PI/180;
int x=(int)(80+radius*system.math.cos(angle));
int y=(int)(80+radius*system.math.sin(angle));
putpixel(myGraphics,x,y,color.Red);
}
Prit
  • 25
  • 1
  • 7
  • you will need to paint some radiant drawings also to have a 3d look on 2d plane. .probably a duplicate of [draw circle in Visual C#](http://stackoverflow.com/a/1835096/2115561) – Rohit Prakash Feb 26 '15 at 06:15
  • Yes it is right that above code creates circle in 2d,my question is how to draw sphere(3d) in 2d – Prit Feb 26 '15 at 06:20
  • 1
    Only thing you can do to draw a sphere in 2d plane is adding shadow effect to a circle. – Bhaskar Feb 26 '15 at 07:34
  • @L16H7,how? I don't have an idea of shadow effect. – Prit Feb 26 '15 at 08:09
  • You need to know & describe just what you want: A __sphere always looks like a circle__ whether it is in 3d or projected to 2d. - So what do you want it to look? Add a few links to images that show what you want! [Here is a solution](http://stackoverflow.com/questions/3519835/c-sharp-radial-gradient-brush-effect-in-gdi-and-winforms/13623715?s=1|5.0218#13623715) that fills a circle with a radial gradient brush. – TaW Feb 26 '15 at 08:26
  • Use gradient colour as answered by TaW and if you are imagining sphere on a plane, then you add shadow (gray coloured oval shape) behind the circle opposite to the direction of the white spot on the circle (which represents the direction of a light source) on the plane. You have to calculate the shape and size of the shadow first, then draw it. @Prit – Bhaskar Feb 26 '15 at 10:45
  • @ L16H7 see my updated answer for an even nicer shadow! – TaW Feb 26 '15 at 13:37
  • @Prit: Did you resolve your problem? – TaW Mar 09 '15 at 16:22

1 Answers1

4

Expanding on Drew's answer here is an example of a Radial Gradient Brush using a PathGradientBrush with a set of four named colors. To show the sphere in different colors it would be best to create the color array in code..

private void panel1_Paint(object sender, PaintEventArgs e)
{

    Rectangle bounds = panel1.ClientRectangle;
    using (var ellipsePath = new GraphicsPath())
    {
        ellipsePath.AddEllipse(bounds);
        using (var brush = new PathGradientBrush(ellipsePath))
        {
            // set the highlight point:
            brush.CenterPoint = new PointF(bounds.Width/3f, bounds.Height/4f);
            ColorBlend cb = new ColorBlend(4);
            cb.Colors = new Color[] 
               {  Color.DarkRed, Color.Firebrick, Color.IndianRed, Color.PeachPuff };
            cb.Positions = new float[] { 0f, 0.3f, 0.6f, 1f };
            brush.InterpolationColors = cb;
            brush.FocusScales = new PointF(0, 0);

            e.Graphics.FillRectangle(brush, bounds);
        }
    }

}

This results in a circle that looks like a 3d-sphere..

Update: In fact the (basically) same code that draws the sphere can be used to draw a nice shadow. Here are the changes: choose a location and size that suits your needs, use an array of 3 colors and use shades of black with alpha values of maybe: 0,96,192 with positions of 0f, 0.3f, 1f . This way the shadow will be semitransparent all over and fading out at the edges.. I add an example with a wooden background so you can see how the shadow blends..

enter image description hereenter image description here

Community
  • 1
  • 1
TaW
  • 53,122
  • 8
  • 69
  • 111