2017-05-24 11 views
1

私は正多角形を描き、それらを等分しました。 それはこのようなものだ:enter image description here2色で正多角形を塗ります

が、私はこのような2色でそれを埋めるためにしたい:

enter image description here

どのように私はこれを実装するのですか?ポリゴンを描画する方法 コードは以下の通りです:

Graphics g = e.Graphics; 
    nPoints = CalculateVertices(sides, radius, angle, center); 

    g.DrawPolygon(navypen, nPoints); 
    g.FillPolygon(BlueBrush, nPoints); 
    Point center = new Point(ClientSize.Width/2, ClientSize.Height/2); 

    for(int i = 0; i < sides; i++) { 
     g.DrawLine(new Pen(Color.Navy), center.X, center.Y, nPoints[i].X, nPoints[i].Y); 

    } 

    private PointF[] CalculateVertices(int sides, int radius, float startingAngle, Point center) 
    { 
     if (sides < 3) { 
      sides = 3; 
     } 
      //throw new ArgumentException("Polygon must have 3 sides or more."); 

     List<PointF> points = new List<PointF>(); 
     float step = 360.0f/sides; 

     float angle = startingAngle; //starting angle 
     for (double i = startingAngle; i < startingAngle + 360.0; i += step) //go in a circle 
     { 
      points.Add(DegreesToXY(angle, radius, center)); 
      angle += step; 
     } 

     return points.ToArray(); 
    } 

    private PointF DegreesToXY(float degrees, float radius, Point origin) 
    { 
     PointF xy = new PointF(); 
     double radians = degrees * Math.PI/180.0; 

     xy.X = (int)(Math.Cos(radians) * radius + origin.X); 
     xy.Y = (int)(Math.Sin(-radians) * radius + origin.Y); 

     return xy; 
    } 
+0

これはコンパイルされません: 'ClientSize/2.Width.X、ClientSize/2.Height.Y' - あなたは2つの色をしたい場合は基本的には2 polygoneを必要としています。 - また:あなたは何をターゲットにしています:Winforms、WPF、ASP ..? __Always__あなたの質問に正しくタグを付けてください! - lso:List の配列は避けてください。一緒に働くことは非常によかった! – TaW

+0

@TaWああ、私はそれを間違って入力し、私はWinformsを使用しています(私の質問を編集しました!!)。私は2色のカラー配列を作ったが、使用方法はわからない。 – user19283043

+0

[ExtFloodFill](https://msdn.microsoft.com/en-us/library/windows/desktop/dd162709(v = vs.85).aspx)関数は[PInvoke](http://www.pinvoke.net/default.aspx/gdi32.extfloodfill)を介して機能します。 –

答えて

2

はいくつかの方法がありますが、ほとんどストレートフォワードは、個別に異なる色のポリゴン(三角形)を描画することです。

Assumig色のList<T>

List<Color> colors = new List<Color> { Color.Yellow, Color.Red }; 

あなたはDrawLine呼び出し前にこれを追加することができます。私は%演算子を使用してnPointscolorsの両方を包む方法

using (SolidBrush brush = new SolidBrush(colors[i%2])) 
    g.FillPolygon(brush, new[] { center, nPoints[i], nPoints[(i+1)% sides]}); 

注意!

enter image description here

+0

このアイデアは素晴らしいです!どうもありがとう – user19283043

関連する問題