double realRadius = radius * Math.Min(xScale, yScale);
Point realP1 = canvas.NormalizePoint(p1);
Point realP2 = canvas.NormalizePoint(p2);
var x = realP2.X - realP1.X;
var y = realP2.Y - realP1.Y;
double d = Math.Sqrt(x * x + y * y);
x /= d;
y /= d;
var x1 = x * Math.Cos(angle30) - y * Math.Sin(angle30);
var y1 = x * Math.Sin(angle30) + y * Math.Cos(angle30);
var x2 = x * Math.Cos(-angle30) - y * Math.Sin(-angle30);
var y2 = x * Math.Sin(-angle30) + y * Math.Cos(-angle30);
x = x * realRadius;
y = y * realRadius;
realP1 = new Point(realP1.X + x, realP1.Y + y);
realP2 = new Point(realP2.X - x, realP2.Y - y);
canvas.Graphics.DrawLine(border, (int)realP1.X, (int)realP1.Y, (int)realP2.X, (int)realP2.Y);
canvas.Graphics.DrawLine(border, (int)(realP2.X - x1 * realRadius), (int)(realP2.Y - y1 * realRadius), (int)realP2.X, (int)realP2.Y);
canvas.Graphics.DrawLine(border, (int)(realP2.X - x2 * realRadius), (int)(realP2.Y - y2 * realRadius), (int)realP2.X, (int)realP2.Y);
}
}
}
using System.Drawing;
using System.Windows.Forms;
using ColoringGraphProblem.Drawing;
using ColoringGraphProblem.Exceptions;
using ColoringGraphProblem.Views.Dialogs;
using ColoringGraphProblem.WorkWithFiles;
namespace ColoringGraphProblem.Views
{
public class GraphCreatingModel
{
//форма
private readonly GraphCreatingForm form;
//содержит в себе граф
private readonly GraphClicker graphClicker;
//Есть ли граф
public bool HasGraph()
{
return graphClicker.HasGraph();
}
//запомнили форму и создали clicker
public GraphCreatingModel(GraphCreatingForm form)
{
this.form = form;
graphClicker = new GraphClicker(form.GetPictureBox());
}
//создать граф
public void CreateGraph(IWin32Window owner)
{
//выдаем диалог ввода числа
var inputIntegerModel = new InputIntegerModel("Введите число...", "Введите количество вершин в графе:");
var nullableValue = inputIntegerModel.GetValue(owner);
if (nullableValue == null) return;
var value = nullableValue.Value;
if (value <= 0)
throw new UserException("Количество вершин в графе должно быть больше нуля. Значение {0} недопустимо.", value);
if (value > 30)
throw new UserException("Ожидается, что количество вершин в графе будет не больше, чем {0}. Значение {1} неприемлемо.", 30, value);
graphClicker.SetGraph(new Graph(value));
}
//зачитываем граф и кладем его в clicker
public void OpenGraph(string filePath)
{
graphClicker.SetGraph(GraphReader.Read(filePath, 30));
}
//Нарисовать граф, если он есть
public void Draw()
{
if (graphClicker.HasGraph())
graphClicker.Draw(new Canvas(form.GetPictureBox()));
}
//Нарисовать граф, если он есть
public void Draw(Graphics graphics)
{
if (graphClicker.HasGraph())
graphClicker.Draw(new Canvas(form.GetPictureBox(), graphics));
}
//пишем граф в файл
public void SaveGraph(string filePath)
{
GraphWriter.Write(filePath, graphClicker.Graph);
}
}
}
using System;
using System.Windows.Forms;
using ColoringGraphProblem.Exceptions;
using ColoringGraphProblem.Views.CommonForms;
namespace ColoringGraphProblem.Views
{
public partial class GraphCreatingForm : Form
{
private GraphCreatingModel model;
public PictureBox GetPictureBox()
{
return pictureBox;
}
public GraphCreatingForm()
{
InitializeComponent();
}
//Вызывается, когда пользователь в меню выбирает соотвествующую кнопку
private void создатьToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
model.CreateGraph(this);
model.Draw();
}
catch (UserException ue)
{
new ErrorMessageModel().ShowMessage(this, ue.Message);
}
}
//Вызывается, когда пользователь в меню выбирает соотвествующую кнопку
private void открытьToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
if (openFileDialog.ShowDialog(this) == DialogResult.OK)
{
if (openFileDialog.CheckFileExists)
{
model.OpenGraph(openFileDialog.FileName);
model.Draw();
}
else throw new UserException("Файл '{0}' не найден.", openFileDialog.FileName);
}
}
catch (UserException ue)
{
new ErrorMessageModel().ShowMessage(this, ue.Message);
}
}
//Вызывается, когда пользователь в меню выбирает соотвествующую кнопку
private void сохранитьToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!model.HasGraph()) return;
try
{
if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
model.SaveGraph(saveFileDialog.FileName);
}
catch (UserException ue)
{
new ErrorMessageModel().ShowMessage(this, ue.Message);
}
}
//Вызывается, когда пользователь в меню выбирает соотвествующую кнопку
private void выйтиToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
//Вызывается, когда меняется размер формы и PictureBox
private void pictureBox_Resize(object sender, EventArgs e)
{
while (model == null) ;
model.Draw();
}
//Вызывается, когда PictureBox'у хочется перерисоваться
private void pictureBox_Paint(object sender, PaintEventArgs e)
{
while (model == null) ;
model.Draw(e.Graphics);
}
//Вызывается, когда произошла первая отрисовка формы
private void GraphCreatingForm_Shown(object sender, EventArgs e)
{
model = new GraphCreatingModel(this);
}
}
}
namespace ColoringGraphProblem.Views
{
partial class GraphCreatingForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <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 && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.файлToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.открытьToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.создатьToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.сохранитьToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripSeparator();
this.выйтиToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
this.menuStrip1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.SuspendLayout();
//
// menuStrip1
//
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.файлToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(427, 24);
this.menuStrip1.TabIndex = 0;
this.menuStrip1.Text = "menuStrip";
//
// файлToolStripMenuItem
//
this.файлToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.открытьToolStripMenuItem,
this.создатьToolStripMenuItem,
this.сохранитьToolStripMenuItem,
this.toolStripMenuItem1,
this.выйтиToolStripMenuItem});
this.файлToolStripMenuItem.Name = "файлToolStripMenuItem";
this.файлToolStripMenuItem.Size = new System.Drawing.Size(48, 20);
this.файлToolStripMenuItem.Text = "Файл";
//
// открытьToolStripMenuItem
//
this.открытьToolStripMenuItem.Name = "открытьToolStripMenuItem";
this.открытьToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.открытьToolStripMenuItem.Text = "Открыть";
this.открытьToolStripMenuItem.Click += new System.EventHandler(this.открытьToolStripMenuItem_Click);
//
// создатьToolStripMenuItem
//
this.создатьToolStripMenuItem.Name = "создатьToolStripMenuItem";
this.создатьToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.создатьToolStripMenuItem.Text = "Создать";
this.создатьToolStripMenuItem.Click += new System.EventHandler(this.создатьToolStripMenuItem_Click);
//
// сохранитьToolStripMenuItem
//
this.сохранитьToolStripMenuItem.Name = "сохранитьToolStripMenuItem";
this.сохранитьToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.сохранитьToolStripMenuItem.Text = "Сохранить";
this.сохранитьToolStripMenuItem.Click += new System.EventHandler(this.сохранитьToolStripMenuItem_Click);
//
// toolStripMenuItem1
//
this.toolStripMenuItem1.Name = "toolStripMenuItem1";
this.toolStripMenuItem1.Size = new System.Drawing.Size(149, 6);
//
// выйтиToolStripMenuItem
//
this.выйтиToolStripMenuItem.Name = "выйтиToolStripMenuItem";
this.выйтиToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.выйтиToolStripMenuItem.Text = "Выйти";
this.выйтиToolStripMenuItem.Click += new System.EventHandler(this.выйтиToolStripMenuItem_Click);
//
// pictureBox
//
this.pictureBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.pictureBox.Location = new System.Drawing.Point(0, 27);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(427, 311);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
this.pictureBox.Resize += new System.EventHandler(this.pictureBox_Resize);
this.pictureBox.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox_Paint);
//
// openFileDialog
//
this.openFileDialog.Filter = "Text files|*.txt|All files|*.*";
//
// saveFileDialog
//
this.saveFileDialog.Filter = "Text files|*.txt|All files|*.*";
//
// GraphCreatingForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(427, 337);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.menuStrip1);
this.MainMenuStrip = this.menuStrip1;
this.Name = "GraphCreatingForm";
this.Text = "Создание или редактирование графа";
this.Shown += new System.EventHandler(this.GraphCreatingForm_Shown);
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem файлToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem создатьToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem открытьToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem сохранитьToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem выйтиToolStripMenuItem;
private System.Windows.Forms.PictureBox pictureBox;
private System.Windows.Forms.OpenFileDialog openFileDialog;
private System.Windows.Forms.SaveFileDialog saveFileDialog;
private System.Windows.Forms.ToolStripSeparator toolStripMenuItem1;
}
}
using System.Drawing;
using System.Windows.Forms;
namespace ColoringGraphProblem.Views.Dialogs
{
public class DialogForm : Form
{
private bool canBeClosed;
private readonly bool defaultValue;
private void MyFormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = !canBeClosed;
}
public void MakeUnsizeable()
{
MaximizeBox = false;
MinimizeBox = false;
MaximumSize = new Size(Width, Height);
MinimumSize = new Size(Width, Height);
}
public DialogForm(bool canBeClosedByUser)
{
FormClosing += MyFormClosing;
defaultValue = canBeClosedByUser;
canBeClosed = defaultValue;
}
public DialogForm() : this(true)
{
}
public void SuperClose()
{
canBeClosed = true;
Close();
canBeClosed = defaultValue;
}
private void InitializeComponent()
{
this.SuspendLayout();
//
// DialogForm
//
this.ClientSize = new System.Drawing.Size(116, 16);
this.Name = "DialogForm";
this.ResumeLayout(false);
}
}
}
namespace ColoringGraphProblem.Views.Dialogs
{
public partial class ErrorMessageForm : DialogForm
{
public ErrorMessageForm()
{
InitializeComponent();
MakeUnsizeable();
}
public void SetErrorMessage(string message)
{
textBox.Text = message;
}
private void textBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar.CompareTo('\r') == 0)
okButton.PerformClick();
}
}
}
namespace ColoringGraphProblem.Views.Dialogs
{
partial class ErrorMessageForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <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 && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.textBox = new System.Windows.Forms.TextBox();
this.okButton = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox
//
this.textBox.Location = new System.Drawing.Point(13, 13);
this.textBox.Multiline = true;
this.textBox.Name = "textBox";