Saturday, March 24, 2012

C# Simple Square Graphics

Hello again!
This post will explain how we can program this:



























And this:

























So what are we watching in this images?
We see a square, inside a square, inside a square and so on.
before we continue you should know - we only draw straight lines!

So Lets Program!
So the variables we should use are:
The Proportion - Which tell us where the program should place the next vertex.
Number of Times - The Number of times the algorithm will work(which means number of squares).
Everything will become clear in a minute!

So we define our first 4 vertexes:
Point[] vertex = new Point[4];
vertex[0] = new Point(0, 0);
vertex[1] = new Point(this.Width-18, 0);
vertex[2] = new Point(this.Width-18, this.Height-45);            
vertex[3] = new Point(0, this.Height-45);


And than we draw our Square:
formgraphics.DrawLine(pen, vertex[0], vertex[1]);
formgraphics.DrawLine(pen1, vertex[1], vertex[2]);
formgraphics.DrawLine(pen2, vertex[2], vertex[3]);
formgraphics.DrawLine(pen3, vertex[0], vertex[3]);



And lastly we change our vertexes with the given proportion:
vertex[0].X = (int)(p * vertex[0].X + q * vertex[1].X);
vertex[0].Y = (int)(p * vertex[0].Y + q * vertex[1].Y);

vertex[1].X = (int)(p * vertex[1].X + q * vertex[2].X);
vertex[1].Y = (int)(p * vertex[1].Y + q * vertex[2].Y);

vertex[2].X = (int)(p * vertex[2].X + q * vertex[3].X);
vertex[2].Y = (int)(p * vertex[2].Y + q * vertex[3].Y);

vertex[3].X = (int)(p * vertex[3].X + q * temp.X);
vertex[3].Y = (int)(p * vertex[3].Y + q * temp.Y);

When p and q represent the proportion and 1 -  the proportion.

And that is all there is to know, pretty simple!

So here is the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SimpleSquareGraphics
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Visible = false;
            label1.Visible = false;
            int times = int.Parse(textBox1.Text);
            double q = double.Parse(textBox2.Text);
            textBox1.Visible = false;
            textBox2.Visible = false;
            label2.Visible = false;
            label3.Visible = false;

            Pen pen = new Pen(Color.Green, 2);
            Pen pen1 = new Pen(Color.Blue, 2);
            Pen pen2 = new Pen(Color.Red, 2);
            Pen pen3 = new Pen(Color.Yellow , 2);

            Graphics formgraphics = this.CreateGraphics();
            Point[] vertex = new Point[4];
            vertex[0] = new Point(0, 0);
            vertex[1] = new Point(this.Width-18, 0);
            vertex[2] = new Point(this.Width-18, this.Height-45);            
            vertex[3] = new Point(0, this.Height-45);
           
            //proportion
            double p = 1 - q;

            //Drawing Square
            formgraphics.DrawLine(pen, vertex[0], vertex[1]);
            formgraphics.DrawLine(pen1, vertex[1], vertex[2]);
            formgraphics.DrawLine(pen2, vertex[2], vertex[3]);
            formgraphics.DrawLine(pen3, vertex[0], vertex[3]);

            for (int i = 0; i < times; i++)
            {
                Point temp = new Point(vertex[0].X, vertex[0].Y);

                vertex[0].X = (int)(p * vertex[0].X + q * vertex[1].X);
                vertex[0].Y = (int)(p * vertex[0].Y + q * vertex[1].Y);

                vertex[1].X = (int)(p * vertex[1].X + q * vertex[2].X);
                vertex[1].Y = (int)(p * vertex[1].Y + q * vertex[2].Y);

                vertex[2].X = (int)(p * vertex[2].X + q * vertex[3].X);
                vertex[2].Y = (int)(p * vertex[2].Y + q * vertex[3].Y);

                vertex[3].X = (int)(p * vertex[3].X + q * temp.X);
                vertex[3].Y = (int)(p * vertex[3].Y + q * temp.Y);

                formgraphics.DrawLine(pen, vertex[0], vertex[1]);
                formgraphics.DrawLine(pen1, vertex[1], vertex[2]);
                formgraphics.DrawLine(pen2, vertex[2], vertex[3]);
                formgraphics.DrawLine(pen3, vertex[0], vertex[3]);
            }
            pen.Dispose();
            pen1.Dispose();
            pen2.Dispose();
            pen3.Dispose();
            formgraphics.Dispose();
        }
    }
}


Ant here is the download link:
Download This Project (rar file)

Open without compiling -> Go to the project folder -> bin - > debug -> SimpleSquareGraphics.exe
Enjoy (:
And remember
Questions = Comments!


No comments:

Post a Comment