Using the Equation of a Straight Line in C#
The equation of a straight line has the general form
y = mx + c;
where m is the slope and c
is the intercept on the y-axis.
For a vertical line, x is constant and for
a horizontal line, y is constant.
Generating Straight Lines for C#
Given any 2 points (x1, y1)
and (x2, y2); we'll have:
|
y2 - y1 |
= |
y - y1 |
x2 - x1 |
x - x1 |
⇒ y = ( |
y2 - y1 |
) x + |
x2y1 - x1y2 |
x2 - x1 |
x2 - x1 |
Comparing this to the general equation of a straight line,
i.e. y = mx + c
m = |
y2 - y1 |
x2 - x1 |
& |
c = |
x2y1 - x1y2 |
x2 - x1 |
Say we are to find the equation for the line represented
by the arbitrary points (50, 50) and (200, 100):
m = |
100 - 50 |
= |
50 |
= |
1 |
200 - 50 |
150 |
3 |
& |
c = |
200(50) - 50(100) |
= |
10000 - 5000 |
200 - 50 |
150 |
= |
5000 |
= |
100 |
150 |
3 |
Hence,
y = 1/3x + 100/3
or
3y = x + 100
Code to Animate a Graphic Object by a Line Equation in C#
To make a graphic (dot) travel by the equation of a line,
continuously increment x, and use the equation to
get the corresponding y value.
Let's do so with the above equation representing points
(x1, y1) = (50, 50)
and (x2, y2) = (100, 200).
Create a new class;
Call it StraightLine.
Type out the adjoining C# code for animating an image body through
the path of a straight line.
Dymetric class
using System.Windows.Forms;
namespace Dymetric
{
class Dymetric
{
private StraightLine line_motion;
private bool do_simulation;
public Dymetric(int width, int height)
{
line_motion = new StraightLine(width, height);
do_simulation = false;
}
public void decideAction(PaintEventArgs e, bool click_check)
{
if (do_simulation && click_check)
{
line_motion.inPlay(e);
do_simulation = false;
}
else
{
line_motion.clearAndDraw(e);
do_simulation = true;
}
}
}
}
C# code for StraightLine class
using System;
using System.Threading;
using System.Drawing;
using System.Windows.Forms;
namespace Dymetric
{
class StraightLine
{
private int x1, y1, x2, y2, x, y;
private double m, c;
private const int dotDIAMETER = 10;
private Bitmap offscreen_bitmap;
Graphics offscreen_g;
private Brush dot_colour, bg_colour;
public StraightLine(int screen_width, int screen_height)
{
dot_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);
x1 = x = 10;
y1 = y = 10;
x2 = offscreen_bitmap.Width - 50;
y2 = offscreen_bitmap.Height - 50;
m = (double)(y2 - y1) / (x2 - x1);
c = (x2 * y1 - x1 * y2) / (x2 - x1);
}
public void clearAndDraw(PaintEventArgs e)
{
offscreen_g.Clear(Color.LightGray);
offscreen_g.FillEllipse(dot_colour, x, y, dotDIAMETER, dotDIAMETER);
Graphics gr = e.Graphics;
gr.DrawImage(offscreen_bitmap, 0, 55, offscreen_bitmap.Width, offscreen_bitmap.Height);
}
public void inPlay(PaintEventArgs e)
{
Graphics gr = e.Graphics;
while (x < offscreen_bitmap.Width - dotDIAMETER)
{
offscreen_g.FillEllipse(dot_colour, x, y, dotDIAMETER, dotDIAMETER);
gr.DrawImage(offscreen_bitmap, 0, 55, offscreen_bitmap.Width, offscreen_bitmap.Height);
x += 20;
y = (int)Math.Ceiling(m * x + c);
Thread.Sleep(50);
}
x = x1;
y = y1;
}
}
}