add extend vector function, 2D endpoint function, input check on wrap point
This commit is contained in:
parent
772c5482dd
commit
c0852ba04f
|
@ -138,10 +138,15 @@ glm::vec2 Segment::center() const
|
|||
return subsegments(2)[0].end_;
|
||||
}
|
||||
|
||||
glm::vec2 Segment::step(float speed) const
|
||||
float Segment::angle() const
|
||||
{
|
||||
float angle = glm::atan(end_.x - start_.x, end_.y - start_.y);
|
||||
return glm::vec2(speed * glm::sin(angle), speed * glm::cos(angle));
|
||||
return glm::atan(end_.x - start_.x, end_.y - start_.y);
|
||||
}
|
||||
|
||||
glm::vec2 Segment::step(float distance) const
|
||||
{
|
||||
float angle = this->angle();
|
||||
return glm::vec2(distance * glm::sin(angle), distance * glm::cos(angle));
|
||||
}
|
||||
|
||||
glm::vec2 Segment::step_relative(float relative_length_per_step) const
|
||||
|
|
|
@ -44,7 +44,18 @@ public:
|
|||
Box box() const;
|
||||
void move(const glm::vec2&);
|
||||
glm::vec2 center() const;
|
||||
glm::vec2 step(float) const;
|
||||
|
||||
/*!
|
||||
* @return angle in radians between the start and end points
|
||||
*/
|
||||
float angle() const;
|
||||
|
||||
/*!
|
||||
* @param distance length of the step
|
||||
* @return a 2D vector representing a step along the segment at the given distance
|
||||
*/
|
||||
glm::vec2 step(float distance) const;
|
||||
|
||||
glm::vec2 step_relative(float) const;
|
||||
std::vector<Segment> subsegments(int) const;
|
||||
inline bool operator<(const Segment& segment) const { return operator<(segment.length()); }
|
||||
|
|
|
@ -226,6 +226,18 @@ namespace sb
|
|||
return range_percent_map;
|
||||
}
|
||||
|
||||
/*!
|
||||
* Extend a vector's contents in-place by another vector's contents. The vectors must contain identical types.
|
||||
*
|
||||
* @param vector_to_extend vector that will be edited to include the contents of the second vector
|
||||
* @param incoming_vector vector of values to append to existing vector
|
||||
*/
|
||||
template <typename VectorType>
|
||||
void extend(std::vector<VectorType>& vector_to_extend, const std::vector<VectorType>& incoming_vector)
|
||||
{
|
||||
vector_to_extend.insert(vector_to_extend.end(), incoming_vector.begin(), incoming_vector.end());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int SDL_SetRenderDrawColor(SDL_Renderer*, const Color&);
|
||||
|
|
10
src/math.cpp
10
src/math.cpp
|
@ -15,11 +15,6 @@ glm::vec2 sb::velocity_to_delta(float angle, float magnitude)
|
|||
return {glm::sin(angle) * magnitude, glm::cos(angle) * magnitude};
|
||||
}
|
||||
|
||||
glm::vec2 sb::velocity_to_delta(glm::vec2 velocity)
|
||||
{
|
||||
return velocity_to_delta(velocity.x, velocity.y);
|
||||
}
|
||||
|
||||
float sb::angle_between(glm::vec2 start, glm::vec2 end)
|
||||
{
|
||||
return glm::atan(end.x - start.x, end.y - start.y);
|
||||
|
@ -35,6 +30,11 @@ float sb::angle_ratio(float start, float end)
|
|||
return sb::angle_difference(start, end) / glm::pi<float>();
|
||||
}
|
||||
|
||||
glm::vec2 sb::endpoint(glm::vec2 start, float angle, float magnitude)
|
||||
{
|
||||
return start + velocity_to_delta(angle, magnitude);
|
||||
}
|
||||
|
||||
std::vector<glm::vec2> sb::bezier(const std::vector<glm::vec2>& control, int resolution)
|
||||
{
|
||||
std::vector<glm::vec2> points;
|
||||
|
|
29
src/math.hpp
29
src/math.hpp
|
@ -70,11 +70,6 @@ namespace sb
|
|||
*/
|
||||
glm::vec2 velocity_to_delta(float angle, float magnitude);
|
||||
|
||||
/*!
|
||||
* @overload glm::vec2 velocity_to_delta(float angle, float magnitude)
|
||||
*/
|
||||
glm::vec2 velocity_to_delta(glm::vec2 velocity);
|
||||
|
||||
/*!
|
||||
* Get the angle between two vectors, or the angle the first would rotate to to point toward the second.
|
||||
*
|
||||
|
@ -105,6 +100,18 @@ namespace sb
|
|||
*/
|
||||
float angle_ratio(float start, float end);
|
||||
|
||||
/*!
|
||||
* Get the end point of a move given a start point, angle, and magnitude.
|
||||
*
|
||||
* This gives the same result as start + velocity_to_delta(float angle, float magnitude).
|
||||
*
|
||||
* @param start a 2D vertex representing the starting point of this step
|
||||
* @param angle an angle in radians representing the direction of the step
|
||||
* @param magnitude a float representing the speed or distance to travel in this step
|
||||
* @return a 2D vertex representing the end point of this step
|
||||
*/
|
||||
glm::vec2 endpoint(glm::vec2 start, float angle, float magnitude);
|
||||
|
||||
/*!
|
||||
* Calculate a 2D bezier curve from four 2D control points.
|
||||
*
|
||||
|
@ -137,7 +144,17 @@ namespace sb
|
|||
const glm::vec<dimensions, Type, qualifier>& clip_lower,
|
||||
const glm::vec<dimensions, Type, qualifier>& clip_upper)
|
||||
{
|
||||
return glm::mod(vertex - clip_upper, clip_upper - clip_lower) + clip_lower;
|
||||
/* If any clip dimension is zero, throw an error because it will cause NaN to appear in the output. */
|
||||
glm::vec<dimensions, Type, qualifier> clip_delta = clip_upper - clip_lower;
|
||||
for (glm::length_t ii = 0; ii < clip_delta.length(); ii++)
|
||||
{
|
||||
if (clip_delta[ii] == 0)
|
||||
{
|
||||
throw std::invalid_argument("Submitted clip area contains a dimension of size zero.");
|
||||
}
|
||||
}
|
||||
|
||||
return glm::mod(vertex - clip_upper, clip_delta) + clip_lower;
|
||||
}
|
||||
|
||||
/*!
|
||||
|
|
Loading…
Reference in New Issue