Írjunk szimulációs programot szimmetrikus forgattyú elrendezésű dugattyús belsőégésű motor működésére..
A feladat megoldása
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int k;
if (textBox1.Text != "")
{
k = Int32.Parse(textBox1.Text);
Dugattyu d1 =
new Dugattyu(200, 10, 150, 250, 10,
210, 70, k);
d1.Mozog(this);
}
}
}
class Henger
{
protected int x1h, y1h;
protected int a1, b1;
public Henger(int x, int y, int a, int b)
{
x1h = x;
y1h = y;
a1 = a;
b1 = b;
}
public virtual void Rajzol(Form1 form)
{
Graphics g = form.CreateGraphics();
Pen p = new Pen(Color.Black, 1);
g.DrawLine(p, new Point(x1h, y1h),
new Point(x1h + a1, y1h));
g.DrawLine(p, new Point(x1h, y1h),
new Point(x1h, y1h + b1 + 17));
g.DrawLine(p, new Point(x1h + a1, y1h),
new Point(x1h + a1, y1h + b1 + 17));
}
}
class Dugattyu : Henger
{
private int h1, sz, m;
private int kesleltet;
private int x1d, y1d, x2d, y2d; // rúd
private int xt1, yt1, xt2, yt2; // tengely
private int xr1, yr1, r1, Rt;
private int xR2, yR2;
private int xv, yv;
private int x1dv, y1dv, x2dv, y2dv;
private int xr1v, yr1v;
private int xt1v, yt1v, xt2v, yt2v;
public Dugattyu(int x, int y, int a, int b, int r, int h,
int R2, int kesl) :
base(x, y, a, b)
{
xv = 0;
yv = 0;
h1 = h;
r1 = r;
Rt = R2;
kesleltet = kesl
x1d = x1h + 3;
y1d = y1h + 3;
x2d = x1d + a1 - 6;
y2d = y1d;
sz = x2d - x1d;
m = b1 / 2;
xr1 = x1d + sz / 2;
yr1 = y1d + m / 2;
xt1 = xr1; yt1 = yr1;
xt2 = xt1; yt2 = yr1 + h1;
xR2 = xr1;
yR2 = yr1 + h1 + Rt;
}
public void Számol()
{
x1dv = x1d;
y1dv = y1d + yv;
x2dv = x2d;
y2dv = y1dv;
xr1v = xr1;
yr1v = yr1 + yv;
xt1v = xr1v; yt1v = yr1v;
xt2v = xt2 + xv; yt2v = yt2 + yv;
}
public override void Rajzol(Form1 form)
{
Graphics g = form.CreateGraphics();
g.Clear(Color.White);
base.Rajzol(form);
Pen p = new Pen(Color.Red, 1);
g.DrawLine(p, new Point(x1dv, y1dv),
new Point(x2dv, y2dv));
g.DrawLine(p, new Point(x1dv, y1dv),
new Point(x1dv, y2dv + m));
g.DrawLine(p, new Point(x2dv, y2dv),
new Point(x2dv, y2dv + m));
Rectangle t = new Rectangle(
new Point(xr1v - r1, yr1v - r1),
new Size(2 * r1, 2 * r1));
g.DrawEllipse(p, t);
g.DrawLine(p, new Point(xt1v, yt1v),
new Point(xt2v, yt2v));
Rectangle t1 = new Rectangle(
new Point(xt2v - r1, yt2v - r1),
new Size(2 * r1, 2 * r1));
g.DrawLine(p, new Point(xt2v, yt2v),
new Point(xR2, yR2));
Rectangle t2 = new Rectangle(
new Point(xR2 - r1, yR2 - r1),
new Size(2 * r1, 2 * r1));
g.DrawEllipse(p, t2);
Pen p1 = new Pen(Color.Cyan, 1);
Rectangle t3 = new Rectangle(
new Point(xR2 - Rt, yR2 - Rt),
new Size(2 * Rt, 2 * Rt));
g.DrawEllipse(p1, t3);
Thread.Sleep(kesleltet);
}
public void Forgat(int j, int e1, int e2, Form1 form)
{
double szog;
szog = j * 3.141596 / 180.0;
xv = (int)(e1 * (Rt * Math.Sin(szog))); // trunc
yv = (int)(Rt + e2 * (Rt * Math.Cos(szog))); //trunc
Számol();
Rajzol(form);
}
public void Mozog(Form1 form)
{
int i, j, ki;
ki = 0;
for (j = 1; j <= 4; j++)
{
switch (j)
{
case 1:
for (i = 0; i <= 90; i++)
{
Forgat(i, 1, -1, form);
}
break;
case 2:
for (i = 90; i >= 0; i--)
{
Forgat(i, 1, 1, form);
}
break;
case 3:
for (i = 0; i <= 90; i++)
{
Forgat(i, -1, 1, form);
}
break;
case 4:
for (i = 90; i >= 0; i--)
{
Forgat(i, -1, -1, form);
}
break;
}
if (ki == 1) break;
}
}
A program futási eredményei: