Facet class file
#include "stdafx.h"
#include "Facet.h"
#include "QuadraticRegion.h"
QuadraticRegion* qregion;
Facet::Facet(HWND hWnd, int window_width, int window_height)
{
qregion = new QuadraticRegion(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() {
qregion->paint();
}
bool Facet::actionPerformed(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
switch (LOWORD(wParam))
{
case 12321:
qregion->checkBoundary();
return TRUE;
default:
return FALSE;
}
}
Facet::~Facet()
{
delete qregion;
}
QuadraticRegion Header file
#pragma once
#define aWIDTH 5
#define aHEIGHT 5
#define ballWIDTH 100
#define ballHEIGHT 100
class QuadraticRegion
{
public:
QuadraticRegion(HWND, int, int);
virtual ~QuadraticRegion();
void paint();
void checkBoundary();
protected:
HWND hWindow;
HDC hdc;
int window_width;
int window_height;
COLORREF ball_colour;
int x_ball;
int y_ball;
int previous_x;
int previous_y;
int xq_start;
int yq_start;
int xq_min;
int yq_min;
double xq_lb;
double xq_ub;
int xq_stop;
int x;
int y;
double a, b, c;
double discriminant;
HPEN background_pen;
HBRUSH background_brush;
HPEN ball_pen;
HBRUSH ball_brush;
HBRUSH curve_brush;
};
C++ code for QuadraticRegion Class file
#include "stdafx.h"
#include "QuadraticRegion.h"
#include <math.h>
QuadraticRegion::QuadraticRegion(HWND hWnd, int window_width, int window_height)
{
hWindow = hWnd;
this->window_width = window_width;
this->window_height = window_height;
ball_colour = RGB(255, 255, 0);
x_ball = 50;
y_ball = 200;
previous_x = x_ball;
previous_y = y_ball;
xq_start = 300;
yq_start = 150;
xq_min = 450;
yq_min = 400;
xq_stop = 600;
x = xq_start;
y = yq_start;
a = (double)(yq_start - yq_min) / pow((xq_start - xq_min), 2);
b = -2 * a * xq_min;
c = yq_min + a * pow(xq_min, 2);
discriminant = sqrt(b * b - 4 * a * (c - (y_ball + (double)(ballHEIGHT / 2))));
if (a < 0) {
xq_lb = (double)(-b + discriminant) / (2 * a);
xq_ub = (double)(-b - discriminant) / (2 * a);
}
else {
xq_lb = (double)(-b - discriminant) / (2 * a);
xq_ub = (double)(-b + discriminant) / (2 * a);
}
background_pen = CreatePen(PS_SOLID, 1, RGB(192, 192, 192));
background_brush = CreateSolidBrush(RGB(192, 192, 192));
ball_pen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
ball_brush = CreateSolidBrush(ball_colour);
curve_brush = CreateSolidBrush(RGB(0, 0, 0));
hdc = GetDC(hWindow);
}
void QuadraticRegion::paint() {
SelectObject(hdc, ball_pen);
SelectObject(hdc, curve_brush);
for (; x < xq_stop; x++) {
y = (int)round(a * x * x + b * x + c);
Ellipse(hdc, x, y, x + aWIDTH, y + aHEIGHT);
}
x = xq_start;
SelectObject(hdc, background_pen);
SelectObject(hdc, background_brush);
Ellipse(hdc, previous_x, previous_y, previous_x + ballWIDTH, previous_y + ballHEIGHT);
SelectObject(hdc, ball_pen);
SelectObject(hdc, ball_brush);
Ellipse(hdc, x_ball, y_ball, x_ball + ballWIDTH, y_ball + ballHEIGHT);
previous_x = x_ball;
previous_y = y_ball;
}
void QuadraticRegion::checkBoundary() {
while (x_ball + ballWIDTH <= window_width) {
if ((x_ball <= xq_lb && x_ball + ballWIDTH >= xq_lb)
|| (x_ball <= xq_ub && x_ball + ballWIDTH >= xq_ub)) {
ball_colour = RGB(255, 0, 0);
}
else if (x_ball >= xq_lb && x_ball + ballWIDTH <= xq_ub) {
ball_colour = RGB(0, 255, 0);
}
else {
ball_colour = RGB(255, 255, 0);
}
DeleteObject(ball_brush);
ball_brush = CreateSolidBrush(ball_colour);
paint();
x_ball += 5;
Sleep(50);
}
}
QuadraticRegion::~QuadraticRegion()
{
DeleteObject(background_pen);
DeleteObject(background_brush);
DeleteObject(ball_pen);
DeleteObject(ball_brush);
DeleteObject(curve_brush);
ReleaseDC(hWindow, hdc);
}