簡體   English   中英

如何使用C#在2D平面中繪制球體?

[英]how to draw sphere in 2d plane using c#?

我正在使用以下代碼片段繪制圓,在代碼中我生成圓點。我要繪制球體,它們生成球體點的邏輯嗎?

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);
}

在此擴展Drew的答案的示例是使用帶有一組四種命名顏色的PathGradientBrush的徑向漸變畫筆的示例。 要以不同的顏色顯示球體,最好在代碼中創建顏色數組。

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);
        }
    }

}

這將導致一個看起來像3d球形的圓。

更新 :實際上,繪制球體的(基本上)相同的代碼可用於繪制漂亮的陰影。 進行以下更改:選擇適合您需要的位置和大小,使用3種顏色的數組,並使用黑色陰影,其alpha值可能為:0,96,192,位置為0f,0.3f,1f。 這樣,陰影將遍歷半透明並在邊緣逐漸消失。.我添加了一個帶有木制背景的示例,以便您可以看到陰影是如何融合的。

在此處輸入圖片說明在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM