-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTriangle.cs
103 lines (96 loc) · 3 KB
/
Triangle.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// PascalsTriangle, by Carl Walsh Sept 2010
using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
namespace Pascals
{
class Triangle
{
List<int[]> rows;
int MOD;
int ROWS;
public Triangle(int numRows, int mod)
{
ROWS = numRows;
MOD = mod;
rows = new List<int[]>();
for(int i = 0; i < numRows; ++i)
{
rows.Add(new int[i+1]);
if(i == 0)
rows[i][0] = 1;
else
for(int n = 0; n <= i; n++)
rows[i][n] = GetSum(i,n, mod);
}
}
int GetSum(int i, int n, int mod)
{
int[] row = rows[i-1];
int left = 0;
if(n != 0)
left = row[n-1];
int right = 0;
if(n < row.Count())
right = row[n];
return (right + left) % mod;
}
public Bitmap Image()
{
//There will be a square dot for each number, of dynamically chosen size
int DOT_SIZE = 625 / ROWS; //the pixel dimension of a dot
if (DOT_SIZE > 50)
DOT_SIZE = 50;
if (DOT_SIZE < 1)
DOT_SIZE = 1;
Bitmap ans = new Bitmap(ROWS * DOT_SIZE, ROWS * DOT_SIZE);
var graphics = Graphics.FromImage(ans);
graphics.Clear(Color.White);
//0 is black, and the other colors are rainbow
Color[] color = new Color[MOD];
color[0] = Color.Black;
for (int i = 1; i < MOD; ++i)
color[i] = ColorHandler.HSVtoColor(255 * (i - 1) / MOD, 255, 255);
//TODO get rid of
for (int r = 0; r < ROWS; ++r)
for (int c = 0; c <= r; ++c)
ChangeDot(c, r, ROWS, color[rows[r][c]], DOT_SIZE, ans);
return ans;
}
void ChangeDot(int column, int row, int ROWS,
Color c, int dotSize, Bitmap img)
{
int offset = dotSize * (ROWS - row - 1) / 2;
Point p = new Point(offset + column * dotSize, row * dotSize);
int x = 0;
while (x < dotSize)
{
p.Y = row * dotSize;
int y = 0;
while (y < dotSize)
{
img.SetPixel(p.X, p.Y, c);
++y;
++p.Y;
}
++x;
++p.X;
}
}
public void Print()
{
for (int i = 0; i < rows.Count; ++i)
{
for (int j = 1; j < rows.Count - i; ++j)
Console.Write(' ');
for (int n = 0; n < rows[i].Count(); ++n)
{
Console.Write(rows[i][n]);
Console.Write(' ');
}
Console.WriteLine();
}
}
}
}