usingMaths.com
From Theory to Practice - Math You Can Use.







<< PreviousNext >>

Animating along a Straight Line in C++ - Slope, Intercept, and Line Equation



Understanding the Straight Line Equation (y = mx + c) | Maths Explanation for C++ Kids

In this tutorial, you'll learn how to draw a straight line in C++ using basic coordinate geometry principles. This lesson is designed for senior secondary students studying linear equations and straight-line graphs. We'll use simple C++ code to plot points, calculate the slope, and visualize the line on a canvas.

In coordinate geometry, whether for use in C++ or any other language, 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.
This formula helps in calculating and drawing straight lines in C++, whether for graphics, animations, or math-based programming.



Example: Finding the Line Equation Between Two Points | Maths Explanation for C++ Kids

In C++, you can formulate line equation using two known points:
Given any 2 points on the C++ Canvas (x1, y1) and (x2, y2); we'll have:

C++ straight line graph coordinates for linear equation y = mx + c
Figure: C++ straight line graph coordinates for linear equation y = mx + c
  y2 - y1 = y - y1
x2 - x1 x - x1
⇒ y   =   ( y2 - y1 ) x   +   x2y1 - x1y2
x2 - x1 x2 - x1

Comparing this linear equation, for the given C++ canvas points, 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 passing through the arbitrary points (50, 50) and (200, 100) on a C++ canvas:

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

This gives a C++-ready straight line equation that you can use to animate objects or draw lines on a canvas.


C++ Code Example - Animate Object Along a Straight Line

To animate a dot along a straight line in C++, we can increment the x-coordinate and compute the matching y-coordinate using the equation of the line.

Let's implement a C++ animation algorithm with the above equation representing points (x1, y1) = (50, 50) and (x2, y2) = (100, 200).

Create a new C++ project; call it Dymetric.
Create 2 new C++ class files;
Call them Facet and StraightLine.
Type out the adjoining C++ code for animating an image body through the path of a straight line.



Summary: Visualizing Linear Equations in C++

The straight line equation is one of the first concepts students learn in senior secondary mathematics. In C++, we can easily plot a line by defining its slope (m) and intercept (c). This C++ maths tutorial demonstrates how to compute the equation of a line given two points and visualize it using code.

Using C++ to draw straight lines helps students understand the relationship between slope and intercept in linear equations. The simple C++ code example provided demonstrates how to draw and animate a straight line in C++ using the slope-intercept equation. It's a fundamental concept in mathematical programming, computer graphics, and C++ animation.
This foundation helps you transition into more advanced C++ graphics and animation projects.


So! C++ Fun Practice Exercise - Animate in Straight Line

As a fun practice exercise, try modifying the C++ code to explore different gradients and intercepts. This will be a great way to connect mathematics and programming, and help you understand more about C++ animations and linear equations.







C++ Straight Line Dymetric Window Code Stub


C++ Straight Line Canvas Frame and Button Controls - Header File

#pragma once

class Facet
{
public:
    Facet(HWNDintint);
    virtual ~Facet();
    bool decorateButton(WPARAMLPARAM);
    bool actionPerformed(HWNDWPARAMLPARAM);
    void informPaint();
};


C++ Straight Line Canvas Frame and Button Controls - Class File

#include "stdafx.h"
#include "Facet.h"
#include "StraightLine.h"

StraightLine* motion;

/*
* Our custom class that interfaces between the parent window
* and the subsequent daemonstrator classes
*/

Facet::Facet(HWND hWndint window_widthint window_height)
{
    motion = new StraightLine(hWndwindow_widthwindow_height);
}


/*
* This guy decorates buttons with colour and title text
*/

bool Facet::decorateButton(WPARAM wParamLPARAM lParam) {
    // button glide calling
    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;
}

/*
* Call the target class' draw method
*/

void Facet::informPaint() {
    motion->paint();
}


/*
* Say there is more than a single push button,
* this guy picks out the correct button that got clicked
* and calls the corresponding apt function
*/

bool Facet::actionPerformed(HWND hWndWPARAM wParamLPARAM lParam)
{
    switch (LOWORD(wParam))
    {
    case 12321:
        motion->moveInLine();
        return TRUE;
    default:
        return FALSE;
    }
}

Facet::~Facet()
{
    delete motion;
}

C++ Animation Code for Straight Line - Header File

#pragma once

#define aWIDTH 10
#define aHEIGHT 10

class StraightLine
{
public:
    StraightLine(HWNDintint);
    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; // slope and y-intercept of a straight line
    HPEN ball_pen;
    HBRUSH ball_brush;
};


C++ Animation Code for Straight Line - Class File

#include "stdafx.h"
#include "StraightLine.h"
#include <math.h>


StraightLine::StraightLine(HWND hWndint window_widthint window_height)
{
    hWindow = hWnd// save away window handle
    this->window_width = window_width// save away window width
    this->window_height = window_height// save away window width

    ball_colour = RGB(255, 0, 0);
    x1 = 20;
    x2 = 800;
    y1 = 100;
    y2 = 400;
    x = x1;
    y = y1;

    m = (double)(y2 - y1) / (x2 - x1); // slope
    c = (double)(x2 * y1 - x1 * y2) / (x2 - x1); // y-intercept

    // Pen and brush for travelling ball
    ball_pen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
    ball_brush = CreateSolidBrush(ball_colour);

    hdc = GetDC(hWindow);

    SelectObject(hdc, ball_pen); // select ball pen colour
    SelectObject(hdc, ball_brush); // select ball brush colour
}

/*
* draws the ball/circle using the apt color
*/

void StraightLine::paint() {
    // draw a dot
    Ellipse(hdc, x, y, x + aWIDTH, y + aHEIGHT);
}

/*
Repeatedly draws ball so as to simulate a continuous motion
*/

void StraightLine::moveInLine() {
    // condition for continuing motion
    while (x + aWIDTH <= window_width) {
        y = (int)ceil(m * x + c);
        paint();

        x += 20;
        // introduce a delay between renderings
        Sleep(50);
    }
}

StraightLine::~StraightLine()
{
    DeleteObject(ball_pen);
    DeleteObject(ball_brush);

    ReleaseDC(hWindow, hdc);
}





<< PreviousNext >>