• ≡ Menu
  • Home
  • Category
    • Animation
    • Static Models
    • Artwork
    • Music
    • Programming
    • Videos
    • Other
  • Tools
    • C#
    • Engines/SDK's
    • Design
    • Animation/Modelling
    • Design/Mapping
    • How to
  • Home
  • Contact
  • Projects
  • Notes
Menu
C# Programming Quadratic eq & graph (Beta)

Quadratic eq & graph (Beta)


Hi everyone!

I've been working on a multi function program that can resolve and graph mathematic functions (like senx, cosx, ax²+bx+c, mx+b, 1/x, Math.Pow(e, x), etc). Last week I made the quadratic function algorithm.

When I get the program finished I'll upload here with download link, it's called Blitzmath.

So, here's the code:

Equation to get x1 and x2:

x1 = (-b + (Math.Sqrt((Math.Pow(b, 2)) - (4 * a * c)))) / (2 * a);
x2 = (-b - (Math.Sqrt((Math.Pow(b, 2)) - (4 * a * c)))) / (2 * a);  

Note: You must declare a, b, c, x1, x2 as double.

After that, you should want to get vertices, here's the equation:

xv = (x1 + x2) / 2;
yv = (a*(Math.Pow(xv, 2)))+(b*xv)+c;

We need those vertices to set up the origin with R² plane

Well, now we're goin' to graph this function.

First of all you need to put a Panel and a Timer in your form, just like this:























Now we should set up those controls like this:

Double click on the Timer in order to modify its code

private void timer1_Tick(object sender, EventArgs e)
        {
            System.Drawing.Pen linep;
            linep = new System.Drawing.Pen(System.Drawing.Color.Black);
            System.Drawing.Graphics g = panel1.CreateGraphics();
            g.DrawLine(linep, 0, panel1.Height / 2, panel1.Width, panel1.Height / 2);
            g.DrawLine(linep, panel1.Width / 2, 0, panel1.Width / 2, panel1.Height);
            timer1.Enabled = false;
        }

And then go to public form1() and paste this:

public Form1()
        {
            
            InitializeComponent();
            timer1.Enabled = true;
        }

This code will draw the panel as x,y coordinates plane

Well, let's draw that parabola:
Note: The code has some troubles drawing negative vertices, I'll try to solve that as soon as I can.

System.Drawing.Graphics g = panel1.CreateGraphics();
int origy, origx;
Bitmap pen = new Bitmap(1, 1);
int vx = Convert.ToInt32(xv);
int vy = Convert.ToInt32(yv);
origx = (panel1.Width / 2) ;
origy = (panel1.Height / 2) ;
pen.SetPixel(0, 0, Color.Blue);
for (double i = 0; i <= 1000; i += 0.01)
            {
                x = i+xv;
                if (yv < 0)
                {
                    origy = (panel1.Height / 2) + vy;
                }
                y = (a * (Math.Pow((double)x, 2D)) + (b * x) + c) + yv;          
                g.DrawImageUnscaled(pen, origx + (int)x, origy - (int)y);
                g.DrawImageUnscaled(pen, origx - (int)x, origy - (int)y);
                if (y >= origy)
                {
                    break;
                }
            }
That should work in order to draw an aproximated graph.
If you want a values table you should declare two lists linked to separate listBoxes

List<double> table = new List<double>();
List<double> table2 = new List<double>();

After that you must evaluate the function on x values from 0 to <10, we'll do this with a FOR:

for (double j = 0; j < 10; j++)
            {
                double fx = (a * (j * j)) + (b * j) + c;
                table.Add(j);
                table2.Add(fx);
            }

I recommend 'ya to put a new button for showing the lists, in the button_click event paste this:

listBox1.DataSource = table;
listBox2.DataSource = table2;

I made this program in 45 minutes, so it has some bugs and the code is very untidy, if you have any question about the code or any recomendation just leave a comment here!
Add Comment
C#, Programming
domingo, 18 de septiembre de 2016

Share

Like

G+

Tweet

Tweet

Related Posts

:)
:(
=(
^_^
:D
=D
;)
:-bd
:p
:cool
:lv

Older Home

Weekly Posts

  • Maverick Artwork
    Maverick Artwork
    #1 #2 #3 #4
  • 3rd Rotation of the 7th Circle
    3rd Rotation of the 7th Circle
    Hi there! This is my 2nd soundtrack for my game, hope y'all enjoy it! Credits to Arnaud Krakowka for the drumtrack, visit his chan...

Label

  • Artwork
  • C#
  • Models
  • Music
  • Programming
  • Projects
  • Videos

Contact

Nombre

Correo electrónico *

Mensaje *

Con la tecnología de Blogger.

Labels

  • Artwork
  • C#
  • Models
  • Music
  • Programming
  • Projects
  • Videos

Copyright © 2014 Seventh Krieg All Right Reserved . Blogger Templates Design by Arlina . Powered by Blogger