Ray-casting algorithm: Difference between revisions
Content added Content deleted
(C++ snippet) |
(enhancements for C++) |
||
Line 594: | Line 594: | ||
#include <iostream> |
#include <iostream> |
||
#include <limits> |
#include <limits> |
||
#include <vector> |
|||
using namespace std; |
using namespace std; |
||
Line 620: | Line 619: | ||
}; |
}; |
||
template <unsigned int SIZE> |
|||
struct Figure { |
struct Figure { |
||
string name; |
string name; |
||
Edge edges |
initializer_list<Edge> edges; |
||
bool contains(const Point& p) const |
bool contains(const Point& p) const |
||
{ |
{ |
||
auto c = 0; |
auto c = 0; |
||
for (auto |
for (auto e : edges) |
||
if ( |
if (e(p)) c++; |
||
return c % 2 != 0; |
return c % 2 != 0; |
||
} |
} |
||
Line 645: | Line 643: | ||
int main() |
int main() |
||
{ |
{ |
||
initializer_list<Point> points = { { 5.0, 5.0}, {5.0, 8.0}, {-10.0, 5.0}, {0.0, 5.0}, {10.0, 5.0}, {8.0, 5.0}, {10.0, 10.0} }; |
|||
Figure |
Figure square = { "Square", |
||
{ {{0.0, 0.0}, {10.0, 0.0}}, {{10.0, 0.0}, {10.0, 10.0}}, {{10.0, 10.0}, {0.0, 10.0}}, {{0.0, 10.0}, {0.0, 0.0}} } |
{ {{0.0, 0.0}, {10.0, 0.0}}, {{10.0, 0.0}, {10.0, 10.0}}, {{10.0, 10.0}, {0.0, 10.0}}, {{0.0, 10.0}, {0.0, 0.0}} } |
||
}; |
}; |
||
⚫ | |||
Figure |
Figure square_hole = { "Square hole", |
||
{ {{0.0, 0.0}, {10.0, 0.0}}, {{10.0, 0.0}, {10.0, 10.0}}, {{10.0, 10.0}, {0.0, 10.0}}, {{0.0, 10.0}, {0.0, 0.0}}, |
{ {{0.0, 0.0}, {10.0, 0.0}}, {{10.0, 0.0}, {10.0, 10.0}}, {{10.0, 10.0}, {0.0, 10.0}}, {{0.0, 10.0}, {0.0, 0.0}}, |
||
{{2.5, 2.5}, {7.5, 2.5}}, {{7.5, 2.5}, {7.5, 7.5}}, {{7.5, 7.5}, {2.5, 7.5}}, {{2.5, 7.5}, {2.5, 2.5}} |
{{2.5, 2.5}, {7.5, 2.5}}, {{7.5, 2.5}, {7.5, 7.5}}, {{7.5, 7.5}, {2.5, 7.5}}, {{2.5, 7.5}, {2.5, 2.5}} |
||
} |
} |
||
}; |
}; |
||
square_hole.check(points, cout); |
|||
Figure |
Figure strange = { "Strange", |
||
{ {{0.0, 0.0}, {2.5, 2.5}}, {{2.5, 2.5}, {0.0, 10.0}}, {{0.0, 10.0}, {2.5, 7.5}}, {{2.5, 7.5}, {7.5, 7.5}}, |
{ {{0.0, 0.0}, {2.5, 2.5}}, {{2.5, 2.5}, {0.0, 10.0}}, {{0.0, 10.0}, {2.5, 7.5}}, {{2.5, 7.5}, {7.5, 7.5}}, |
||
{{7.5, 7.5}, {10.0, 10.0}}, {{10.0, 10.0}, {10.0, 0.0}}, {{10.0, 0}, {2.5, 2.5}} |
{{7.5, 7.5}, {10.0, 10.0}}, {{10.0, 10.0}, {10.0, 0.0}}, {{10.0, 0}, {2.5, 2.5}} |
||
} |
} |
||
}; |
}; |
||
strange.check(points, cout); |
|||
Figure |
Figure exagon = { "Exagon", |
||
{ {{3.0, 0.0}, {7.0, 0.0}}, {{7.0, 0.0}, {10.0, 5.0}}, {{10.0, 5.0}, {7.0, 10.0}}, {{7.0, 10.0}, {3.0, 10.0}}, |
{ {{3.0, 0.0}, {7.0, 0.0}}, {{7.0, 0.0}, {10.0, 5.0}}, {{10.0, 5.0}, {7.0, 10.0}}, {{7.0, 10.0}, {3.0, 10.0}}, |
||
{{3.0, 10.0}, {0.0, 5.0}}, {{0.0, 5.0}, {3.0, 0.0}} |
{{3.0, 10.0}, {0.0, 5.0}}, {{0.0, 5.0}, {3.0, 0.0}} |
||
} |
} |
||
}; |
}; |
||
exagon.check(points, cout); |
|||
for(auto f : {square, square_hole, strange, exagon}) |
|||
⚫ | |||
return EXIT_SUCCESS; |
return EXIT_SUCCESS; |
||
} |
|||
</lang> |
|||
{{output}} |
{{output}} |
||
As D. |
As D. |