Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Graphics/Graphics.cpp
- Committer:
- HenryWTriff
- Date:
- 2020-02-11
- Revision:
- 2:d08b6a1eaf2b
- Child:
- 3:ceed6d026b8b
File content as of revision 2:d08b6a1eaf2b:
#include "Graphics.h"
//**************************
// MAIN GRAPHICS FUNCTION
//**************************
void Graphics::Draw_Map(int game_fps, Point_2D translation, int angle, float squish, Line_2D *Track_Lines, N5110 &LCD)
{
Line_2D Track_Lines_Transformed[4];
for(int i = 0; i < 4; i++) {
Track_Lines_Transformed[i] = Track_Lines[i];
//Translation
Track_Lines_Transformed[i].from = Translate_Point(Track_Lines_Transformed[i].from, Round(translation.x), Round(translation.y));
Track_Lines_Transformed[i].to = Translate_Point(Track_Lines_Transformed[i].to, Round(translation.x), Round(translation.y));
//Rotation
Track_Lines_Transformed[i].from = Rotate_Point(Track_Lines_Transformed[i].from, angle);
Track_Lines_Transformed[i].to = Rotate_Point(Track_Lines_Transformed[i].to, angle);
//Squish
Track_Lines_Transformed[i].from = Squish_Point(Track_Lines_Transformed[i].from, squish);
Track_Lines_Transformed[i].to = Squish_Point(Track_Lines_Transformed[i].to, squish);
}
LCD.clear();
for(int i = 0; i < 4; i++) {
Graphics_Draw_Line(Track_Lines_Transformed[i].from, Track_Lines_Transformed[i].to, true, LCD);
}
LCD.refresh();
wait(0.02);
}
//*******************
// SCREEN SETTINGS
//*******************
void Graphics::Change_Contrast(N5110 &LCD, Gamepad &Device)
{
LCD.setContrast((0.35 + 0.2 * Device.read_pot1()));
}
//**************
// TRANSFORM
//**************
//ROTATE
Point_2D Graphics::Rotate_Point(Point_2D point, float angle)
{
angle = (angle * PI)/180;
float x_rotated = point.x * cos(angle) - point.y * sin(angle);
float y_rotated = point.y * cos(angle) + point.x * sin(angle);
return {x_rotated, y_rotated};
}
//TRANSLATE
Point_2D Graphics::Translate_Point(Point_2D point, int translate_x, int translate_y)
{
float x_translated = point.x - translate_x;
float y_translated = point.y - translate_y;
return {x_translated, y_translated};
}
//SQUISH
Point_2D Graphics::Squish_Point(Point_2D point, float squish)
{
float x_squish = point.x;
float y_squish = point.y * squish;
return {x_squish, y_squish};
}
//********
// MATH
//********
int Graphics::Round(float number)
{
int number_int = (number * 10);
int remainder = number_int % 10;
if(remainder < 5) {
return ((number_int - remainder) / 10);
} else {
return ((number_int + (10 - remainder)) / 10);
}
}
float Graphics::Gradient(Point_2D from, Point_2D to)
{
float change_in_y = to.y - from.y;
float change_in_x = to.x - from.x;
float gradient = change_in_y / change_in_x;
if(gradient < 0.001 && gradient > -0.001) {
return 0;
} else {
return gradient;
}
}
bool Graphics::Gradient_Check_Infinate(Point_2D from, Point_2D to)
{
float change_in_x = to.x - from.x;
if(change_in_x < 0.001 && change_in_x > -0.001) {
return true;
} else {
return false;
}
}
//********
// DRAW
//********
void Graphics::Graphics_Draw_Line(Point_2D from, Point_2D to, bool solid, N5110 &LCD) //Draw a line between two points on only the portion of the screen
{
if( Gradient_Check_Infinate(from, to) == false ) { //Checking to see if the line is vertical
Point_2D plot_x_from = {0,0};
Point_2D plot_x_to = {0,0};
Point_2D plot_y_from = {0,0};
Point_2D plot_y_to = {0,0};
float gradient = Gradient(from, to); // Calculating the gradient
float y_intercept = (from.y - gradient * from.x); // Calulating the y intercept y - mx = c
float x_intercept = (from.x - from.y / gradient); // Calculating the x intercept x - y/m = d
if(gradient <= 1 && gradient >= -1) {
//Reordering from and to so that the for loops below can use ++
if(to.x < from.x) {
plot_x_from = to;
plot_x_to = from;
} else {
plot_x_from = from;
plot_x_to = to;
}
if(solid == true) {
for(int x = Round(plot_x_from.x); x <= Round(plot_x_to.x); x++) { //Iterating through the x points
int y = -(Round((gradient * x) + y_intercept)); //Calculating the value of y for each x point
Point_2D plot_x = {x+42, y+36}; //Assigning them to a Plot_2D variable and transforming so the centre of the screen is 0,0
if(plot_x.x <= 84 && plot_x.x >= 0 && plot_x.y <=48 && plot_x.y >= 0) {
LCD.setPixel(plot_x.x, plot_x.y, true); //Plotting the points
}
}
} else {
for(int x = Round(plot_x_from.x); x <= Round(plot_x_to.x); x+=2) { //Iterating through the x points
int y = -(Round((gradient * x) + y_intercept)); //Calculating the value of y for each x point
Point_2D plot_x = {x+42, y+36}; //Assigning them to a Plot_2D variable and transforming so the centre of the screen is 0,0
if(plot_x.x <= 84 && plot_x.x >= 0 && plot_x.y <=48 && plot_x.y >= 0) {
LCD.setPixel(plot_x.x, plot_x.y, true); //Plotting the points
}
}
}
} else {
if(to.y < from.y) {
plot_y_from = to;
plot_y_to = from;
} else {
plot_y_from = from;
plot_y_to = to;
}
if(solid == true) {
for(int y = Round(plot_y_from.y); y <= Round(plot_y_to.y); y++) { //Iterating through the Y points
int x = Round((y / gradient) + x_intercept); //Calculating the value of x for every y point
Point_2D plot_y = {x+42, (-y)+36}; //Assigning them to a Plot_2D variable and transforming so the centre of the screen is 0,0
if(plot_y.x <= 84 && plot_y.x >= 0 && plot_y.y <=48 && plot_y.y >= 0) {
LCD.setPixel(plot_y.x, plot_y.y, true); //Plotting the points
}
}
} else {
for(int y = Round(plot_y_from.y); y <= Round(plot_y_to.y); y+=2) { //Iterating through the Y points
int x = Round((y / gradient) + x_intercept); //Calculating the value of x for every y point
Point_2D plot_y = {x+42, (-y)+36}; //Assigning them to a Plot_2D variable and transforming so the centre of the screen is 0,0
if(plot_y.x <= 84 && plot_y.x >= 0 && plot_y.y <=48 && plot_y.y >= 0) {
LCD.setPixel(plot_y.x, plot_y.y, true); //Plotting the points
}
}
}
}
} else {
Point_2D plot_y_from = {0,0};
Point_2D plot_y_to = {0,0};
//Reordering from and to so that the for loops below can use ++
if(to.y < from.y) {
plot_y_from = to;
plot_y_to = from;
} else {
plot_y_from = from;
plot_y_to = to;
}
float x_intercept = from.x; //Calculating the x_intercept
if(solid == true) {
for(int y = plot_y_from.y; y <= plot_y_to.y; y++) {
Point_2D plot_y = {x_intercept+42, (-y)+36}; //Assigning them to a Plot_2D variable and transforming so the centre of the screen is 0,0
LCD.setPixel(plot_y.x, plot_y.y, true); //Plotting the points
}
} else {
for(int y = plot_y_from.y; y <= plot_y_to.y; y+=2) {
Point_2D plot_y = {x_intercept+42, (-y)+36}; //Assigning them to a Plot_2D variable and transforming so the centre of the screen is 0,0
LCD.setPixel(plot_y.x, plot_y.y, true); //Plotting the points
}
}
}
}