Saturday, August 4, 2012

C# Tree fractal

NOTE: I UPDATED ALL DOWNLOAD LINK 

Hello again!
Remember i did a javascript version of the Tree Fractal(with the help of rosettacode)
So iv'e built another version in C# 








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 TreeFractal
{
    public partial class Form1 : Form
    {
        static public Pen p = new Pen(Color.Black, 1);
        static public Graphics g;
        static public double deg_2_rad = Math.PI / 180.0; 
        static public float globalH;
        static public float globalW;
        static readonly object _locky = new object();

        public Form1()
        {
            InitializeComponent();
        }


        private void drawTree(float x1, float y1, double angle, int depth,  double angle1)
        {
            try
            {
                if (depth != 0)
                {

                    float x2 = (float)(x1 + (Math.Cos(angle * deg_2_rad) * depth * 10.0));
                    float y2 = (float)(y1 + (Math.Sin(angle * deg_2_rad) * depth * 10.0));
                    lock (_locky)
                    {
                        this.Update();
                        p.Color = Color.FromArgb((depth * 255) % 16 * 14, (depth * 255) % 16 * 14, (depth * 255) % 128 * 2);
                        g.DrawLine(p, x1, y1, x2, y2);
                    }
                   
                    drawTree(x2, y2, angle - angle1, depth - 1, angle1);
                    drawTree(x2, y2, angle + angle1, depth - 1, angle1);
                    
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        
        private void TreeCanvasLimits(float x1,float y1, double angle,int depth,double angle1)
        {
             if (depth != 0){
              float x2 = (float)(x1 - (Math.Cos(angle * deg_2_rad) * depth * 10.0));
              float y2 = (float)(y1 - (Math.Sin(angle * deg_2_rad) * depth * 10.0));
              if (globalW > x2)
              {
                  globalW = x2;
              }
              if(globalH < y2)
              {
                  globalH = y2;
              }
              TreeCanvasLimits(x2, y2, angle - angle1, depth - 1,angle1);
              TreeCanvasLimits(x2, y2, angle + angle1, depth - 1,angle1);
             }
        }
        private void btnCreate_Click(object sender, EventArgs e)
        {
            g = this.CreateGraphics();
            
            try
            {
                //globalH = 0;
                //globalW = 0;
                //TreeCanvasLimits(0, 0, -90.0, int.Parse(txtDep.Text), double.Parse(txtAngle.Text));
                //this.Size = new Size((int)((globalW * (-2)) + 40), (int)((globalH + 60)));
                g.Clear(Color.White);
                drawTree((this.Width ) / 2, (this.Height - 150), -90.0, int.Parse(txtDep.Text), double.Parse(txtAngle.Text));
            }
            catch(Exception exx)
            {
                MessageBox.Show(exx.ToString());
            }
        }

        private void btnLoop_Click(object sender, EventArgs e)
        {
            try
            {
                g = this.CreateGraphics();
                g.Clear(Color.White);
                for (int i = 0; i < 361; i++)
                {
                    //g.Clear(Color.White);
                    //p.Color = Color.FromArgb((int)(i/4),(int)(i/3), (int)(i/2));
                    //MessageBox.Show("before " + i.ToString());
                    this.SuspendLayout();
                    drawTree((this.Width) / 2, (this.Height - 150), -90.0, 10, (double)i);
                    //System.Threading.Thread.Sleep(100);
                    this.ResumeLayout();
                    Application.DoEvents();
                    //MessageBox.Show("after " + i.ToString());
                    this.Update();
                }
                MessageBox.Show("Done");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            this.Update();
        }
    }
}


and here is a download link
Download The Project (rar file)

Friday, August 3, 2012

Flash Game Tryout

Hello Everyone!
This is a flash game iv'e built with the help of this set of videos on youtube, it was very simple and very fun to learn. So hope you enjoy my first flash game (:
Please Subscribe and leave Comments, Cya (:
 (:(:

Saturday, July 28, 2012

JavaScript Mandelbrot set Fractal

Hello Everyone!
To expand my "Build Fractals" Corner I've Taken from RosettaCode the code for the Mandelbrot set fractal.

I know it seems like I'm just copying others code, but I'll Post my original code soon.
So here is the link http://rosettacode.org/wiki/Mandelbrot_set

Iv'e taken the code from there, and i claim no rights on this code or whatsoever.
(To zoom change min and max!)



Mandelbrot set
    
  
 
  
    

Mandelbrot set

xmin = xmax =
ymin = ymax =
iterations =
This page needs a browser with canvas support.
Mandelbrot set

Mandelbrot set

xmin = xmax =
ymin = ymax =
iterations =
This page needs a browser with canvas support.

Thursday, July 19, 2012

JavaScript Tree Fractal

Hello Everyone! Today we are going to discuss one of the most simple and beautiful fractals there is, the Tree Fractal!
Algorithm (very simple one):
1. Draw a line
2. From the end of that line draw another two lines, that are split by an angle
3. Repeat with shorter lines, but with the same angle
 Now, I've taken a code from http://rosettacode.org/wiki/Fractal_tree , and made some changes in it so you can have the opportunity to create your own tree fractal right here in the site,
So Here Is The JavaScript Code:


Just enter the parameters(angle and depth) and it will create you the tree(credit for rosettacode):
BEWARE - to much depth will get your browser stuck!
Angle: Depth:            
 

Tuesday, July 10, 2012

The Future of the Internet

Hello Everyone, haven't published in a while(Sorry For That). Yesterday I came across with this video about the future of the internet. I find this video to be very interesting, so I decided to bring it to you.
 So here is the video, Enjoy (:

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!


Friday, March 23, 2012

C# Warcraft 3 Hero Line Wars AutoClicker

Hello There!

So this post is for the Warcraft 3 players!
If you play in the battle.net you probably came across with my favorite custom game.
Hero Line Wars!

Why do we Need an Autoclicker for this?
Everytime you want to buy a creature you can click on an hotkey to buy it, so my program just clicks for you the requested times.



Very simple program, using mostly the RegisterHotKey command (explanation on the AutoClicker Post).
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;
using System.Runtime.InteropServices;
using System.Threading;

namespace Warcraft3HLW
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll")]
        private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);

        [DllImport("user32.dll")]
        private static extern bool UnregisterHotKey(IntPtr hWnd, int id);

        static bool run = false;
        
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            RegisterHotKey(this.Handle, (int)Keys.NumPad0, 0, (int)Keys.NumPad0);
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);

            if (m.Msg == 0x312)
            {

                Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF);
                if (key == Keys.NumPad0 && run)
                {
                    run = !run;
                    UnregisterHotKey(this.Handle, (int)Keys.Q);
                    UnregisterHotKey(this.Handle, (int)Keys.W);
                    UnregisterHotKey(this.Handle, (int)Keys.E);
                    UnregisterHotKey(this.Handle, (int)Keys.R);
                    UnregisterHotKey(this.Handle, (int)Keys.T);
                    UnregisterHotKey(this.Handle, (int)Keys.A);
                    UnregisterHotKey(this.Handle, (int)Keys.S);
                    UnregisterHotKey(this.Handle, (int)Keys.D);
                    UnregisterHotKey(this.Handle, (int)Keys.F);
                    UnregisterHotKey(this.Handle, (int)Keys.G);
                }
                else
                {
                    if (key == Keys.NumPad0 && !run)
                    {
                        run = !run;

                        RegisterHotKey(this.Handle, (int)Keys.NumPad0, 0, (int)Keys.NumPad0);
                        RegisterHotKey(this.Handle, (int)Keys.Q, 0, (int)Keys.Q);
                        RegisterHotKey(this.Handle, (int)Keys.W, 0, (int)Keys.W);
                        RegisterHotKey(this.Handle, (int)Keys.E, 0, (int)Keys.E);
                        RegisterHotKey(this.Handle, (int)Keys.R, 0, (int)Keys.R);
                        RegisterHotKey(this.Handle, (int)Keys.T, 0, (int)Keys.T);
                        RegisterHotKey(this.Handle, (int)Keys.A, 0, (int)Keys.A);
                        RegisterHotKey(this.Handle, (int)Keys.S, 0, (int)Keys.S);
                        RegisterHotKey(this.Handle, (int)Keys.D, 0, (int)Keys.D);
                        RegisterHotKey(this.Handle, (int)Keys.F, 0, (int)Keys.F);
                        RegisterHotKey(this.Handle, (int)Keys.G, 0, (int)Keys.G);
                    }
                }
                if (run)
                {
                    string ans = "";
                    switch (key)
                    {
                        case Keys.Q:
                            ans = "Q";
                            break;

                        case Keys.W:
                            ans = "W";
                            break;

                        case Keys.E:
                            ans = "E";
                            break;

                        case Keys.R:
                            ans = "R";
                            break;

                        case Keys.T:
                            ans = "T";
                            break;

                        case Keys.A:
                            ans = "A";
                            break;

                        case Keys.S:
                            ans = "S";
                            break;

                        case Keys.D:
                            ans = "D";
                            break;

                        case Keys.F:
                            ans = "F";
                            break;

                        case Keys.G:
                            ans = "G";
                            break;
                    }
                    if (ans != "")
                    {
                        for (int i = 0; i < 30; i++)
                        {
                            SendKeys.Send(ans);
                        }
                    }
                }
            }
         
        }

        static void doit(Message m)
        {
            
        }
    }
}


Use numlock0 to start and stop the program, and click on the requested Hotkey (for example Q).

Ok so you can download this Project at the following link:
Download This Project (rar file)

Run Without compiling ->Go to the project folder ->bin ->debug -> Warcraft3HLW.exe
This is it, I made this program very simple, and without labels and stuff.

Enjoy (:
And Remember
Questions = Comments!

Thursday, March 22, 2012

C# Mandelbrot set Fractal

Welcome back!

Today I'm going to show you how to build a program that does this:


















(image from Wikipedia)

But What is this?
This is a fractal based on the Mandelbrot Set. I don't really know anything on the Mandelbrot Set except what i read from Wikipedia, so here is a link on the Mandelbrot Set - Look at the "for programmers" section, this is what i looked at.

So How we Program This?
Ok, From what I understood from Wikipedia the fractal we are trying to represent is based on the following formula:





when c is a complex number.
(info from Wikipedia)

Which means we need to programmatically consider the real and imaginary values of x and y.
As far as i understood, we are checking if the x and y values that we put in the formula above is not going above infinity, if they do they are not in the set.

So the whole program I built is based on the pseudocode on Wiki.
I added an extra thing that i found on the web, to make it more interesting I'm taking the number of times the loop went for every pixel and if it's not the max value(not in the set) i'm using the following code to paint it:

You can play with the colors if you change this code.

Code Explanation:
Ok, so what I'm doing in my code is as follows:
1. I define a bitmap that is representing the real axis and imaginary axis, which means that the middle of the bitmap is actually the (0,0) point of the grid, and as written in Wiki "for programmers" section i define the left edge as -2, the right edge as 2, the upper edge as 2 and the down edge as -2.
now we defined a grid.

2. I'm running on every pixel on the bitmap, calculating it's x and y value and then starting the loop that checks if it's in the Mandelbrot Set.

3.Calculating and printing the color of the pixel and then starting again with the next pixel.

The Program Output's this:


Now I added a zooming function, to zoom in you just click on the bitmap.
How Does It Work?
I just define new borders to the bitmap, something less than two.
So If you zoom in you can get quite astonishing pictures, as so:



Now you have to remember that this program isn't accurate!
Why?
The program relies on "double" type numbers, that has limited accuracy, so the more zoomed in you get The more it is not accurate (at first it's accurate but after zooming in it won't be).

I added another couple of things:
Save Button - saves the current bitmap image to desktop.
Reset Button - to start from the beginning.

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;
using System.IO;

namespace Fractals
{
    public partial class Form1 : Form
    {
        static double currentmaxr = 0;
        static double currentminr = 0;
        static double currentmaxi = 0;
        static double currentmini = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            Bitmap img = MandelbrotSet(pictureBox1, 2, -2, 2, -2);
            pictureBox1.Image = img;
        }
        static Bitmap MandelbrotSet(PictureBox pictureBox1, double maxr, double minr, double maxi, double mini)
        {
            currentmaxr = maxr;
            currentmaxi = maxi;
            currentminr = minr;
            currentmini = mini;
            Bitmap img = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            double zx = 0;
            double zy = 0;
            double cx = 0;
            double cy = 0;
            double xjump = ((maxr - minr) / Convert.ToDouble(img.Width));
            double yjump = ((maxi - mini) / Convert.ToDouble(img.Height));
            double tempzx = 0;
            int loopmax = 1000;
            int loopgo = 0;
            for (int x = 0; x < img.Width; x++)
            {
                cx = (xjump * x) - Math.Abs(minr);
                for (int y = 0; y < img.Height; y++)
                {
                    zx = 0;
                    zy = 0;
                    cy = (yjump * y) - Math.Abs(mini);
                    loopgo = 0;
                    while (zx * zx + zy * zy <= 4 && loopgo < loopmax)
                    {
                        loopgo++;
                        tempzx = zx;
                        zx = (zx * zx) - (zy * zy) + cx;
                        zy = (2 * tempzx * zy) + cy;
                    }
                    if (loopgo != loopmax)
                        img.SetPixel(x, y, Color.FromArgb(loopgo % 128 * 2, loopgo % 32 * 7, loopgo % 16 * 14));
                    else
                        img.SetPixel(x, y, Color.Black);

                }
            }
            return img;

        }


        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            int ex = e.X;
            int ey = e.Y;
            double currentxjump = ((currentmaxr - currentminr) / Convert.ToDouble(pictureBox1.Width));
            double currentyjump = ((currentmaxi  - currentmini) / Convert.ToDouble(pictureBox1.Height));

            int zoomx = pictureBox1.Width/5 ;
            int zoomy = pictureBox1.Height/5;
            Bitmap img = MandelbrotSet(pictureBox1,((ex +zoomx) * currentxjump) -Math.Abs(currentminr) , ((ex-zoomx) * currentxjump) -Math.Abs(currentminr) , ((ey+zoomy ) * currentyjump) - Math.Abs(currentmini) , ((ey- zoomy) * currentyjump) - Math.Abs(currentmini));
            pictureBox1.Image.Dispose();
            pictureBox1.Image = img;
        }

        private void button1_Click(object sender, EventArgs e)
        {
           Bitmap img = MandelbrotSet(pictureBox1, 2, -2, 2, -2);
            pictureBox1.Image.Dispose();
            pictureBox1.Image = img;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
            string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            //string folderf = @"\Fractals";
            string filename = @"\Fractals";
            string filetype = @".jpeg";
            int mone=0;
            
            while (File.Exists(path + filename + mone.ToString() + filetype))
            {
                mone++;
            }
           
            pictureBox1.Image.Save(path + filename + mone.ToString() + filetype);
            MessageBox.Show("Saved To Desktop");

            
        }
    }

}





And here is the downloading link:
Download This Project (rar file)

Run without compiling: Go to the project folder -> bin -> debug -> Fractals.exe
Have Fun!
And Remember
Questions = Comments!

C# Sierpinski Triangle

Hello again lads!
Today we are going to program Sierpinski Triangle,

But What is Sierpinski Triangle? 
Sierpinski Triangle is a very nice triangle fractal, the fractal shows a triangle that has 3 triangles inside of it, and inside everyone of these triangles there is another 3 triangles and so on.

Here is a picture of it: 
















(This image is taken from Wikipedia)

Some of you may recognize this shape - it is a very common fractal.
Ok, so if you want to read more about this fractal here is a Wikipedia link

And Now to the Programming:
Ok, I  found an algorithm that represents this fractal, and it goes as follows:

1. Write three dots that represent the vertexes of the biggest triangle (the first triangle).
2. Start with a random point that is in the triangle borders.
3. Now choose a random point of the three vertexes you defined earlier, and calculate the distance between the current point and the vertex you choose, and print a point at the half of this distance.
Now use the point you printed and Do step 3 again and again, until you get the requested result.

I added another thing that makes it a bit more interesting. I painted the first vertexes with 3 different colors, red green and blue, and every time I'm printing a point I'm printing it with the color of the vertex that is chosen randomly.

This Program outputs this:


Now you need to remember that this algorithm is not 100% accurate!

Ok so here is the Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SerpinskiTri
{
    class Program
    {
        static void DotPrint(int x, int y, ConsoleColor col)
        {
            Console.ForegroundColor = col;
            Console.CursorLeft = x;
            Console.CursorTop = y;
            Console.Write(".");
        }
        static void Main(string[] args)
        {
            Console.CursorVisible = false;
            Console.SetWindowSize(80,40);
            int times = 0;
            Random rnd = new Random();
            Console.WriteLine();
            Console.Write("Enter Number of Times:");
            times = int.Parse(Console.ReadLine());
            int ChangeX = 35;
            int ChangeY = 20;
            int random = 0;
            for (int i = 0; i < times; i++)
            {
                random = rnd.Next(0,3);
                switch (random)
                {
                    case 0: //red dot
                        ChangeX = (ChangeX + 35) / 2; // the 35 is the x value of the red dot
                        ChangeY = (ChangeY + 1) / 2;
                        DotPrint(ChangeX, ChangeY, ConsoleColor.Red);
                        break;
                    case 1: // blue dot
                        ChangeX = (ChangeX + 1) / 2;
                        ChangeY = (ChangeY + 35) / 2;
                        DotPrint(ChangeX, ChangeY, ConsoleColor.Blue);
                        break;
                    case 2: // green dot
                        ChangeX = (ChangeX + 70) / 2;
                        ChangeY = (ChangeY + 35) / 2;
                        DotPrint(ChangeX, ChangeY, ConsoleColor.Green);
                        break;
                }
            }
            Console.ReadKey();
        }
    }
}


You can also download this project:
Download This Project (rar file)

Run Without Compiling: go to the project folder ->bin -> debug -> SerpinskiTri.exe

Questions = Comments!
I'll try to answer Asap.

Sunday, March 18, 2012

C Quadratic Equation Solver

Hello My Fellow Friends!

Today we are going to build a quadratic equation solver.

Why Quadratic Equation Solver?
-Good for home work, Saves you time and the energy!

Before we Start
Ok so I'm going to write this program in C, I'm using Bloodshed Dev- C/C++, which is based on GCC.
It doesn't matter what compiler you are using, but in my Project files, there is a .dev file that is meant for this compiler, in case you were wondering.

So Let's Start!
The quadratic equation solver will be mostly based on the....
Quadratic Equation
not very predictable.

The quadratic equation is:


(image from Wikipedia)



For an equation of the form:

(image from Wikipedia)



Now the equation can have three cases for any a,b,c, we determine the case by the Delta which is:

(guess what - image from Wikipedia)



case 1:
if Delta>0 (means it is positive) we have two different answers, x1 and x2.

case 2:
if Delta=0 we have one answer only, x.

case 3:
if Delta<0 (means it is negative) there is no answers (can't do square root of a negative number).

The program will use square root function, which is from the math library.
So we have to include the math.h as seen in the code.

That is the basic things that we need to know.
So here is my program


Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(int argc, char *argv[])
{
    int a,b,c; // aX^2 + bX + c = 0
    printf("Please Enter a,b,c\n");
    scanf("%d %d %d",&a,&b,&c);
    float d = b*b - (4*a*c);  //d = Delta
    
    if(d==0) printf("Delta = 0\n x=%f\n",(float)(-1*(b/2*a)));
    if(d<0) printf("Delta <0");
    else
    {
        if(d>0)
        {
              printf("Delta > 0\n x1=%f\n",(-1*b + sqrt(d))/2*a);
              printf("Delta > 0\n x2=%f\n",(-1*b - sqrt(d))/2*a);
        }
    }
  system("PAUSE"); 
  return 0;
}


Download This Project(rar file)

Notes:
- .c file is the code.
- .exe file is the output of this code (after compiling)
- .dev a file for Bloodshed Dev- C/C++ compiler.
-  Questions = Comments!

Saturday, March 17, 2012

C# AutoClicker

Hello There!
Today we are going to program an AutoClicker.



What is an AutoClicker?
AutoClicker is a program that automatically clicks a mouse click for you.

Why AutoClicker?
- There are a lot of games (mostly online) that we need to click a lot and we don't have the energy to do it.
- This Could be a nice prank if that's your laugh.

OK, So Let's Start!
This Program will be written in C#.
If you don't have any C# Compiler you can download  Visual Studio Express Edition, I recommend 2008, but it's ok if you have any other version.
If we wan't to build a good AutoClicker we have to make it approachable from any program,
so we have to find a way to start the AutoClicking without having to focus our AutoClicker.
I have found a nice way to do this:
using RegisterHotKey function that is in the "user32.dll".
To take this function from the "user32.dll" we will use the following.
Right Click on the form -> View Code

Add
using System.Runtime.InteropServices; 
and copy this Code

[DllImport("user32.dll")];

public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk); 


now we can use this function as we want.
This function registers a key, and when the key we registered is clicked it makes it go to our program.
In the program it will go to the "WndProc" function.(WndProc is the function that every time our form is being called it runs). 
Another Function we are going to need is:

[DllImport("user32.dll")];
  
public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);


copy it under the RegisteHotKey function.
This function is the function that clicks for us.
In the WndProc we need to check what it was called for.
To see if it was called because of a hotkey we use a hotkey const.
(every event has it's code)

int WM_HOTKEY = 0x312;

if (m.Msg == WM_HOTKEY)
            
Thats the code for hotkey.
now to check what hot key was clicked we use:
 Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF);
------------------------------------------------------------------------------------------------
Ok, i'm going to leave my code here, and the whole project.
If you have any questions you can write a comment, and i'll answer Asap.

Code:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;

namespace AutoClicker
{
    public partial class Form1 : Form
    {
        static Thread AutoClick;
        static int militime;
        public static int WM_HOTKEY = 0x312;

        [DllImport("user32.dll")]
        public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);

        [DllImport("user32.dll")]
        public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);

        private const int MOUSEEVENTF_LEFTDOWN = 0x02;
        private const int MOUSEEVENTF_LEFTUP = 0x04;
        private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
        private const int MOUSEEVENTF_RIGHTUP = 0x10;

        public void AClick()
        {
            while (true)
            {
                int x = Cursor.Position.X;
                int y = Cursor.Position.Y;
                mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, x, y, 0, 0);
                Thread.Sleep(militime);

            }
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            AutoClick = new Thread(AClick);
            RegisterHotKey(this.Handle,(int)Keys.F1,0,(uint)Keys.F1);
            AutoClick.IsBackground = true;
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);

            if (m.Msg == WM_HOTKEY)
            {
                try
                {
                    militime = int.Parse(textBox1.Text);
                }
                catch
                {
                    militime = 0;
                }
                Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF);
                if (key == Keys.F1)
                {
                    if (!AutoClick.IsAlive)
                    {
                        AutoClick.Start();
                        lstate.Text = "Clicking";
                        lstate.ForeColor = System.Drawing.Color.Green;
                        Controls.Add(lstate);
                    }
                    else
                    {
                        AutoClick.Abort();
                        AutoClick = new Thread(AClick);
                        lstate.Text = "Not Clicking";
                        lstate.ForeColor = System.Drawing.Color.Red;
                        Controls.Add(lstate);
                    }
                }
            }
            
        }

    }
}


Download The Project (rar file)

Important Notes:
-This program will not run on your computer if you don't have .Net support.
-If you have Windows that is a greater version than XP then you should have .Net support.
-Will not work on Mac or Linux without a special .Net support (if exists?)

Another Note:
if you want to activate the program without having to compile it yourself
(means open it as a .exe file and not as the project code itself)
Go to the Project Folder -> bin -> Debug -> AutoClicker.exe

First Post

Hello lads!
in this blog i will post my programs.
they will be open source.
Enjoy (: