Dymetric class
using System.Windows.Forms;
namespace Dymetric
{
class Dymetric
{
private StraightLineRegion line_region;
private bool do_simulation;
public Dymetric(int screen_width, int screen_height)
{
line_region = new StraightLineRegion(screen_width, screen_height);
do_simulation = false;
}
public void decideAction(PaintEventArgs e, bool click_check)
{
if (do_simulation && click_check)
{
line_region.inPlay(e);
do_simulation = false;
}
else
{
line_region.clearAndDraw(e);
do_simulation = true;
}
}
}
}
C# code for StraightLineRegion class
using System.Threading;
using System.Drawing;
using System.Windows.Forms;
namespace Dymetric
{
class StraightLineRegion
{
private int x_ball, y_ball, previous_x, previous_y;
private const int ballDIAMETER = 80;
private int x1, y1, x2, y2;
private double m, c, x_line;
private Bitmap offscreen_bitmap;
Graphics offscreen_g;
private Brush ball_colour, bg_colour;
public StraightLineRegion(int screen_width, int screen_height)
{
ball_colour = new SolidBrush(Color.Yellow);
bg_colour = new SolidBrush(Color.LightGray);
offscreen_bitmap = new Bitmap(screen_width, screen_height - 55,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
offscreen_g = Graphics.FromImage(offscreen_bitmap);
offscreen_g.Clear(Color.LightGray);
previous_x = x_ball = 10;
previous_y = y_ball = offscreen_bitmap.Height / 2 - ballDIAMETER / 2;
x1 = offscreen_bitmap.Width / 2 - 100;
y1 = 10;
x2 = offscreen_bitmap.Width / 2 + 100;
y2 = offscreen_bitmap.Height - 50;
m = (y2 - y1) / (x2 - x1);
c = (x2 * y1 - x1 * y2) / (x2 - x1);
x_line = (y_ball - c) / m;
}
public void clearAndDraw(PaintEventArgs e)
{
offscreen_g.DrawLine(Pens.Black, x1, y1, x2, y2);
offscreen_g.FillEllipse(bg_colour, previous_x, previous_y, ballDIAMETER, ballDIAMETER);
offscreen_g.FillEllipse(ball_colour, x_ball, y_ball, ballDIAMETER, ballDIAMETER);
previous_x = x_ball;
previous_y = y_ball;
Graphics gr = e.Graphics;
gr.DrawImage(offscreen_bitmap, 0, 55, offscreen_bitmap.Width, offscreen_bitmap.Height);
}
public void inPlay(PaintEventArgs e)
{
while (x_ball < offscreen_bitmap.Width - ballDIAMETER)
{
clearAndDraw(e);
x_ball += 10;
Thread.Sleep(50);
}
x_ball = 10;
y_ball = offscreen_bitmap.Height / 2 - ballDIAMETER / 2;
}
}
}