Skip to content

Instantly share code, notes, and snippets.

@Experiment5X
Created November 29, 2012 02:26
Show Gist options
  • Save Experiment5X/4166386 to your computer and use it in GitHub Desktop.
Save Experiment5X/4166386 to your computer and use it in GitHub Desktop.
Sick Drawing Stuff
using System;
using System.Drawing;
using System.Windows.Forms;
namespace DrawingStuff
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
comboBox1.SelectedIndex = 0;
}
Random rand = new Random();
private void button1_Click(object sender, EventArgs e)
{
Graphics g = panel1.CreateGraphics();
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
if (comboBox1.SelectedIndex == 0)
DrawLineFlower(g, new Point((int)numericUpDown3.Value, (int)numericUpDown4.Value), (int)numericUpDown1.Value, (int)numericUpDown2.Value);
else
DrawColorFlower(g, new Point((int)numericUpDown3.Value, (int)numericUpDown4.Value), (int)numericUpDown1.Value, (int)numericUpDown2.Value);
}
void DrawLineFlower(Graphics g, Point start, int petals, int length = 50)
{
double offset = Math.PI * (rand.Next(0, 200) / 100f);
for (int i = 1; i <= petals; i++)
DrawLine(g, start, length, (i * (Math.PI * 2) + offset) / petals);
}
void DrawColorFlower(Graphics g, Point start, int petals, int length = 50)
{
double offset = Math.PI * (rand.Next(0, 200) / 100f);
for (int i = 1; i <= petals; i++)
DrawTriangle(g, start, length, (i * (Math.PI * 2) + offset) / petals, (2 * Math.PI) / petals, ((i & 1) == 1) ? button3.BackColor : button4.BackColor);
}
void DrawTriangle(Graphics g, Point start, int length, double angleA, double angleB, Color c)
{
Point[] points = { start, GetEndPoint(start, length, angleA), GetEndPoint(start, length, angleA + angleB) };
Brush b = new SolidBrush(c);
g.FillPolygon(b, points);
}
void DrawLine(Graphics g, Point start, int length, double angle)
{
Pen p = new Pen(Color.Black);
g.DrawLine(p, start, GetEndPoint(start, length, angle));
}
Point GetEndPoint(Point start, int length, double angle)
{
return new Point((int)(start.X + length * Math.Cos(angle)), (int)(start.Y + length * Math.Sin(angle)));
}
void DrawLine(Graphics g, Point start, int length, int angle)
{
DrawLine(g, start, length, DegreeToRadians(angle));
}
double DegreeToRadians(int degree)
{
return degree * (Math.PI / 180);
}
private void button2_Click(object sender, EventArgs e)
{
Graphics g = panel1.CreateGraphics();
g.Clear(panel1.BackColor);
}
private void panel1_Click(object sender, EventArgs e)
{
Point cursorPos = panel1.PointToClient(Cursor.Position);
Graphics g = panel1.CreateGraphics();
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
if (comboBox1.SelectedIndex == 0)
DrawLineFlower(g, cursorPos, (int)numericUpDown1.Value, (int)numericUpDown2.Value);
else
DrawColorFlower(g, cursorPos, (int)numericUpDown1.Value, (int)numericUpDown2.Value); ;
}
private void button3_Click(object sender, EventArgs e)
{
ColorDialog dialog = new ColorDialog();
dialog.ShowDialog();
button3.BackColor = dialog.Color;
}
private void button4_Click(object sender, EventArgs e)
{
ColorDialog dialog = new ColorDialog();
dialog.ShowDialog();
button4.BackColor = dialog.Color;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment