Download SliceRectangleSample.zip, last updated 10/02/2013 (23.18 KB)

Download
  • md5: 1e8ccdf10305cc43789e4fa28c96006b
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Windows.Forms;
using Cyotek.Drawing;

namespace SliceRectangleSample
{
  // Cyotek Slice Rectangle Sample
  // Copyright (c) 2013 Cyotek. All Rights Reserved.
  // http://cyotek.com
  // http://cyotek.com/blog/dividing-up-a-rectangle-based-on-pairs-of-points-using-csharp

  // If you use this code in your applications, attribution or donations are welcome.

  internal partial class MainForm : Form
  {
    #region Instance Fields

    private ColorSwatchCollection _colors;

    private Settings _settings;

    #endregion

    #region Constructors

    public MainForm()
    {
      this.InitializeComponent();
    }

    #endregion

    #region Class Members

    private static void DrawCross(Graphics graphics, Point point, Color color)
    {
      using (Pen pen = new Pen(color))
      {
        graphics.DrawLine(pen, point.X - 2, point.Y - 2, point.X + 2, point.Y + 2);
        graphics.DrawLine(pen, point.X - 2, point.Y + 2, point.X + 2, point.Y - 2);
      }
    }

    #endregion

    #region Overridden Members

    /// <summary>
    ///   Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
      if (disposing)
      {
        if (_colors != null)
          _colors.Dispose();

        if (components != null)
          components.Dispose();
      }

      base.Dispose(disposing);
    }

    protected override void OnLoad(EventArgs e)
    {
      _colors = new ColorSwatchCollection(ColorSwatchCollection.Basic);
      _colors.CreateBrushes();

      _settings = Samples.Sample1;

      base.OnLoad(e);

      this.UpdateDisplay();
    }

    #endregion

    #region Members

    private void ListPoints()
    {
      pointsTextBox.Text = string.Join(Environment.NewLine, _settings.GetPoints().Select(p => string.Format("{0}, {1}\t[{2}]", p.Location.X, p.Location.Y, p.Connections)));
    }

    private void ListRectangles()
    {
      rectanglesTextBox.Text = string.Join(Environment.NewLine, _settings.GetRectangles().Select(r => string.Format("{0}, {1}, {2}, {3}", r.Left, r.Top, r.Width, r.Height)));
    }

    private void ListSegments()
    {
      segmentsTextBox.Text = string.Join(Environment.NewLine, _settings.Segments.Select(s => string.Format("{0}, {1} - {2}, {3}", s.Location.X, s.Location.Y, s.EndLocation.X, s.EndLocation.Y)));
    }

    private void UpdateDisplay()
    {
      this.ListSegments();
      this.ListPoints();
      this.ListRectangles();
      previewSplitContainer.Panel1.Invalidate();
      previewSplitContainer.Panel2.Invalidate();
    }

    #endregion

    #region Event Handlers

    private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
    {
      using (AboutDialog dialog = new AboutDialog())
        dialog.ShowDialog(this);
    }

    private void exitToolStripMenuItem_Click(object sender, EventArgs e)
    {
      this.Close();
    }

    private void openToolStripMenuItem_Click(object sender, EventArgs e)
    {
      using (FileDialog dialog = new OpenFileDialog())
      {
        dialog.Filter = "Text Files (*.txt)|*.txt";
        dialog.DefaultExt = "txt";
        dialog.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;

        if (dialog.ShowDialog(this) == DialogResult.OK)
        {
          _settings.Load(dialog.FileName);
          this.UpdateDisplay();
        }
      }
    }

    private void previewSplitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
    {
      float scaleX;
      float scaleY;

      _colors.Reset();

      scaleX = previewSplitContainer.Panel1.Width / (float)_settings.Size.Width;
      scaleY = previewSplitContainer.Panel1.Height / (float)_settings.Size.Height;

      e.Graphics.ScaleTransform(scaleX, scaleY);
      e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

      e.Graphics.FillRectangle(Brushes.AntiqueWhite, new Rectangle(Point.Empty, _settings.Size));

      foreach (Segment segment in _settings.Segments)
      {
        using (Pen pen = new Pen(Color.FromArgb(128, _colors.Next()), 1) { DashStyle = DashStyle.Dot })
        {
          e.Graphics.DrawLine(pen, segment.Location, segment.EndLocation);
          DrawCross(e.Graphics, segment.Location, _colors.Current);
          DrawCross(e.Graphics, segment.EndLocation, _colors.Current);
        }
      }
    }

    private void previewSplitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
    {
      float scaleX;
      float scaleY;

      _colors.Reset();

      scaleX = previewSplitContainer.Panel1.Width / (float)_settings.Size.Width;
      scaleY = previewSplitContainer.Panel1.Height / (float)_settings.Size.Height;

      e.Graphics.ScaleTransform(scaleX, scaleY);

      e.Graphics.FillRectangle(Brushes.AntiqueWhite, new Rectangle(Point.Empty, _settings.Size));

      foreach (Rectangle rectangle in _settings.GetRectangles())
        e.Graphics.FillRectangle(_colors.NextBrush(), rectangle);
    }

    private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
    {
      using (FileDialog dialog = new SaveFileDialog())
      {
        dialog.Filter = "Text Files (*.txt)|*.txt";
        dialog.DefaultExt = "txt";
        dialog.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;

        if (dialog.ShowDialog(this) == DialogResult.OK)
          _settings.Save(dialog.FileName);
      }
    }

    #endregion
  }
}

Donate

Donate