2014-08-28 2 views
1

win 형태 C#에서 Venn 다이어그램을 만들어야합니다. 나는 Graphics.DrawEllipse와 FillElipse를 사용하여 그것을하려고 노력해왔다. 그러나 공통 부분을 어떻게 채울 수 있을지 확신하지 못합니다. 여기 C# win에서 Venn 다이어그램 만들기 양식

enter image description here

이 내 코드입니다

내가 이것을 달성하고자,

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

}

나는 두 개의 타원 그리기와 다른 색상을 가질 필요가 있어요 공통 부분. 라이브러리를 사용할 수 없습니다. 도와주세요.

+0

사이드 참고 : 모든 브러시와 펜을 처분하거나 다시 사용하는 대신 지역 변수의 필드로를 설정하십시오. –

답변

2

GraphicPath을 사용하려면 System.Drawing.Drawing2D; 네임 스페이스를 추가 할 수 있습니다. 그래픽 경로를 만들고 교차 영역을 가져옵니다.

이 코드를 사용해보십시오 : (I 테스트 목적으로 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); 
    } 

} 

출력 :

enter image description here

+0

잘 작동합니다 ... 이와 관련된 질문이 하나 더 있는데, 저는 또한 venn 다이어그램의 왼쪽/오른쪽 및 공통 부분에서 마우스 클릭을 처리하고 싶습니다. 마우스 위치는 어떻게 저장합니까? – user1821499

3

당신이 반투명 색상을 사용할 수있는 색상의 있도록 겹치는 부분은 두 원의 실제 합성 색상입니다.

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

enter image description here

+0

누군가이 지역의 마우스 클릭을 처리하는 방법에 대해 조언 해 줄 수 있습니까? 왼쪽/오른쪽/공통? – user1821499

관련 문제