Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

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 (:
 (:(:

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!