AceInfinity
Emeritus, Contributor
Images:
Video:
edit: New version (April 19, 2013)
Source:
*Code from old version - new version on CodePlex.
PascalsTriangle.cs
Form1.cs
New version: https://pascalstriangle.codeplex.com/
Enjoy
:thumbsup2:
Video:
edit: New version (April 19, 2013)
Source:
*Code from old version - new version on CodePlex.
PascalsTriangle.cs
Code:
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections.Generic;
using System.Windows.Forms;
namespace PascalsTriangleDisplay.UI
{
sealed class PascalsTriangle : PictureBox
{
public PascalsTriangle()
{
DoubleBuffered = true;
Size = new Size(100, 100);
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
BackColor = Color.Transparent;
}
public int Base { get; set; }
private int _rad = 2;
public int Rad
{
get { return _rad; }
set { _rad = value; }
}
public Color DefaultBaseColor { get; set; }
public Dictionary<int, Color> BaseColors { get; set; }
private const decimal Sqrt2 = 1.4142135623730950488016887242m;
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
int gridTiles = Width / Rad;
decimal dy = Sqrt2 * Rad;
int[] previousColors = new int[] { };
for (int i = 0; i < gridTiles / 2 - Rad; i++)
{
int[] rowColors = new int[i + 1];
for (int j = 0; j <= i; j++)
{
int colorIndex;
if (j == 0 || j == i)
{
colorIndex = 1;
}
else
{
int leftColor = previousColors[j - 1];
int rightColor = previousColors[j];
colorIndex = (leftColor + rightColor) % Base;
}
Color fillColor = BaseColors.ContainsKey(colorIndex) ? BaseColors[colorIndex] : DefaultBaseColor;
DrawElipse(e.Graphics, fillColor, new Point((Width / 2) + Rad * (2 * j - i), (int)(dy * i * 1.415m)), Rad * 2);
rowColors[j] = colorIndex;
}
previousColors = rowColors;
}
}
private int _prevSize;
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
if (Height != _prevSize)
{
_prevSize = Width = Height;
}
else
{
_prevSize = Height = Width;
}
Invalidate();
}
private static void DrawElipse(Graphics g, Color fill, Point location, int diameter)
{
Size s = new Size(diameter, diameter);
g.DrawEllipse(Pens.Black, new Rectangle(location, s));
using (SolidBrush sb = new SolidBrush(Color.FromArgb(200, fill)))
g.FillEllipse(sb, new Rectangle(location, s));
}
}
}
Form1.cs
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
namespace PascalsTriangleDisplay
{
public sealed partial class Form1 : Form
{
private int _colorCount;
private readonly BindingList<Color> _baseColors = new BindingList<Color>();
// Constructor
public Form1()
{
InitializeComponent();
DoubleBuffered = true;
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
_baseColors.ListChanged += _baseColors_ListChanged;
nudBase.Value = 3;
nudRad.Value = 3;
}
// Base Colors Changed
void _baseColors_ListChanged(object sender, ListChangedEventArgs e)
{
colorListView1.Items.Clear();
Dictionary<int, Color> colors = new Dictionary<int, Color>();
colorListView1.BeginUpdate();
for (int i = 0; i < _baseColors.Count; i++)
{
Color baseColor = _baseColors[i];
colors.Add(i, baseColor);
ListViewItem lvi = new ListViewItem(new[] { i.ToString(CultureInfo.InvariantCulture), string.Format("RGB: {0}, {1}, {2}", baseColor.R, baseColor.G, baseColor.B) });
lvi.SubItems[1].BackColor = baseColor;
colorListView1.Items.Add(lvi);
}
colorListView1.EndUpdate();
pascalsTriangle1.BaseColors = colors;
_colorCount = pascalsTriangle1.Base;
}
// Update
private void btnUpdate_Click(object sender, EventArgs e)
{
pascalsTriangle1.Invalidate();
}
// Value Changes
private void nudBase_ValueChanged(object sender, EventArgs e)
{
pascalsTriangle1.Base = (int)nudBase.Value;
if (_colorCount < pascalsTriangle1.Base)
{
for (int i = _colorCount; i < pascalsTriangle1.Base; i++)
{
_baseColors.Add(Color.Black);
}
}
else
{
for (int i = 0; i < _colorCount - pascalsTriangle1.Base; i++)
{
_baseColors.RemoveAt(_baseColors.Count - 1);
}
}
}
private void nudRad_ValueChanged(object sender, EventArgs e)
{
pascalsTriangle1.Rad = (int)nudRad.Value;
}
// Update Base Color
private void colorListView1_DoubleClick(object sender, EventArgs e)
{
if (colorListView1.SelectedItems.Count > 0)
{
using (ColorDialog cd = new ColorDialog())
{
cd.AnyColor = true;
cd.FullOpen = true;
if (cd.ShowDialog() == DialogResult.OK)
{
_baseColors[colorListView1.SelectedIndices[0]] = cd.Color;
}
}
}
}
}
}
New version: https://pascalstriangle.codeplex.com/
Enjoy
:thumbsup2:
Last edited: