diff --git a/Oving 3/ObligatoriskeOppgaver/ObligatoriskeOppgaver.sln b/Oving 3/ObligatoriskeOppgaver/ObligatoriskeOppgaver.sln new file mode 100644 index 0000000..ff9259f --- /dev/null +++ b/Oving 3/ObligatoriskeOppgaver/ObligatoriskeOppgaver.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.271 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ObligatoriskeOppgaver", "ObligatoriskeOppgaver\ObligatoriskeOppgaver.vcxproj", "{B7F91FBF-D3FE-4DEC-BA17-75E818059272}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {B7F91FBF-D3FE-4DEC-BA17-75E818059272}.Debug|x64.ActiveCfg = Debug|x64 + {B7F91FBF-D3FE-4DEC-BA17-75E818059272}.Debug|x64.Build.0 = Debug|x64 + {B7F91FBF-D3FE-4DEC-BA17-75E818059272}.Debug|x86.ActiveCfg = Debug|Win32 + {B7F91FBF-D3FE-4DEC-BA17-75E818059272}.Debug|x86.Build.0 = Debug|Win32 + {B7F91FBF-D3FE-4DEC-BA17-75E818059272}.Release|x64.ActiveCfg = Release|x64 + {B7F91FBF-D3FE-4DEC-BA17-75E818059272}.Release|x64.Build.0 = Release|x64 + {B7F91FBF-D3FE-4DEC-BA17-75E818059272}.Release|x86.ActiveCfg = Release|Win32 + {B7F91FBF-D3FE-4DEC-BA17-75E818059272}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {463F8BFA-463F-4621-8174-AEBED4A9F6DB} + EndGlobalSection +EndGlobal diff --git a/Oving 3/ObligatoriskeOppgaver/ObligatoriskeOppgaver/ObligatoriskeOppgaver.vcxproj b/Oving 3/ObligatoriskeOppgaver/ObligatoriskeOppgaver/ObligatoriskeOppgaver.vcxproj new file mode 100644 index 0000000..6535ae6 --- /dev/null +++ b/Oving 3/ObligatoriskeOppgaver/ObligatoriskeOppgaver/ObligatoriskeOppgaver.vcxproj @@ -0,0 +1,148 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {b7f91fbf-d3fe-4dec-ba17-75e818059272} + ObligatoriskeOppgaver + 10.0.17763.0 + + + + Application + true + v141 + MultiByte + + + Application + false + v141 + true + MultiByte + + + Application + true + v141 + MultiByte + + + Application + false + v141 + true + MultiByte + + + + + + + + + + + + + + + + + + + + + C:\Program Files\TDT4102\includes;$(IncludePath) + + + C:\Program Files\TDT4102\includes;$(IncludePath) + + + C:\Program Files\TDT4102\includes;$(IncludePath) + + + C:\Program Files\TDT4102\includes;$(IncludePath) + + + + Level3 + Disabled + true + true + + + Console + + + + + Level3 + Disabled + true + true + + + Console + + + + + Level3 + MaxSpeed + true + true + true + true + + + true + true + Console + + + + + Level3 + MaxSpeed + true + true + true + true + + + true + true + Console + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Oving 3/ObligatoriskeOppgaver/ObligatoriskeOppgaver/ObligatoriskeOppgaver.vcxproj.filters b/Oving 3/ObligatoriskeOppgaver/ObligatoriskeOppgaver/ObligatoriskeOppgaver.vcxproj.filters new file mode 100644 index 0000000..abf6680 --- /dev/null +++ b/Oving 3/ObligatoriskeOppgaver/ObligatoriskeOppgaver/ObligatoriskeOppgaver.vcxproj.filters @@ -0,0 +1,36 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + Source Files + + + + + Source Files + + + Source Files + + + \ No newline at end of file diff --git a/Oving 3/ObligatoriskeOppgaver/ObligatoriskeOppgaver/cannonball.cpp b/Oving 3/ObligatoriskeOppgaver/ObligatoriskeOppgaver/cannonball.cpp new file mode 100644 index 0000000..8aa2d28 --- /dev/null +++ b/Oving 3/ObligatoriskeOppgaver/ObligatoriskeOppgaver/cannonball.cpp @@ -0,0 +1,118 @@ +#include "cannonball.h" +#include "std_lib_facilities.h" +#define _USE_MATH_DEFINES +#include +#include "utilities.h" + +double acclY() +{ + return -9.81; +} + +double velY(double initVelocityY, double time) +{ + return initVelocityY + acclY() * time; +} + +double posY(double initPosition, double initVelocity, double time) +{ + return initPosition + initVelocity * time + (acclY() *pow(time, 2)) / 2; +} + +double posX(double initPosition, double initVelocity, double time) +{ + return initPosition + initVelocity * time; +} + +void printTime(double time) +{ + int minSec = 60; + int hourSec = pow(minSec, 2); + int hours = time / hourSec; + int timeLeft = time - hours * hourSec; + int minutes = timeLeft / minSec; + timeLeft = timeLeft - minutes * minSec; + cout << hours << " hours, " << minutes << " minutes, " << timeLeft << " seconds" << "\n"; +} + +double flightTime(double initVelocityY) +{ + return (-2 * initVelocityY) / acclY(); +} + +void getUserInput(double& theta, double& absVelocity) +{ + double deg; + cout << "Angle of velocity: "; + cin >> deg; + theta = degToRad(deg); + cout << "Velocity: "; + cin >> absVelocity; +} + +double degToRad(double deg) +{ + return deg * M_PI / 180; +} + +double getVelocityX(double theta, double absVelocity) +{ + return absVelocity * cos(theta); +} + +double getVelocityY(double theta, double absVelocity) +{ + return absVelocity * sin(theta); +} + +void getVelocityVector(double theta, double absVelocity, double& velocityX, double& velocityY) +{ + velocityX = getVelocityX(theta, absVelocity); + velocityY = getVelocityY(theta, absVelocity); +} + +double getDistanceTraveled(double velocityX, double velocityY) +{ + return posX(0, velocityX, flightTime(velocityY)); +} + +double targetPractice(double distanceToTarget, double velocityX, double velocityY) +{ + return abs(distanceToTarget - getDistanceTraveled(velocityX, velocityY)); +} + +void playTargetPractice(int rounds) +{ + srand(static_cast(time(nullptr))); + bool won = false; + double theta, absVelocity; // Initial angle and velocity + double initX, initY; // Initial velocities, in component-form + double fTime; + double distance; + int target = randomWithLimits(100, 1000); + for (int r = 1; r <= rounds; ++r) + { + getUserInput(theta, absVelocity); + getVelocityVector(theta, absVelocity, initX, initY); + printTime(flightTime(initY)); + cout << "The cannonball flew " << getDistanceTraveled(initX, initY) << " meters" << endl; + distance = targetPractice(target, initX, initY); + cout << "The cannonball is " << distance << " meters away from target" << endl; + if (distance <= 5) + { + won = true; + break; + } + else + { + if (distance < 0) + cout << "The shot wat too short" << endl; + else + cout << "The shot was to far" << endl; + } + } + if (won) + cout << "Congratulate, you have won!" << endl; + else + cout << "Sorry, game over!" << endl; +} diff --git a/Oving 3/ObligatoriskeOppgaver/ObligatoriskeOppgaver/cannonball.h b/Oving 3/ObligatoriskeOppgaver/ObligatoriskeOppgaver/cannonball.h new file mode 100644 index 0000000..fde1fed --- /dev/null +++ b/Oving 3/ObligatoriskeOppgaver/ObligatoriskeOppgaver/cannonball.h @@ -0,0 +1,35 @@ +#pragma once + +#pragma region Task 1 and 2 +double acclY(); + +double velY(double initVelocityY, double time); + +double posY(double initPosition, double initVelocity, double time); +double posX(double initPosition, double initVelocity, double time); + +void printTime(double time); + +double flightTime(double initVelocityY); +#pragma endregion + + +#pragma region Task 4 +void getUserInput(double& theta, double& absVelocity); + +double degToRad(double deg); + +double getVelocityX(double theta, double absVelocity); +double getVelocityY(double theta, double absVelocity); + +void getVelocityVector(double theta, double absVelocity, double& velocityX, double& velocityY); + +// Takes in initial velocity in x and y and gives the traveled distance in the x-axis +double getDistanceTraveled(double velocityX, double velocityY); + +double targetPractice(double distanceToTarget, double velocityX, double velocityY); + +#pragma endregion + +void playTargetPractice(int rounds); + diff --git a/Oving 3/ObligatoriskeOppgaver/ObligatoriskeOppgaver/main.cpp b/Oving 3/ObligatoriskeOppgaver/ObligatoriskeOppgaver/main.cpp new file mode 100644 index 0000000..bec507a --- /dev/null +++ b/Oving 3/ObligatoriskeOppgaver/ObligatoriskeOppgaver/main.cpp @@ -0,0 +1,45 @@ +#include "std_lib_facilities.h" +#include "cannonball.h" +#include "utilities.h" + +bool testDeviation(double compareOperant, double toOperand, double maxError, string name); + +int main(){ + +#pragma region Testing - Task 3 + /* + int space = 8; + for (double i = 0; i <= 5; i += 2.5) { + cout << setw(space) << acclY() << setw(space) << velY(25, i) << setw(space) << + posY(0, 25, i) << setw(space) << posX(0, 50, i) << endl; + } + */ +#pragma endregion + +#pragma region Task 4d + /* + double theta; + double absVelocity; + getUserInput(theta, absVelocity); + double initX, initY; + getVelocityVector(theta, absVelocity, initX, initY); + cout << "Distance traveled: " << getDistanceTraveled(initX, initY) << endl; + cout << "Distance from target: " << targetPractice(10, initX, initY) << endl; + */ +#pragma endregion + +#pragma region Game - Task 5d + + playTargetPractice(10); + +#pragma endregion + + return 0; +} + +bool testDeviation(double compareOperant, double toOperand, double maxError, string name) +{ + if (abs(compareOperant - toOperand) < maxError) + return true; + return false; +} diff --git a/Oving 3/ObligatoriskeOppgaver/ObligatoriskeOppgaver/utilities.cpp b/Oving 3/ObligatoriskeOppgaver/ObligatoriskeOppgaver/utilities.cpp new file mode 100644 index 0000000..04c548f --- /dev/null +++ b/Oving 3/ObligatoriskeOppgaver/ObligatoriskeOppgaver/utilities.cpp @@ -0,0 +1,8 @@ +#include "utilities.h" +#include "std_lib_facilities.h" + +int randomWithLimits(int min, int max) +{ + return rand() % (max - min + 1) + min;; +} + diff --git a/Oving 3/ObligatoriskeOppgaver/ObligatoriskeOppgaver/utilities.h b/Oving 3/ObligatoriskeOppgaver/ObligatoriskeOppgaver/utilities.h new file mode 100644 index 0000000..42d114f --- /dev/null +++ b/Oving 3/ObligatoriskeOppgaver/ObligatoriskeOppgaver/utilities.h @@ -0,0 +1,6 @@ +#pragma once + +int randomWithLimits(int min, int max); + + + diff --git a/Oving 3/Øving_3(24.jan).pdf b/Oving 3/Øving_3(24.jan).pdf new file mode 100644 index 0000000..4c80498 Binary files /dev/null and b/Oving 3/Øving_3(24.jan).pdf differ