You are on page 1of 7

Base Ball

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

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

//BaseBall (ideal Case)

private void button1_Click(object sender, EventArgs e)


{
Point or = new Point(200, 250);
int width = 150;
DrawAxes(or, 150, "x", "y");
int size = 500;
double v0 = 20, g = 9.8, th = 45 * 3.1417 / 180,dt=0.01;
double[] vx = new double[size];
double[] vy = new double[size];
double[] x = new double[size];
double[] y = new double[size];
vx[0] = v0 * Math.Cos(th);
vy[0] = v0 * Math.Sin(th);
for (int i = 0; i < x.Length - 1; i++)
{
vx[i + 1] = vx[i];
vy[i + 1] = vy[i] - g * dt;
x[i + 1] = x[i] + vx[i] * dt;
y[i + 1] = y[i] + vy[i] * dt;
}
Graphics gg = this.CreateGraphics();
Pen pp = new Pen(Color.Red);
for (int i = 0; i < x.Length ; i++)
{
if (y[i] >= 0) // to stop at +ve side
{
gg.DrawEllipse(pp, or.X + (float)(x[i] / x.Max()
* width), or.Y - (float)(y[i] / y.Max() * width), 5, 5);
}
}
}

//BaseBall(realistic case)

private void button2_Click(object sender, EventArgs e)


{
Point or = new Point(200, 250);
int width = 150;
DrawAxes(or, 150, "x", "y");
int size = 500;
double v0 = 20, g = 9.8, th = 45 * 3.1417 / 180, dt =
0.01;
double v,vd=35,dv=5,b2m;
double[] vx = new double[size];
double[] vy = new double[size];
double[] x = new double[size];
double[] y = new double[size];
vx[0] = v0 * Math.Cos(th);
vy[0] = v0 * Math.Sin(th);
for (int i = 0; i < x.Length - 1; i++)
{
v = Math.Sqrt((Math.Pow(vx[i], 2) + Math.Pow(vy[i],
2)));
b2m=0.0039+0.0058/(1+Math.Exp((v-vd)/dv));

vx[i + 1] = vx[i]-(b2m*v*vx[i])*dt;

vy[i + 1] = vy[i] - g * dt - (b2m * v * vy[i]) * dt;


x[i + 1] = x[i] + vx[i] * dt;
y[i + 1] = y[i] + vy[i] * dt;
}
Graphics gg = this.CreateGraphics();
Pen pp = new Pen(Color.Green);
for (int i = 0; i < x.Length; i++)
{
if (y[i] >= 0) // to stop at +ve side
{
gg.DrawEllipse(pp, or.X + (float)(x[i] / x.Max()
* width), or.Y - (float)(y[i] / y.Max() * width), 5, 5);
}
}
}

//BaseBall(wind Effect)

private void button3_Click(object sender, EventArgs e)


{
Point or = new Point(200, 250);
int width = 150;
DrawAxes(or, 150, "x", "y");
int size = 500;
double v0 = 20, g = 9.8, th = 45 * 3.1417 / 180, dt =
0.01;
double v, vd = 35, dv = 5, b2m;
double y0 = 1 * Math.Pow(10, 4),
row,vw=5,vwx=vw*Math.Cos(th);
double[] vx = new double[size];
double[] vy = new double[size];
double[] x = new double[size];
double[] y = new double[size];
vx[0] = v0 * Math.Cos(th);
vy[0] = v0 * Math.Sin(th);

for (int i = 0; i < x.Length - 1; i++)


{
v = Math.Sqrt((Math.Pow(vx[i], 2) + Math.Pow(vy[i],
2)));
b2m = 0.0039 + 0.0058 / (1 + Math.Exp((v - vd) /
dv));
row = Math.Exp((-y[i]/y0));

vx[i + 1] = vx[i] - row*b2m


*Math.Abs((v+vw))*(vx[i]-vwx)*dt;

vy[i + 1] = vy[i] - g * dt -row*b2m


*Math.Abs((v+vw))*vy[i] * dt;
x[i + 1] = x[i] + vx[i] * dt;
y[i + 1] = y[i] + vy[i] * dt;
}
Graphics gg = this.CreateGraphics();
Pen pp = new Pen(Color.Blue);
for (int i = 0; i < x.Length; i++)
{
if (y[i] >= 0) // to stop at +ve side
{
gg.DrawEllipse(pp, or.X + (float)(x[i] / x.Max()
* width), or.Y - (float)(y[i] / y.Max() * width), 5, 5);
}
}
}

//BaseBall(Spin Effect X & y)

private void button4_Click(object sender, EventArgs e)


{
Point or = new Point(200, 250);
int width = 150;
DrawAxes(or, 150, "x", "y");
int size = 500;
double v0 = 20, g = 9.8, th = 45 * 3.1417 / 180, dt =
0.01;
double v, vd = 35, dv = 5, b2m;
double s0=4,w=40,m=4;
double[] vx = new double[size];
double[] vy = new double[size];
double[] vz = new double[size];
double[] x = new double[size];
double[] y = new double[size];
double[] z = new double[size];
vx[0] = v0 * Math.Cos(th);
vy[0] = v0 * Math.Sin(th);
vz[0] = 0.1;
for (int i = 0; i < x.Length - 1; i++)
{
v = Math.Sqrt((Math.Pow(vx[i], 2) + Math.Pow(vy[i],
2)+Math.Pow(vz[i],2)));
b2m = 0.0039 + 0.0058 / (1 + Math.Exp((v - vd) /
dv));
vx[i + 1] = vx[i] - (b2m * v * vx[i]) * dt;

vy[i + 1] = vy[i] - g * dt ;

vz[i + 1] = vz[i] - s0/m* vx[i]*w * dt;

x[i + 1] = x[i] + vx[i] * dt;


y[i + 1] = y[i] + vy[i] * dt;
z[i + 1] = z[i] + vz[i] * dt;
}
Graphics gg = this.CreateGraphics();
Pen pp = new Pen(Color.Green);
for (int i = 0; i < x.Length; i++)
{
if (y[i] >= 0) // to stop at +ve side
{
gg.DrawEllipse(pp, or.X+
(float)(x[i]/x.Max()*width), or.Y - (float)(y[i]/y.Max()*width), 5,
5);

}
}
}
private void DrawAxes(Point or, int width, String xlabel,
String ylabel)
{
Point p1 = new Point(or.X - width, or.Y);
Point p2 = new Point(or.X + width, or.Y);
Point p3 = new Point(or.X, or.Y - width);
Point p4 = new Point(or.X, or.Y + width);
Graphics gg = this.CreateGraphics();
Pen pp = new Pen(Color.Brown, 3); // increase axes
size
gg.DrawLine(pp, p1, p2);
gg.DrawLine(pp, p3, p4);
Font f = new Font("Arial", 17); // increase the
label size and style of label
SolidBrush sb = new SolidBrush(Color.Black);

gg.DrawString(xlabel, f, sb, (or.X + p2.X) / 2, or.Y);


gg.DrawString(ylabel, f, sb, or.X - 75, (or.Y + p3.Y) /
2);
}
//BaseBall(Spin Effect X & z)

private void button5_Click(object sender, EventArgs e)


{
Point or = new Point(200, 250);
int width = 150;
DrawAxes(or, 150, "X", "Z");
int size = 1000;
double v0 = 70, g = 9.8, th = 45 * 3.1417 / 180, dt =
0.01;
double v, vd = 35, dv = 5, b2m;
double s0 = 4, w = 40, m = Math.Pow(10,4);
double[] vx = new double[size];
double[] vy = new double[size];
double[] vz = new double[size];
double[] x = new double[size];
double[] y = new double[size];
double[] z = new double[size];
vx[0] = v0 * Math.Cos(th);
vy[0] = v0 * Math.Sin(th);
vz[0] = 0.1;
for (int i = 0; i < x.Length - 1; i++)
{
v = Math.Sqrt((Math.Pow(vx[i], 2) + Math.Pow(vy[i],
2) + Math.Pow(vz[i], 2)));
b2m = 0.0039 + 0.0058 / (1 + Math.Exp((v - vd) /
dv));

vx[i + 1] = vx[i] - (b2m * v * vx[i]) * dt;

vy[i + 1] = vy[i] - g * dt;

vz[i + 1] = vz[i] - (s0 / m) *( vz[i] * w * dt);

x[i + 1] = x[i] + vx[i] * dt;


y[i + 1] = y[i] + vy[i] * dt;
z[i + 1] = z[i] + vz[i] * dt;
}
Graphics gg = this.CreateGraphics();
Pen pp = new Pen(Color.Green);
for (int i = 0; i < x.Length; i++)
{
if (y[i] >= 0) // to stop at +ve side
{
gg.DrawEllipse(pp, or.X + (float)(x[i] / x.Max()
* width), or.Y - (float)(z[i] / z.Max() * width), 5, 5);

}
}
}
}
}

You might also like