Lightening Bolts on C#


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace LighteningBoltsCS
{
    public partial class DemoForm : Form
    {
        public DemoForm()
        {
            InitializeComponent();
        }
        private Pen pen;
        private Graphics g;
        private Random random = new Random();

       

        private void DemoForm_Paint(object sender, PaintEventArgs e)
        {
            g = e.Graphics;
            pen = new Pen(Color.White);
            int cnt = 2;
            for (int i = 0; i < cnt && checkBox1.Checked; i++)
                fracture(checkBox1.Left, checkBox1.Bottom, button1.Left, button1.Top, 4);
            for (int i = 0; i < cnt && checkBox2.Checked; i++)
                fracture(checkBox2.Left, checkBox2.Bottom, button1.Left + button1.Width/2, button1.Top, 4);
            for (int i = 0; i < cnt && checkBox3.Checked; i++)
                fracture(checkBox3.Left, checkBox3.Bottom, button1.Right, button1.Top, 4);
        }
        private void line(float x1, float y1, float x2, float y2)
        {
            g.DrawLine(pen, x1, y1, x2, y2);
        }
        private void fracture(double ax, double ay, double bx, double by, int k)
        {
            double bxax = (bx - ax) / 5;
            double byay = (by - ay) / 4;
            double cx = ax + bxax;
            double cy = ay + byay;
            double dx = bx - bxax;
            double dy = by - byay;
            double dsv = Math.Sqrt(bxax * bxax + byay * byay);
            double ndc = Math.Atan2(dy - cy, dx - cx + 0.01);
            double ex, ey;
            if ((random.Next() % 2)==0)
            {
                ex = cx + Math.Cos(-Math.PI / 3 + ndc) * dsv;
                ey = cy + Math.Sin(-Math.PI / 3 + ndc) * dsv;
            }
            else
            {
                ex = cx + Math.Cos(Math.PI / 3 + ndc) * dsv;
                ey = cy + Math.Sin(Math.PI / 3 + ndc) * dsv;
            }
            if (k > 1)
            {
                fracture(ax, ay, cx, cy, k - 1);
                fracture(cx, cy, ex, ey, k - 1);
                fracture(ex, ey, dx, dy, k - 1);
                fracture(dx, dy, bx, by, k - 1);
            }
            else
            {
                line((float)ax, (float)ay, (float)cx, (float)cy);
                line((float)cx, (float)cy, (float)ex, (float)ey);
                line((float)ex, (float)ey, (float)dx, (float)dy);
                line((float)dx, (float)dy, (float)bx, (float)by);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Invalidate();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            checkBox1.Checked = !checkBox1.Checked;
            checkBox2.Checked = !checkBox2.Checked;
            checkBox3.Checked = !checkBox3.Checked;
        }


    }
}

Comments

Anonymous said…
cool! :)

Popular posts from this blog

FarmVille Clicker

Brief history of my life (Part I - from birth 'till university)