簡體   English   中英

在C#win窗體中創建維恩圖

[英]Creating Venn diagram in C# win Form

我需要以win形式C#創建維恩圖。 我一直在嘗試使用Graphics.DrawEllipse和FillElipse。 但是我不確定如何填寫共同部分。

我想實現這個目標

在此處輸入圖片說明

這是我的代碼,

private void panelControlVennDiagram_Paint(object sender, PaintEventArgs e)
{
  Brush brushLeft = new SolidBrush(Color.Blue);
  Brush brushRight = new SolidBrush(Color.LightPink);
  Brush brushCommon = new SolidBrush(Color.Purple);

  Pen pen = new Pen(brushLeft, 10);

  Rectangle leftVenn = new Rectangle(20, 50,100,100);
  Rectangle rightVenn = new Rectangle(90, 50, 100, 100);
  Rectangle commonVenn = new Rectangle(100, 120, 100, 100);

  Font stringFont = new Font("Times New Roman", 9);

  e.Graphics.DrawString("Left:" + leftValue, stringFont, brushLeft, 10,70);
  e.Graphics.DrawString("Right:" + rightValue, stringFont, brushRight, 90,70);
  e.Graphics.DrawString("Common:" + commonValue,stringFont, brushCommon, 100,70);

  // Fill ellipse on screen.
  e.Graphics.FillEllipse(brushLeft, leftVenn);
  e.Graphics.FillEllipse(brushRight, rightVenn);

  e.Graphics.DrawEllipse(Pens.White, leftVenn);
  e.Graphics.DrawEllipse(Pens.White, rightVenn);

}

我要繪制兩個橢圓,並且公共部分需要使用不同的顏色。 我不能使用任何圖書館。 請幫忙。

您可以使用半透明顏色,以便重疊部分的顏色是兩個圓圈的實際合成顏色

Brush brushLeft = new SolidBrush(Color.FromArgb(50, Color.Blue));
Brush brushRight = new SolidBrush(Color.FromArgb(50, Color.Red));

在此處輸入圖片說明

您可以添加System.Drawing.Drawing2D; 使用GraphicPath名稱空間。 創建一個圖形路徑並獲取相交區域。

嘗試以下代碼:(出於測試目的,我已注釋了DrawString

private void panelControlVennDiagram_Paint(object sender, PaintEventArgs e)
{
    Rectangle leftVenn = new Rectangle(20, 50, 100, 100);
    Rectangle rightVenn = new Rectangle(90, 50, 100, 100);          
    Region region1 = new Region();

    //Font stringFont = new Font("Times New Roman", 9);
    //e.Graphics.DrawString("Left:" , stringFont, brushLeft, 10, 70);
    //e.Graphics.DrawString("Right:" , stringFont, brushRight, 90, 70);
    //e.Graphics.DrawString("Common:", stringFont, brushCommon, 100, 70);

    // Fill ellipse on screen.
    using(Brush brushLeft = new SolidBrush(Color.Blue))
    {    
        e.Graphics.FillEllipse(brushLeft, leftVenn);
        e.Graphics.DrawEllipse(Pens.White, leftVenn);
    }

    using(Brush brushRight = new SolidBrush(Color.LightPink))
    {
        e.Graphics.FillEllipse(brushRight, rightVenn);        
        e.Graphics.DrawEllipse(Pens.White, rightVenn);
    } 

    using (GraphicsPath circle_path = new GraphicsPath())
    {
        circle_path.AddEllipse(leftVenn);
        region1.Intersect(circle_path);
    }

    using (GraphicsPath circle_path = new GraphicsPath())
    {
        circle_path.AddEllipse(rightVenn);
        region1.Intersect(circle_path);
    }

    using(Brush brushCommon = new SolidBrush(Color.Purple))
    {  
        e.Graphics.FillRegion(brushCommon, region1);
    }

}

輸出

在此處輸入圖片說明

暫無
暫無

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

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