Why are my local function definitions illegal??? /* Axially Aligned Boxes: Write
ID: 671801 • Letter: W
Question
Why are my local function definitions illegal???
/*
Axially Aligned Boxes:
Write a function that checks if given two axially aligned 2d-boxes (x,y) whether they intersect. You can assume you have the following:
*/
#include <iostream>
#include <string>
using namespace std;
int main(){
// Simple 2D vector class
struct Vec2D
{
float x, y;
};
// 2D axially-aligned bounding box.
struct Box2D
{
Vec2D min, max;
};
bool BoxesIntersect(const Box2D &a, const Box2D &b)
{
if (a.max.x < b.min.x) return false;
if (a.min.x > b.max.x) return false;
if (a.max.y < b.min.y) return false;
if (a.min.y > b.max.y) return false;
return true;
}
return 0;
}
Explanation / Answer
The problem with your code is:
You are trying to define the functions inside the another function(main). You are supposed to define every function individually, and you can call one function from inside the other one.
This syntax will work now:
/*
Axially Aligned Boxes:
Write a function that checks if given two axially aligned 2d-boxes (x,y) whether they intersect. You can assume you have the following:
*/
#include <iostream>
#include <string>
using namespace std;
// Simple 2D vector class
struct Vec2D
{
float x, y;
};
// 2D axially-aligned bounding box.
struct Box2D
{
Vec2D min, max;
};
bool BoxesIntersect(const Box2D &a, const Box2D &b)
{
if (a.max.x < b.min.x) return false;
if (a.min.x > b.max.x) return false;
if (a.max.y < b.min.y) return false;
if (a.min.y > b.max.y) return false;
return true;
}
int main(){
struct Box2D a,b;
/*Sample Values of objects a and b.
a.min.x = 2;
a.max.x = 8;
a.min.y = 0;
a.max.y = 5;
b.min.x = 3;
b.max.x = 9;
b.min.y = 3;
b.max.y = 5;*/
cout<<BoxesIntersect(a,b);
return 0;
}
I tried some sample values for the objects a and b. You can initialize with some other values, or you can read the values for those variables through some other means as per your requirement.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.