Skip to content

Instantly share code, notes, and snippets.

@kim366
Created June 15, 2018 13:03
Show Gist options
  • Select an option

  • Save kim366/bdd0b3ebce4be0eb3be989d569264987 to your computer and use it in GitHub Desktop.

Select an option

Save kim366/bdd0b3ebce4be0eb3be989d569264987 to your computer and use it in GitHub Desktop.
sf::Vector2f put_point_on_line(sf::Vector2f point, sf::Vector2f line_point0, sf::Vector2f line_point1)
{
const double new_point_x
{
(
point.x * std::pow(line_point0.x - line_point1.x, 2)
+
(
point.y * (line_point0.x - line_point1.x)
- line_point0.x * line_point1.y
+ line_point0.y * line_point1.x
)
* (line_point0.y - line_point1.y)
)
/
(
std::pow(line_point0.x, 2)
- 2 * line_point0.x * line_point1.x
+ std::pow(line_point0.y, 2)
- 2 * line_point0.y * line_point1.y
+ std::pow(line_point1.x, 2)
+ std::pow(line_point1.y, 2)
)
};
const double new_point_y
{
(
(
line_point0.y - line_point1.y
)
* new_point_x
+ line_point0.x * line_point1.y
- line_point0.y * line_point1.x
)
/
(
line_point0.x - line_point1.x
)
};
return {static_cast<float>(new_point_x), static_cast<float>(new_point_y)};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment