如何涂一定面积 [英] How to paint a certain area

查看:69
本文介绍了如何涂一定面积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#和amp;绘画中的新手.我正在尝试制作一个简单的程序,它具有3个相交的圆(A,B,C).我想做的就是画一个(根据我得到的结果).

例如:如果结果为1,则我要填充黄色边框区域,如果得到4,则我要填充绿色边框区域,依此类推.

我的代码来绘制这些圆圈:

  private void button1_Click(对象发送者,EventArgs e){图形A = this.CreateGraphics();图形B = this.CreateGraphics();图形C = this.CreateGraphics();Pen Bluepen =新笔(Color.Blue,2);笔RedPen =新笔(Color.Red,2);Pen BlackPen =新的Pen(Color.Black,2);A.DrawEllipse(Bluepen,100,100,150,150);B.DrawEllipse(RedPen,195,100,150,150);C.DrawEllipse(BlackPen,145,190,150,150);} 

解决方案

由于您是本主题的新手,我必须告诉您:人们希望这样做要困难得多.

想到三个解决方案:

  • 构造一个 GraphicsPath ,您可以从三个 Arcs 中进行填充.要计算圆弧,您需要具有的矩形以及扫掠角度和起始角度.这将需要一些数学运算..

  • 绘制成 Bitmap 后,您可以填充要着色的区域.这仅适用于可从中提取每个像素当前颜色的位放大器,不适用于绘制到控件上.

  • 最简单的方法仍然有点复杂,但只是轻微地如此

解决方案3(创建区域并填充区域)

您可以使用各种 set操作组合称为

  private void panel1_Paint(对象发送者,PaintEventArgs e){图形g = e.Graphics;g.SmoothingMode = SmoothingMode.AntiAlias;矩形r1 =新矩形(100,100,150,150);矩形r2 =新矩形(195,100,150,150);矩形r3 =新矩形(145,190,150,150);GraphicsPath gp1 = new GraphicsPath();GraphicsPath gp2 = new GraphicsPath();GraphicsPath gp3 = new GraphicsPath();gp1.AddEllipse(r1);gp2.AddEllipse(r2);gp3.AddEllipse(r3);区域r_1 =新区域(gp1);区域r_2 =新区域(gp2);区域r_3 =新区域(gp3);r_1.Intersect(r_2);//只有五个.r_1.Exclude(r_3);//设置支持的操作!g.SetClip(r_1,CombineMode.Replace);g.Clear(Color.Magenta);//填充剩余的区域g.ResetClip();g.DrawEllipse(Pens.Red,r1);g.DrawEllipse(Pens.Blue,r2);g.DrawEllipse(Pens.Green,r3);//最后处理所有Regions和GraphicsPaths!r_1.Dispose();gp1.Dispose();.....} 

请注意,区域操作会更改当前区域;如果要填充更多区域,则需要恢复更改后的区域!

还请注意,我绘制了任何永久图形所属的位置:在 Paint 事件中,并且使用了其 e.Graphics 对象.

GraphicsPaths 作为 Regions 是GDI对象,应将其丢弃!


解决方案1的说明(通过Math创建GraphicsPath)

完全涉及数学.通过做一些假设,可以大大简化任务:假设圆具有相同的大小.同样,我们首先只看两个具有相同y位置的圆.最后,圆圈形成一个对称图形.(顺便说一句,它们没有:红色圆圈的x = 190,绿色圆圈的y = 186,45.)

获得两个交点以及扫掠角度并不是很困难.

接下来,可以使用 Matrix 将两个点围绕整个图形的中心两次旋转120°;参见此帖子

I am new to drawing and paints in c# & I am trying to make a simple program it has 3 intersecting circles (A,B,C). What i want to do is paint a certain (according to result I get).

For example: If I get 1 as a result I want to fill the yellow bordered region, if I get 4 I want to fill green bordered region and so on.

My Code to draw these circles:

private void button1_Click(object sender, EventArgs e)
    {
        Graphics A = this.CreateGraphics();
        Graphics B = this.CreateGraphics();
        Graphics C = this.CreateGraphics();
        Pen Bluepen = new Pen(Color.Blue, 2);
        Pen RedPen = new Pen(Color.Red, 2);
        Pen BlackPen = new Pen(Color.Black, 2);
        A.DrawEllipse(Bluepen,100, 100, 150, 150);
        B.DrawEllipse(RedPen, 195, 100, 150, 150);
        C.DrawEllipse(BlackPen, 145, 190, 150, 150);
    }

解决方案

Since you are new to this topic I have to tell you: This is a lot harder that one would hope for.

Three solutions come to mind:

  • Construct a GraphicsPath you could fill from three Arcs. To calculate the arcs you need the rectangles you have but also the sweeping angle and also the starting angle. This will take quite some math..

  • After having drawn into a Bitmap you could floodfill the area you want to color. This will only work for bitamps from which you can extract the current color of each pixel, not for drawing onto controls..

  • The simplest way it still a bit involved, but only mildly so

Solution 3 (Create a Region and fill it)

You can use all sorts of set operations to combine areas called Regions. And you can construct a Region from a GraphicsPath. And you can construct a GraphicsPath by adding an ellipse. And you can clip the drawing area of a Graphics object to a Region.

Let's try:

private void panel1_Paint(object sender, PaintEventArgs e)
{
    Graphics g = e.Graphics;
    g.SmoothingMode = SmoothingMode.AntiAlias;

    Rectangle r1 = new Rectangle(100, 100, 150, 150);
    Rectangle r2 = new Rectangle(195, 100, 150, 150);
    Rectangle r3 = new Rectangle(145, 190, 150, 150);
    GraphicsPath gp1 = new GraphicsPath();
    GraphicsPath gp2 = new GraphicsPath();
    GraphicsPath gp3 = new GraphicsPath();
    gp1.AddEllipse(r1);
    gp2.AddEllipse(r2);
    gp3.AddEllipse(r3);
    Region r_1 = new Region(gp1);
    Region r_2 = new Region(gp2);
    Region r_3 = new Region(gp3);

    r_1.Intersect(r_2);   // just two of five..
    r_1.Exclude(r_3);     // set operations supported!

    g.SetClip(r_1, CombineMode.Replace);
    g.Clear(Color.Magenta);    // fill the remaining region
    g.ResetClip();

    g.DrawEllipse(Pens.Red, r1);
    g.DrawEllipse(Pens.Blue, r2);
    g.DrawEllipse(Pens.Green, r3);

   // finally dispose of all Regions and GraphicsPaths!!
    r_1.Dispose();
    gp1.Dispose();
    .....

}

Do note that the region operations change the current region; if you want to fill more areas you need to restore the changed region!

Also note that I draw where any persistent drawing belongs: In the Paint event and that I use its e.Graphics object..

GraphicsPaths as Regions are GDI objects and should be disposed off!


Notes on solution 1 (Create a GraphicsPath by Math)

The full math is rather involved. By making a few assumptions the task can be greatly simplified: Let's assume the circles have the same size. Also that we first look at two circles only, with the same y-position. Finally that the circles form a symmetrical figure. (Which btw they don't: the red circle should have x=190 and the green one y=186,45..)

Getting the two intersection points as well as the sweeping angle is not so hard.

Next one can rotate the two points twice around the center of the whole figure by 120° using a Matrix; see here for an example. Now we have six points; we still need the smaller sweeping angle, which is also found with simple math.

Finally we can construct all 12 (!) GraphicsPaths from the 12 arcs and combine them at will.

The good part is that we can both fill and draw those paths. But, the code is rather extensive..


Notes on solution 2 (floodfill)

While you can't floodfill directly on a control you can prepare the result in a bitmap and then display that image on the control with Graphics.DrawImage.

For an example of coding a floodfill see this post!

这篇关于如何涂一定面积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆