CSsharp Example:
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace CirclePack
{
public partial class CirclePackForm : Form
{
private int _circleCount;
private double _radius;
private double[] _xValues;
private double[] _yValues;
private int _xRect = 150;
private int _yRect = 50;
private int _rectSize = 400;
public CirclePackForm()
{
InitializeComponent();
ClearSolutionValues();
}
void ClearSolutionValues()
{
_circleCount = 5;
_radius = 0.0;
_xValues = new double[0];
_yValues = new double[0];
}
private void DrawCircles(int circleCount, double radius, double[] xValues, double[] yValues, Graphics graph)
{
double size = 200;
float xi, yi;
float rad = (float)(2 * size * radius);
Pen blackPen = new Pen(Color.Black, 2);
graph.DrawRectangle(blackPen, _xRect, _yRect, _rectSize, _rectSize);
blackPen.Dispose();
graph.SmoothingMode = SmoothingMode.AntiAlias;
blackPen = new Pen(Color.Black, 1);
Brush redBrush = new SolidBrush(Color.PaleVioletRed);
for (int i = 0; i < circleCount; i++)
{
xi = (float)(_xRect + size * (1.0 + xValues[i] - radius));
yi = (float)(_yRect + size * (1.0 + yValues[i] - radius));
graph.FillEllipse(redBrush, xi, yi, rad, rad);
graph.DrawEllipse(blackPen, xi, yi, rad, rad);
}
blackPen.Dispose();
redBrush.Dispose();
}
private void CirclePackForm_Paint(object sender, PaintEventArgs e)
{
Graphics graph = e.Graphics;
if ((_circleCount > 0) && (_radius != 0.0))
DrawCircles(_circleCount, _radius, _xValues, _yValues, graph);
else
{
Brush grayBrush = new SolidBrush(Color.Gray);
graph.FillRectangle(grayBrush, _xRect, _yRect, _rectSize, _rectSize);
grayBrush.Dispose();
}
graph.Dispose();
}
private void btnSolve_Click(object sender, EventArgs e)
{
ClearSolutionValues();
if (txtCircles.Text.Length > 0)
{
_circleCount = Convert.ToInt32(txtCircles.Text);
}
MplCirclePack cpMpl = new MplCirclePack();
bool result = cpMpl.SolveModel("LGO", _circleCount);
if (!result) {
MessageBox.Show("There is no solution available with this input.\n" +
"The solver returned the following message: \n\n" +
cpMpl.ResultString);
}
_radius = cpMpl.Radius;
_xValues = cpMpl.XValues;
_yValues = cpMpl.YValues;
this.Refresh();
}
private void btnClear_Click(object sender, EventArgs e)
{
ClearSolutionValues();
this.Refresh();
}
}
}