I need to create a Venn diagram in win form C#. I have been trying to do it using Graphics.DrawEllipse and 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;
namespace to use GraphicPath
. Create a graphic path and get the intersection region.
Try this code: (I have commented DrawString
for testing purpose)
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 :
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.