1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
#include <iostream>
#include <math.h>
using namespace std;
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include "SDL_Helper.h"
int limitAngle(int angle_arg);
int main(int argc, char* argv[])
{
// Point Structure
struct Point_w_Dir
{
double x;
double y;
int a;
};
// Window Constants
const int SCREEN_W = 800;
const int SCREEN_H = 600;
const string WINDOW_NAME = "Car and Goal";
// Math Constants
const double deg2rad = acos(0) / 90;
// Program Variables.
Point_w_Dir car = {100, 100, 0};
Point_w_Dir goal = {600, 250, 300};
// SDL window and renderer
SDL_Renderer *renderer;
SDL_Window* window;
// SDL input and end program variables
SDL_Event e;
bool quit = false;
// Initialize SDL and TTF.
init_SDL(renderer, window, SCREEN_W, SCREEN_H, WINDOW_NAME);
// Create font.
TTF_Font *font = TTF_OpenFont("arial_black.ttf", 18);
// Main loop.
while (!quit)
{
// Check for input.
while (SDL_PollEvent(&e))
{
switch(e.type)
{
case SDL_QUIT:
quit = true;
break;
default:
break;
}
}
// Increase angle and keep it between 0 and 359
car.a++;
car.a = limitAngle(car.a);
// Calculate car angle in degrees
double car_radians = car.a * deg2rad;
// Build text strings for output.
string car_angle = "Car angle: " + to_string(car.a) +
"\xB0";
// Calculate slope for the car, but prevent undefined.
double car_m;
if (car.a == 90 || car.a == 270)
car_m = 9999;
else
car_m = tan(car_radians);
// Calculate slope for the goal, but prevent undefined.
double goal_m;
if (goal.a == 90 || goal.a == 270)
goal_m = 9999;
else
goal_m = tan(goal.a * deg2rad);
double car_b = car.y - (car.x * car_m);
double goal_b = goal.y - (goal.x * goal_m);
// Calculate the intercept points
double x_intercept = (goal_b - car_b) / (car_m - goal_m);
double y_intercept = car_m * x_intercept + car_b;
// Clear the background.
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
// Draw goal
SDL_SetRenderDrawColor(renderer, 120, 120, 255, 255);
SDL_RenderDrawPoint(renderer, goal.x, goal.y);
// Draw car
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderDrawPoint(renderer, car.x, car.y);
// Draw lines from car and goal to intersection point.
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
SDL_RenderDrawLine(renderer, goal.x, goal.y, x_intercept, y_intercept);
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
SDL_RenderDrawLine(renderer, car.x, car.y, x_intercept, y_intercept);
// Draw text.
draw_text(renderer, font, car_angle, 20, SCREEN_H - 38);
// Draw everything to screen.
SDL_RenderPresent(renderer);
}
// Destroy Font
TTF_CloseFont(font);
font = NULL;
// Close down SDL and TTF.
stop_SDL(renderer, window);
return 0;
}
// Limit angle from 0 to 359 degrees.
int limitAngle(int angle_arg)
{
if (angle_arg >= 360.0)
return (angle_arg - 360.0);
else if (angle_arg < 0.0)
return (angle_arg + 360.0);
else
return angle_arg;
}
|