small code cleanup
-Intersect struct replaced with std::optional<sf::Vector2f> -names of LineSegmaent members changed to startPoint and endPoint
This commit is contained in:
parent
bc7ef93844
commit
b9ea5bd951
|
|
@ -1,4 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
// a collection of useful funtions that i use across projects
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,6 @@ Pos=60,60
|
||||||
Size=400,400
|
Size=400,400
|
||||||
|
|
||||||
[Window][Config]
|
[Window][Config]
|
||||||
Pos=60,60
|
Pos=7,8
|
||||||
Size=395,254
|
Size=705,277
|
||||||
|
|
||||||
|
|
|
||||||
32
main.cpp
32
main.cpp
|
|
@ -11,13 +11,11 @@
|
||||||
|
|
||||||
struct LineSegment
|
struct LineSegment
|
||||||
{
|
{
|
||||||
sf::Vector2f a{};
|
sf::Vector2f startPoint{};
|
||||||
|
|
||||||
sf::Vector2f b{};
|
sf::Vector2f endPoint{};
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Intersect { bool result{}; sf::Vector2f pos{}; };
|
|
||||||
|
|
||||||
struct imguiColor
|
struct imguiColor
|
||||||
{
|
{
|
||||||
float r{};
|
float r{};
|
||||||
|
|
@ -36,22 +34,22 @@ imguiColor constructImguiColor(sf::Color color)
|
||||||
return imguiColor((float)color.r / 255.f, (float)color.g / 255.f, (float)color.b / 255.f, (float)color.a / 255.f);
|
return imguiColor((float)color.r / 255.f, (float)color.g / 255.f, (float)color.b / 255.f, (float)color.a / 255.f);
|
||||||
}
|
}
|
||||||
|
|
||||||
Intersect lineIntersect(LineSegment first, LineSegment second)
|
std::optional<sf::Vector2f> lineIntersect(LineSegment first, LineSegment second)
|
||||||
{
|
{
|
||||||
sf::Vector2f firstVector = (first.b - first.a);
|
sf::Vector2f firstVector = (first.endPoint - first.startPoint);
|
||||||
sf::Vector2f secondVector = (second.b - second.a);
|
sf::Vector2f secondVector = (second.endPoint - second.startPoint);
|
||||||
|
|
||||||
float lengthCrossProduct = firstVector.cross(secondVector);
|
float lengthCrossProduct = firstVector.cross(secondVector);
|
||||||
|
|
||||||
sf::Vector2f fms = second.a - first.a; //first line start point minus second
|
sf::Vector2f fms = second.startPoint - first.startPoint; //first line start point minus second
|
||||||
|
|
||||||
float firstScalar = fms.cross(secondVector) / lengthCrossProduct;
|
float firstScalar = fms.cross(secondVector) / lengthCrossProduct;
|
||||||
float secondScalar = fms.cross(firstVector) / lengthCrossProduct;
|
float secondScalar = fms.cross(firstVector) / lengthCrossProduct;
|
||||||
|
|
||||||
if ((firstScalar >= 0 and firstScalar <= 1) and (secondScalar >= 0 and secondScalar <= 1))
|
if ((firstScalar >= 0 and firstScalar <= 1) and (secondScalar >= 0 and secondScalar <= 1))
|
||||||
return{ true, sf::Vector2f(first.a.x + (firstScalar * firstVector.x), first.a.y + (firstScalar * firstVector.y)) };
|
return sf::Vector2f(first.startPoint.x + (firstScalar * firstVector.x), first.startPoint.y + (firstScalar * firstVector.y));
|
||||||
else
|
else
|
||||||
return{ false, sf::Vector2f(0,0) };
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
sf::VertexArray constructRectangle(sf::Vector2f center, sf::Vector2f size, sf::Color color = sf::Color::Black)
|
sf::VertexArray constructRectangle(sf::Vector2f center, sf::Vector2f size, sf::Color color = sf::Color::Black)
|
||||||
|
|
@ -137,7 +135,7 @@ int main()
|
||||||
|
|
||||||
{
|
{
|
||||||
float i = 0.f, angleIncrement = 360.f / (float)numRays;
|
float i = 0.f, angleIncrement = 360.f / (float)numRays;
|
||||||
for (int i = 0; i < numRays; i++) rays.push_back(constructRay(mousePos, 5000.f, sf::degrees(angleIncrement * i), rayColor.asSfColor()));
|
for (int i = 0; i < numRays; i++) rays.push_back(constructRay(mousePos, rayLength, sf::degrees(angleIncrement * i), rayColor.asSfColor()));
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<sf::VertexArray> polygons;
|
std::vector<sf::VertexArray> polygons;
|
||||||
|
|
@ -147,7 +145,6 @@ int main()
|
||||||
//main game loop
|
//main game loop
|
||||||
while (window.isOpen())
|
while (window.isOpen())
|
||||||
{
|
{
|
||||||
|
|
||||||
//handle input
|
//handle input
|
||||||
while (const std::optional event = window.pollEvent())
|
while (const std::optional event = window.pollEvent())
|
||||||
{
|
{
|
||||||
|
|
@ -164,7 +161,7 @@ int main()
|
||||||
{
|
{
|
||||||
case sf::Keyboard::Scancode::C:
|
case sf::Keyboard::Scancode::C:
|
||||||
polygons.clear();
|
polygons.clear();
|
||||||
polygons.push_back(constructRectangle(center, center));
|
polygons.push_back(constructRectangle(center, center));//outer bounds of the window
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case sf::Keyboard::Scancode::R:
|
case sf::Keyboard::Scancode::R:
|
||||||
|
|
@ -208,18 +205,17 @@ int main()
|
||||||
|
|
||||||
for (auto& ray : rays)
|
for (auto& ray : rays)
|
||||||
{
|
{
|
||||||
|
|
||||||
sf::Vector2f closestIntersection = ray[1].position;
|
sf::Vector2f closestIntersection = ray[1].position;
|
||||||
Intersect currentIntersect{};
|
std::optional<sf::Vector2f> currentIntersect{};
|
||||||
for (auto& poly : polygons)
|
for (auto& poly : polygons)
|
||||||
{
|
{
|
||||||
for (int i = 1; i <= poly.getVertexCount() - 1; i++) {
|
for (int i = 1; i <= poly.getVertexCount() - 1; i++) {
|
||||||
currentIntersect = lineIntersect({ ray[0].position, ray[1].position }, { poly[i - 1].position , poly[i].position });
|
currentIntersect = lineIntersect({ ray[0].position, ray[1].position }, { poly[i - 1].position , poly[i].position });
|
||||||
if (currentIntersect.result)
|
if (currentIntersect)
|
||||||
{
|
{
|
||||||
if ((closestIntersection - ray[0].position).length() > (currentIntersect.pos - ray[0].position).length())
|
if ((closestIntersection - ray[0].position).length() > (currentIntersect.value() - ray[0].position).length())
|
||||||
{
|
{
|
||||||
closestIntersection = currentIntersect.pos;
|
closestIntersection = currentIntersect.value();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue