简体   繁体   English

在C#win窗体中创建维恩图

[英]Creating Venn diagram in C# win Form

I need to create a Venn diagram in win form C#. 我需要以win形式C#创建维恩图。 I have been trying to do it using Graphics.DrawEllipse and FillElipse. 我一直在尝试使用Graphics.DrawEllipse和FillElipse。 But I am not sure how do I fill in the common part. 但是我不确定如何填写共同部分。

I want to achieve this, 我想实现这个目标

在此处输入图片说明

Here is my code for this, 这是我的代码,

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

} }

I am drawing two ellipses and need to have a different color for the common part. 我要绘制两个椭圆,并且公共部分需要使用不同的颜色。 I can not use any library. 我不能使用任何图书馆。 Please help. 请帮忙。

you could use a semi-transparent color so that the color of the overlapping parts is the actual composite color of the two circles 您可以使用半透明颜色,以便重叠部分的颜色是两个圆圈的实际合成颜色

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

在此处输入图片说明

You could add System.Drawing.Drawing2D; 您可以添加System.Drawing.Drawing2D; namespace to use GraphicPath . 使用GraphicPath名称空间。 Create a graphic path and get the intersection region. 创建一个图形路径并获取相交区域。

Try this code: (I have commented DrawString for testing purpose) 尝试以下代码:(出于测试目的,我已注释了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);
    }

}

Output : 输出

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM