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.
Facet class file
#include "stdafx.h"
#include "Facet.h"
#include "StraightLine.h"
StraightLine* motion;
Facet::Facet(HWND hWnd, int window_width, int window_height)
{
motion = new StraightLine(hWnd, window_width, window_height);
}
bool Facet::decorateButton(WPARAM wParam, LPARAM lParam) {
if (wParam == 12321)
{
LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT)lParam;
SetDCBrushColor(lpDIS->hDC, RGB(255, 192, 203));
SelectObject(lpDIS->hDC, GetStockObject(DC_BRUSH));
RECT rect = { 0 };
rect.left = lpDIS->rcItem.left;
rect.right = lpDIS->rcItem.right;
rect.top = lpDIS->rcItem.top;
rect.bottom = lpDIS->rcItem.bottom;
RoundRect(lpDIS->hDC, rect.left, rect.top, rect.right, rect.bottom, 50, 50);
DrawText(lpDIS->hDC, TEXT("MOVE"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
return TRUE;
}
return FALSE;
}
void Facet::informPaint() {
motion->paint();
}
bool Facet::actionPerformed(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
switch (LOWORD(wParam))
{
case 12321:
motion->moveInLine();
return TRUE;
default:
return FALSE;
}
}
Facet::~Facet()
{
delete motion;
}
StraightLine Header file
#pragma once
#define aWIDTH 10
#define aHEIGHT 10
class StraightLine
{
public:
StraightLine(HWND, int, int);
virtual ~StraightLine();
void paint();
void moveInLine();
protected:
HWND hWindow;
HDC hdc;
int window_width;
int window_height;
COLORREF ball_colour;
int x1;
int x2;
int y1;
int y2;
int x;
int y;
double m, c;
HPEN ball_pen;
HBRUSH ball_brush;
};
C++ code for StraightLine Class file
#include "stdafx.h"
#include "StraightLine.h"
#include <math.h>
StraightLine::StraightLine(HWND hWnd, int window_width, int window_height)
{
hWindow = hWnd;
this->window_width = window_width;
this->window_height = window_height;
ball_colour = RGB(255, 0, 0);
x1 = 20;
x2 = 800;
y1 = 100;
y2 = 400;
x = x1;
y = y1;
m = (double)(y2 - y1) / (x2 - x1);
c = (double)(x2 * y1 - x1 * y2) / (x2 - x1);
ball_pen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
ball_brush = CreateSolidBrush(ball_colour);
hdc = GetDC(hWindow);
SelectObject(hdc, ball_pen);
SelectObject(hdc, ball_brush);
}
void StraightLine::paint() {
Ellipse(hdc, x, y, x + aWIDTH, y + aHEIGHT);
}
void StraightLine::moveInLine() {
while (x + aWIDTH <= window_width) {
y = (int)ceil(m * x + c);
paint();
x += 20;
Sleep(50);
}
}
StraightLine::~StraightLine()
{
DeleteObject(ball_pen);
DeleteObject(ball_brush);
ReleaseDC(hWindow, hdc);
}