0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C#リージョン

Posted at
Form1.cs
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace region
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var paths = new GraphicsPath[3];
            var r = 60;
            var s = 80;
            for (var i = 0; i < paths.Length; i++)
            {
                var rad = (Math.PI / 180) * (120 * i + 30);
                var x = (float)(150 + Math.Cos(rad) * r);
                var y = (float)(150 + Math.Sin(rad) * r);

                var path = new GraphicsPath();
                path.AddEllipse(x - s, y - s, 2 * s, 2 * s);
                paths[i] = path;
            }

            {
                var rgn1 = new Region(paths[0]);
                rgn1.Exclude(paths[1]);
                rgn1.Exclude(paths[2]);
                var rgn2 = new Region(paths[1]);
                rgn2.Exclude(paths[2]);
                rgn2.Exclude(paths[0]);
                var rgn4 = new Region(paths[2]);
                rgn4.Exclude(paths[0]);
                rgn4.Exclude(paths[1]);
                var rgn7 = new Region(paths[0]);
                rgn7.Intersect(paths[1]);
                rgn7.Intersect(paths[2]);

                var pic = pictureBox1;
                pic.Image = new Bitmap(pic.Width, pic.Height);
                var g = Graphics.FromImage(pic.Image);
                g.Clear(Color.FromArgb(0x40, 0x40, 0x40));

                g.FillRegion(new SolidBrush(Color.FromArgb(0x00, 0x00, 0xff)), rgn1);
                g.FillRegion(new SolidBrush(Color.FromArgb(0xff, 0x00, 0x00)), rgn2);
                g.FillRegion(new SolidBrush(Color.FromArgb(0x00, 0xff, 0x00)), rgn4);
                g.FillRegion(new SolidBrush(Color.FromArgb(0xff, 0xff, 0xff)), rgn7);
            }

            {
                var rgn = new Region(paths[0]);
                rgn.Xor(paths[1]);
                rgn.Xor(paths[2]);

                var pic = pictureBox2;
                pic.Image = new Bitmap(pic.Width, pic.Height);
                var g = Graphics.FromImage(pic.Image);
                g.Clear(Color.FromArgb(0x40, 0x40, 0x40));

                g.FillRegion(new SolidBrush(Color.FromArgb(0xff, 0x80, 0x00)), rgn);
            }
        }
    }
}

region.png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?